Search in sources :

Example 1 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project Conversations by siacs.

the class BarcodeProvider method create2dBarcodeBitmap.

public static Bitmap create2dBarcodeBitmap(String input, int size) {
    try {
        final QRCodeWriter barcodeWriter = new QRCodeWriter();
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
        final int width = result.getWidth();
        final int height = result.getHeight();
        final int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            final int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
            }
        }
        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (final Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project cas by apereo.

the class OneTimeTokenQRGeneratorController method generateQRCode.

private static void generateQRCode(final OutputStream stream, final String key) {
    try {
        final Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class);
        hintMap.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
        hintMap.put(EncodeHintType.MARGIN, 2);
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        final QRCodeWriter qrCodeWriter = new QRCodeWriter();
        final BitMatrix byteMatrix = qrCodeWriter.encode(key, BarcodeFormat.QR_CODE, 250, 250, hintMap);
        final int width = byteMatrix.getWidth();
        final BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        final Graphics2D graphics = (Graphics2D) image.getGraphics();
        try {
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, width, width);
            graphics.setColor(Color.BLACK);
            IntStream.range(0, width).forEach(i -> {
                IntStream.range(0, width).filter(j -> byteMatrix.get(i, j)).forEach(j -> graphics.fillRect(i, j, 1, 1));
            });
        } finally {
            graphics.dispose();
        }
        ImageIO.write(image, "png", stream);
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Color(java.awt.Color) OutputStream(java.io.OutputStream) IntStream(java.util.stream.IntStream) BufferedImage(java.awt.image.BufferedImage) EnumMap(java.util.EnumMap) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) Throwables(com.google.common.base.Throwables) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel) RestController(org.springframework.web.bind.annotation.RestController) StandardCharsets(java.nio.charset.StandardCharsets) HttpServletRequest(javax.servlet.http.HttpServletRequest) Graphics2D(java.awt.Graphics2D) EncodeHintType(com.google.zxing.EncodeHintType) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) GetMapping(org.springframework.web.bind.annotation.GetMapping) BitMatrix(com.google.zxing.common.BitMatrix) BarcodeFormat(com.google.zxing.BarcodeFormat) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) BitMatrix(com.google.zxing.common.BitMatrix) EnumMap(java.util.EnumMap) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 3 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project collect by opendatakit.

the class QRCodeUtils method generateQRBitMap.

public static Bitmap generateQRBitMap(String data, int sideLength) throws IOException, WriterException {
    final long time = System.currentTimeMillis();
    String compressedData = CompressionUtils.compress(data);
    // Maximum capacity for QR Codes is 4,296 characters (Alphanumeric)
    if (compressedData.length() > 4000) {
        throw new IOException(Collect.getInstance().getString(R.string.encoding_max_limit));
    }
    Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(compressedData, BarcodeFormat.QR_CODE, sideLength, sideLength, hints);
    Bitmap bmp = Bitmap.createBitmap(sideLength, sideLength, Bitmap.Config.RGB_565);
    for (int x = 0; x < sideLength; x++) {
        for (int y = 0; y < sideLength; y++) {
            bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
        }
    }
    Timber.i("QR Code generation took : %d ms", (System.currentTimeMillis() - time));
    return bmp;
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) EncodeHintType(com.google.zxing.EncodeHintType) HashMap(java.util.HashMap) IOException(java.io.IOException) BitMatrix(com.google.zxing.common.BitMatrix) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)

Example 4 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project pancm_project by xuwujing.

the class QrCodeCreateUtil method createQrCode.

/**
 * 生成包含字符串信息的二维码图片
 *
 * @param outputStream 文件输出流路径
 * @param content      二维码携带信息
 * @param qrCodeSize   二维码图片大小
 * @param imageFormat  二维码的格式
 * @throws WriterException
 * @throws IOException
 */
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException {
    // 设置二维码纠错级别MAP
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    // 矫错级别
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    // 创建比特矩阵(位矩阵)的QR码编码的字符串
    BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
    // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
    int matrixWidth = byteMatrix.getWidth();
    BufferedImage image = new BufferedImage(matrixWidth - 200, matrixWidth - 200, BufferedImage.TYPE_INT_RGB);
    image.createGraphics();
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, matrixWidth, matrixWidth);
    // 使用比特矩阵画并保存图像
    graphics.setColor(Color.BLACK);
    for (int i = 0; i < matrixWidth; i++) {
        for (int j = 0; j < matrixWidth; j++) {
            if (byteMatrix.get(i, j)) {
                graphics.fillRect(i - 100, j - 100, 1, 1);
            }
        }
    }
    return ImageIO.write(image, imageFormat, outputStream);
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 5 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter 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)

Aggregations

BitMatrix (com.google.zxing.common.BitMatrix)32 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)32 EncodeHintType (com.google.zxing.EncodeHintType)24 WriterException (com.google.zxing.WriterException)22 Bitmap (android.graphics.Bitmap)19 Hashtable (java.util.Hashtable)12 HashMap (java.util.HashMap)7 BufferedImage (java.awt.image.BufferedImage)6 IOException (java.io.IOException)5 EnumMap (java.util.EnumMap)5 BinaryBitmap (com.google.zxing.BinaryBitmap)4 ErrorCorrectionLevel (com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)4 Graphics2D (java.awt.Graphics2D)4 Color (java.awt.Color)3 Paint (android.graphics.Paint)2 NonNull (android.support.annotation.NonNull)2 Sigchain (co.krypt.krypton.team.Sigchain)2 BarcodeFormat (com.google.zxing.BarcodeFormat)2 BarcodeEncoder (com.journeyapps.barcodescanner.BarcodeEncoder)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2