Java 类com.google.zxing.MultiFormatWriter 实例源码

项目:super-contacts    文件:BarcodeWriter.java   
public static Bitmap encodeStringToBitmap(String contents) throws WriterException {
    //Null check, just b/c
    if (contents == null) {
        return null;
    }
    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, BarcodeFormat.PDF_417, 700, 900, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:GitHub    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:GitHub    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:IJPay    文件:ZxingKit.java   
/**
 * Zxing图形码生成工具
 *
 * @param contents
 *            内容
 * @param barcodeFormat
 *            BarcodeFormat对象
 * @param format
 *            图片格式,可选[png,jpg,bmp]
 * @param width
 *            宽
 * @param height
 *            高
 * @param margin
 *            边框间距px
 * @param saveImgFilePath
 *            存储图片的完整位置,包含文件名
 * @return {boolean}
 */
public static boolean encode(String contents, BarcodeFormat barcodeFormat, Integer margin,
        ErrorCorrectionLevel errorLevel, String format, int width, int height, String saveImgFilePath) {
    Boolean bool = false;
    BufferedImage bufImg;
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    // 指定纠错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, errorLevel);
    hints.put(EncodeHintType.MARGIN, margin);
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
        // contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, barcodeFormat, width, height, hints);
        MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
        bufImg = MatrixToImageWriter.toBufferedImage(bitMatrix, config);
        bool = writeToFile(bufImg, format, saveImgFilePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bool;
}
项目:framework    文件:BarCodeUtils.java   
/**
 * 条形码编码
 */
public static BufferedImage encode(String contents, int width, int height) {
    int codeWidth = 3 + // start guard
            (7 * 6) + // left bars
            5 + // middle guard
            (7 * 6) + // right bars
            3; // end guard
    codeWidth = Math.max(codeWidth, width);
    try {
        // 原为13位Long型数字(BarcodeFormat.EAN_13),现为128位
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, codeWidth, height, null);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
项目:framework    文件:QRCodeUtils.java   
/**
 * 创建二维码
 *
 * @param size         二维码尺寸
 * @param content      二维码内容
 * @param logoPath     Logo地址
 * @param needCompress 是否压缩
 * @return
 * @throws Exception
 */
private static BufferedImage createImage(int size, String content, String logoPath, boolean needCompress) throws Exception {
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
        }
    }
    if (logoPath == null || "".equals(logoPath)) {
        return image;
    }
    // 插入图片
    QRCodeUtils.insertImage(size, image, logoPath, needCompress);
    return image;
}
项目:keepass2android    文件:QRCodeEncoder.java   
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
    if (encoding != null) {

        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    hints.put(EncodeHintType.MARGIN, 2); /* default = 4 */  


    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:mobile-store    文件:QRCodeEncoder.java   
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:CodeScaner    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:os    文件:MatrixToImageWriterEx.java   
/**
 * 根据内容生成二维码数据
 * @param content 二维码文字内容[为了信息安全性,一般都要先进行数据加密]
 * @param width 二维码照片宽度
 * @param height 二维码照片高度
 * @return
 */
public static BitMatrix createQRCode(String content, int width, int height){
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();   
    //设置字符编码
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
       // 指定纠错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.MARGIN, 1);
       BitMatrix matrix = null;  
       try {  
           matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); 
       } catch (WriterException e) {  
           e.printStackTrace();  
       }

       return matrix;
}
项目:MyFire    文件:ScanMainActivity.java   
/**
 * 用字符串生成二维码
 * 
 * @param str
 * @author J!nl!n
 * @return
 * @throws WriterException
 */
public Bitmap Create2DCode(String str) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 480, 480, hints);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    // 二维矩阵转为一维像素数组,也就是一直横着排了
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = 0xff000000;
            }
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // 通过像素数组生成bitmap,具体参考api
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:MyFire    文件:CodeUtils.java   
/**
 * 用字符串生成二维码
 *
 * @param str
 * @author J!nl!n
 * @return
 * @throws WriterException
 */
public static Bitmap Create2DCode(String str) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 480, 480, hints);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    // 二维矩阵转为一维像素数组,也就是一直横着排了
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = 0xff000000;
            }
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // 通过像素数组生成bitmap,具体参考api
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:androidtools    文件:QRCodeUtils.java   
/**
 * Generate a two-dimensional code image
 *
 * @param content                  An incoming string, usually a URL
 * @param widthAndHeight           widthAndHeight
 * @param openErrorCorrectionLevel Does fault tolerance open?
 * @return A two-dimensional code image [Bitmap]
 */
