Search in sources :

Example 11 with EncodeHintType

use of com.google.zxing.EncodeHintType in project QRCode by 5peak2me.

the class EncodingHandler method createQRCode.

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;
            } else {
                //解决此Bitmap保存到本地图片变黑色的bug
                pixels[y * width + x] = BACKGROUNDCOLRO;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) MultiFormatWriter(com.google.zxing.MultiFormatWriter) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix)

Example 12 with EncodeHintType

use of com.google.zxing.EncodeHintType in project smartmodule by carozhu.

the class GenerateQrcodePIcHelper method createQRCodeWithLogo.

/**
     * 生成带logo的二维码,logo默认为二维码的1/5
     *
     * @param text    需要生成二维码的文字、网址等
     * @param size    需要生成二维码的大小()
     * @param mBitmap logo文件
     * @return bitmap
     */
public static Bitmap createQRCodeWithLogo(String text, int size, Bitmap mBitmap) {
    try {
        IMAGE_HALFWIDTH = size / 10;
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        /*
             * 设置容错级别,默认为ErrorCorrectionLevel.L
             * 因为中间加入logo所以建议你把容错级别调至H,否则可能会出现识别不了
             */
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);
        //矩阵高度
        int width = bitMatrix.getWidth();
        //矩阵宽度
        int height = bitMatrix.getHeight();
        int halfW = width / 2;
        int halfH = height / 2;
        Matrix m = new Matrix();
        float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
        float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight();
        m.setScale(sx, sy);
        //设置缩放信息
        //将logo图片按martix设置的信息缩放
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {
                    //该位置用于存放图片信息
                    //记录图片每个像素信息
                    pixels[y * width + x] = mBitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
                } else {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * size + x] = 0xff000000;
                    } else {
                        //灰色-- 0xEEEEEEEE md_grey_200
                        pixels[y * size + x] = 0xFFFFFFFF;
                    }
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) BitMatrix(com.google.zxing.common.BitMatrix) Matrix(android.graphics.Matrix) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException)

Example 13 with EncodeHintType

use of com.google.zxing.EncodeHintType in project jfinal by jfinal.

the class QrCodeRender method render.

public void render() {
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/png");
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    //去掉白色边框,极度重要,否则二维码周围的白边会很宽
    hints.put(EncodeHintType.MARGIN, 0);
    if (errorCorrectionLevel != null) {
        hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
    }
    try {
        // MultiFormatWriter 可支持多种格式的条形码,在此直接使用 QRCodeWriter,通过查看源码可知少创建一个对象
        // BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        // 经测试 200 X 200 大小的二维码使用 "png" 格式只有 412B,而 "jpg" 却达到 15KB
        // format: "jpg"、"png"
        MatrixToImageWriter.writeToStream(bitMatrix, "png", response.getOutputStream());
    } catch (Exception e) {
        throw new RenderException(e);
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) RenderException(com.jfinal.render.RenderException) EncodeHintType(com.google.zxing.EncodeHintType) HashMap(java.util.HashMap) BitMatrix(com.google.zxing.common.BitMatrix) RenderException(com.jfinal.render.RenderException)

Example 14 with EncodeHintType

use of com.google.zxing.EncodeHintType in project Lazy by l123456789jy.

the class CreatQRCodeImg method createQRImage.

/**
     * 生成二维码Bitmap
     *
     * @param content   内容
     * @param widthPix  图片宽度
     * @param heightPix 图片高度
     * @param logoBm    二维码中心的Logo图标(可以为null)
     * @param filePath  用于存储二维码图片的文件路径
     * @return 生成二维码及保存文件是否成功
     */
public static boolean createQRImage(String content, int widthPix, int heightPix, Bitmap logoBm, String filePath) {
    try {
        if (content == null || "".equals(content)) {
            return false;
        }
        //配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //设置空白边距的宽度
        //            hints.put(EncodeHintType.MARGIN, 2); //default is 4
        // 图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000;
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {
            bitmap = addLogo(bitmap, logoBm);
        }
        //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 70, new FileOutputStream(filePath));
    } catch (WriterException | IOException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : HashMap(java.util.HashMap) BitMatrix(com.google.zxing.common.BitMatrix) IOException(java.io.IOException) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) FileOutputStream(java.io.FileOutputStream) WriterException(com.google.zxing.WriterException)

Example 15 with EncodeHintType

use of com.google.zxing.EncodeHintType in project camel by apache.

the class BarcodeDataFormatTest method testAddToHintMapEncodeHintTypeObject.

/**
     * Test of addToHintMap method, of class BarcodeDataFormat.
     */
@Test
public final void testAddToHintMapEncodeHintTypeObject() {
    EncodeHintType hintType = EncodeHintType.MARGIN;
    Object value = 10;
    BarcodeDataFormat instance = new BarcodeDataFormat();
    instance.addToHintMap(hintType, value);
    assertTrue(instance.getWriterHintMap().containsKey(hintType));
    assertEquals(instance.getWriterHintMap().get(hintType), value);
}
Also used : EncodeHintType(com.google.zxing.EncodeHintType) Test(org.junit.Test)

Aggregations

EncodeHintType (com.google.zxing.EncodeHintType)25 BitMatrix (com.google.zxing.common.BitMatrix)23 Bitmap (android.graphics.Bitmap)13 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)11 Hashtable (java.util.Hashtable)9 EnumMap (java.util.EnumMap)8 MultiFormatWriter (com.google.zxing.MultiFormatWriter)7 WriterException (com.google.zxing.WriterException)7 Test (org.junit.Test)5 BufferedImage (java.awt.image.BufferedImage)3 HashMap (java.util.HashMap)3 BinaryBitmap (com.google.zxing.BinaryBitmap)2 SymbolShapeHint (com.google.zxing.datamatrix.encoder.SymbolShapeHint)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Matrix (android.graphics.Matrix)1 JCommander (com.beust.jcommander.JCommander)1 Throwables (com.google.common.base.Throwables)1 BarcodeFormat (com.google.zxing.BarcodeFormat)1 ResultPoint (com.google.zxing.ResultPoint)1