public static Bitmap createQRCode(String content, int widthAndHeight, boolean openErrorCorrectionLevel) {
    try {
        if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) {
            return null;
        }
        //Processing Chinese characters, if you do not need to change the source code, convert the string into ISO-8859-1 code.
        Map hints = openErrorCorrectionLevel(openErrorCorrectionLevel);
        BitMatrix matrix = new MultiFormatWriter().encode(new String(content.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
        matrix = updateBit(matrix, 8);
        Bitmap bitmap = generateQRBitmap(matrix);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
项目:19porn    文件:InvitationActivity.java   
private Bitmap createQRCode(String str, int widthAndHeight)
        throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 使用utf8编码
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);// 这里需要把hints传进去,否则会出现中文乱码
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    // 上色,如果不做保存二维码、分享二维码等功能,上白色部分可以不写。至于原因,在生成图片的时候,如果没有指定颜色,其会使用系统默认颜色来上色,很多情况就会出现保存的二维码图片全黑
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {// 有数据的像素点使用黑色
                pixels[y * width + x] = BLACK;
            } else {// 其他部分则使用白色
                pixels[y * width + x] = WHITE;
            }
        }
    }
    //生成bitmap
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:civify-app    文件:ShowQrFragment.java   
Bitmap encodeAsBitmap(String str) throws WriterException {
    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, WIDTH, WIDTH, null);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int w = result.getWidth();
    int h = result.getHeight();
    int[] pixels = new int[w * h];
    for (int y = 0; y < h; y++) {
        int offset = y * w;
        for (int x = 0; x < w; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }

    }
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
    return bitmap;
}
项目:APIJSON-Android-RxJava    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:Hitalk    文件:QRCodeEncoder.java   
/**
 * 同步创建指定前景色、指定背景色、带logo的二维码图片。该方法是耗时操作,请在子线程中调用。
 *
 * @param content         要生成的二维码图片内容
 * @param size            图片宽高,单位为px
 * @param foregroundColor 二维码图片的前景色
 * @param backgroundColor 二维码图片的背景色
 * @param logo            二维码图片的logo
 */
public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) {
    try {
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, HINTS);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * size + x] = foregroundColor;
                } else {
                    pixels[y * size + x] = backgroundColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return addLogoToQRCode(bitmap, logo);
    } catch (Exception e) {
        return null;
    }
}
项目:automat    文件:QrcodeUtil.java   
public static String createQrcode(String dir, String _text) {
    String qrcodeFilePath = "";
    try {
        int qrcodeWidth = 300;
        int qrcodeHeight = 300;
        String qrcodeFormat = "png";
        HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(_text, BarcodeFormat.QR_CODE, qrcodeWidth,
                qrcodeHeight, hints);

        BufferedImage image = new BufferedImage(qrcodeWidth, qrcodeHeight, BufferedImage.TYPE_INT_RGB);
        File qrcodeFile = new File(dir + "/" + UUID.randomUUID().toString() + "." + qrcodeFormat);
        ImageIO.write(image, qrcodeFormat, qrcodeFile);
        MatrixToImageWriter.writeToPath(bitMatrix, qrcodeFormat, qrcodeFile.toPath());
        qrcodeFilePath = qrcodeFile.getAbsolutePath();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return qrcodeFilePath;
}
项目:Udhari    文件:QrCodeActivity.java   
private Bitmap endcode(String input) {
    BarcodeFormat format = BarcodeFormat.QR_CODE;
    EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
    hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    BitMatrix result = null;
    try {
        result = new MultiFormatWriter().encode(input, BarcodeFormat.QR_CODE, 500, 500, hint);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    int w = result.getWidth();
    int h = result.getHeight();
    int[] pixels = new int[w * h];
    for (int y = 0; y < h; y++) {
        int offset = y * w;
        for (int x = 0; x < w; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }
    Bitmap bit = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bit.setPixels(pixels, 0, w, 0, 0, w, h);
    ImageView imageView = (ImageView) findViewById(R.id.qr_code_id);
    imageView.setImageBitmap(bit);
    return bit;
}
项目:AndroidBasicLibs    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:ZxingForAndroid    文件:QRCodeEncoder.java   
/**
 * 同步创建指定前景色、指定背景色、带logo的二维码图片。该方法是耗时操作,请在子线程中调用。
 *
 * @param content         要生成的二维码图片内容
 * @param size            图片宽高,单位为px
 * @param foregroundColor 二维码图片的前景色
 * @param backgroundColor 二维码图片的背景色
 * @param logo            二维码图片的logo
 */
public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) {
    try {
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, HINTS);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * size + x] = foregroundColor;
                } else {
                    pixels[y * size + x] = backgroundColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return addLogoToQRCode(bitmap, logo);
    } catch (Exception e) {
        return null;
    }
}
项目:payment-wechat    文件:ZxingUtil.java   
public static void barCode(String contents,String imgPath,int width, int height) {
    int codeWidth = 3 + // start guard
            (7 * 6) + // left bars
            5 + // middle guard
            (7 * 6) + // right bars
            3; // end guard
    codeWidth = Math.max(codeWidth, width);
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.CODE_128, codeWidth, height, null);
        bitMatrix = deleteWhite(bitMatrix);
        MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
    }catch (Exception e) {
        log.error(e.getMessage());
        e.printStackTrace();
    }
}
项目:OSchina_resources_android    文件:QrCodeUtils.java   
/**
 * 传入字符串生成二维码
 *
 * @param str
 * @return
 * @throws WriterException
 */
public static Bitmap Create2DCode(String str) throws WriterException {
    // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, 300, 300);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    // 二维矩阵转为一维像素数组,也就是一直横着排了
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = 0xff000000;
            }

        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    // 通过像素数组生成bitmap,具体参考api
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:tvConnect_android    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str, int size) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, size, size);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];
    for(int x = 0; x < width; x ++){
        for(int y = 0; y < height; y ++){
            if(matrix.get(x, y)){
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:tvConnect_android    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str, int size) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, size, size);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];
    for(int x = 0; x < width; x ++){
        for(int y = 0; y < height; y ++){
            if(matrix.get(x, y)){
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:DailyStudy    文件:QRCodeHelper.java   
/**
 * 生成二维码
 *
 * @param content 二维码内容
 * @param size    二维码内容大小
 * @param color   二维码颜色
 */
public static Bitmap createQRCode(String content, int size,int bgColor, int color) {
    try {
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, CHARACTER_SET);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * size + x] = color;
                } else {
                    pixels[y * size + x] = bgColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return bitmap;
    } catch (WriterException e) {
        return null;
    }
}
项目:Lunary-Ethereum-Wallet    文件:QREncoder.java   
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:Fetax-AI    文件:CodeUtil.java   
private static BufferedImage createImage(String content, String imgPath,
        boolean needCompress) throws Exception {
    Hashtable hints = new Hashtable();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
            BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BufferedImage image = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
                    : 0xFFFFFFFF);
        }
    }
    if (imgPath == null || "".equals(imgPath)) {
        return image;
    }
 // 插入图片
    CodeUtil.insertImage(image, imgPath, needCompress);
    return image;
}
项目:Fetax-AI    文件:CodeUtil.java   
/**
 * 生成条形二维码
 */
public static byte[] encode(String contents, int width, int height, String imgPath) {
    int codeWidth = 3 + // start guard
            (7 * 6) + // left bars
            5 + // middle guard
            (7 * 6) + // right bars
            3; // end guard
    codeWidth = Math.max(codeWidth, width);
    byte[] buf = null;
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                BarcodeFormat.CODE_128, codeWidth, height, null);
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
      ImageIO.write(image, FORMAT_NAME, out);
      out.close();
buf = out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return buf;
}
项目:JavaToolKit    文件:CreateQRCode.java   
/**
 *
 * @param path 完整的路径包括文件名
 * @param width 图片宽
 * @param height 图片高
 * @param contents 内容
 */
public static void createImage(String path,int width,int height,String contents){
    String format = "png";
    //定义二维码的参数
    HashMap hints = new HashMap();
    hints.put(EncodeHintType.AZTEC_LAYERS, "utf-8");//设置编码
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//设置容错等级: L M Q H
    hints.put(EncodeHintType.MARGIN,1);//设置边距

    //生成文件
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,hints);
        Path file = new File(path).toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, format, file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
项目:JAVA-    文件:QrcodeUtil.java   
public static String createQrcode(String dir, String _text) {
    String qrcodeFilePath = "";
    try {
        int qrcodeWidth = 300;
        int qrcodeHeight = 300;
        String qrcodeFormat = "png";
        HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(_text, BarcodeFormat.QR_CODE, qrcodeWidth,
                qrcodeHeight, hints);

        BufferedImage image = new BufferedImage(qrcodeWidth, qrcodeHeight, BufferedImage.TYPE_INT_RGB);
        File qrcodeFile = new File(dir + "/" + UUID.randomUUID().toString() + "." + qrcodeFormat);
        ImageIO.write(image, qrcodeFormat, qrcodeFile);
        MatrixToImageWriter.writeToPath(bitMatrix, qrcodeFormat, qrcodeFile.toPath());
        qrcodeFilePath = qrcodeFile.getAbsolutePath();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return qrcodeFilePath;
}
项目:java-qrcode-terminal    文件:QRterminal.java   
public static String getQr(String text) {
    String s = "生成二维码失败";
    int width = 40;
    int height = 40;
    // 用于设置QR二维码参数
    Hashtable<EncodeHintType, Object> qrParam = new Hashtable<EncodeHintType, Object>();
    // 设置QR二维码的纠错级别——这里选择最低L级别
    qrParam.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    qrParam.put(EncodeHintType.CHARACTER_SET, "utf-8");
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, qrParam);
        s = toAscii(bitMatrix);
    } catch (WriterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return s;
}
项目:MeiLa_GNN    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:NeiHanDuanZiTV    文件:QRCodeEncoder.java   
/**
 * 同步创建指定前景色、指定背景色、带logo的二维码图片。该方法是耗时操作,请在子线程中调用。
 *
 * @param content         要生成的二维码图片内容
 * @param size            图片宽高,单位为px
 * @param foregroundColor 二维码图片的前景色
 * @param backgroundColor 二维码图片的背景色
 * @param logo            二维码图片的logo
 */
public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) {
    try {
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, HINTS);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * size + x] = foregroundColor;
                } else {
                    pixels[y * size + x] = backgroundColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return addLogoToQRCode(bitmap, logo);
    } catch (Exception e) {
        return null;
    }
}
项目:angit    文件:ZxingHelper.java   
/**
 * 条形码编码
 * <p>
 * String imgPath = "target/zxing.png";
 * String contents = "6923450657713";
 * int width = 105, height = 50;
 * ZxingHelper.encode(contents, width, height, imgPath);
 *
 * @param contents 内容
 * @param width    宽度
 * @param height   高度
 * @param imgPath  生成图片路径
 */
public static void encode(String contents, int width, int height, String imgPath) {
    int codeWidth = 3 + // start guard
        (7 * 6) + // left bars
        5 + // middle guard
        (7 * 6) + // right bars
        3; // end guard
    codeWidth = Math.max(codeWidth, width);
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
            BarcodeFormat.EAN_13, codeWidth, height, null);

        MatrixToImageWriter
            .writeToPath(bitMatrix, "png", new File(imgPath).toPath());

    } catch (Exception e) {
        LOGGER.error("生成条形码错误", e);
    }
}
项目:angit    文件:ZxingHelper.java   
/**
 * 二维码编码
 * <p>
 * String imgPath2 = "target/zxing2.png";
 * String contents2 = "Hello Zxing!";
 * int width2 = 300, height2 = 300;
 * ZxingHelper.encode2(contents2, width2, height2, imgPath2);
 *
 * @param contents 内容
 * @param width    宽度
 * @param height   高度
 * @param imgPath  生成图片路径
 */
public static void encode2(String contents, int width, int height, String imgPath) {
    EnumMap<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    // 指定纠错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    // 指定编码格式
    hints.put(EncodeHintType.CHARACTER_SET, "GBK");
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
            BarcodeFormat.QR_CODE, width, height, hints);

        MatrixToImageWriter
            .writeToPath(bitMatrix, "png", new File(imgPath).toPath());

    } catch (Exception e) {
        LOGGER.error("生成二维码错误", e);
    }
}
项目:trust-wallet-android    文件:MyAddressActivity.java   
private Bitmap createQRImage(String address) {
    Point size = new Point();
    getWindowManager().getDefaultDisplay().getSize(size);
    int imageSize = (int) (size.x * QR_IMAGE_WIDTH_RATIO);
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(
                address,
                BarcodeFormat.QR_CODE,
                imageSize,
                imageSize,
                null);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (Exception e) {
        Toast.makeText(this, getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
            .show();
    }
    return null;
}
项目:CryptoPayAPI    文件:CryptoClient.java   
/**
 * Method to get the JavaFX Image QR code for easy input of address
 * @param width width of the image
 * @param height height of the image
 * @return Java FX Image
 * @throws IOException Either when there is an encoding error or java's reserved memory is overwritten.
 * @throws WriterException When ZXING encounters an error.
 */
public Image getQR(int width, int height) throws IOException, WriterException{
    String charset = "UTF-8";
    Map hintMap = new HashMap();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    BitMatrix matrix = new MultiFormatWriter().encode(new String(cryptoAddress.getBytes(charset), charset), BarcodeFormat.QR_CODE, width, height, hintMap);
    MatrixToImageWriter.writeToStream(matrix, "png", stream);
    stream.flush();

    byte[] data = stream.toByteArray();
    stream.close();

    return new Image(new ByteArrayInputStream(data));
}
项目:Mobike    文件:EncodingHandler.java   
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
项目:leoapp-sources    文件:QRWriteTask.java   
private Bitmap createNewQR(String s) {
    MultiFormatWriter mFW = new MultiFormatWriter();
    Bitmap bM = null;

    int px = (int) GraphicUtils.dpToPx(250);

    try {
        BitMatrix bitM = mFW.encode(s, BarcodeFormat.QR_CODE, px, px);
        bM = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
        for (int i = 0; i < px; i++) {
            for (int j = 0; j < px; j++) {
                int c = (bitM.get(i, j)) ? Color.BLACK : Color.WHITE;
                bM.setPixel(i, j, c);
            }
        }
    } catch (WriterException e) {
        Utils.logError(e);
    }
    return bM;
}