Search in sources :

Example 16 with QRCodeWriter

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

use of com.google.zxing.qrcode.QRCodeWriter in project Signal-Android by WhisperSystems.

the class QrCode method create.

@NonNull
public static Bitmap create(String data) {
    try {
        BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512);
        Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
        for (int y = 0; y < result.getHeight(); y++) {
            for (int x = 0; x < result.getWidth(); x++) {
                if (result.get(x, y)) {
                    bitmap.setPixel(x, y, Color.BLACK);
                }
            }
        }
        return bitmap;
    } catch (WriterException e) {
        Log.w(TAG, e);
        return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException) NonNull(android.support.annotation.NonNull)

Example 18 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project KeyBox by skavanagh.

the class OTPAction method qrImage.

@Action(value = "/admin/qrImage")
public String qrImage() {
    String username = UserDB.getUser(AuthUtil.getUserId(servletRequest.getSession())).getUsername();
    String secret = AuthUtil.getOTPSecret(servletRequest.getSession());
    AuthUtil.setOTPSecret(servletRequest.getSession(), null);
    try {
        String qrCodeText = "otpauth://totp/KeyBox%20%28" + URLEncoder.encode(servletRequest.getHeader("host").replaceAll("\\:.*$", ""), "utf-8") + "%29:" + username + "?secret=" + secret;
        QRCodeWriter qrWriter = new QRCodeWriter();
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix matrix = qrWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, hints);
        servletResponse.setContentType("image/png");
        BufferedImage image = new BufferedImage(QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT);
        graphics.setColor(Color.BLACK);
        for (int x = 0; x < QR_IMAGE_WIDTH; x++) {
            for (int y = 0; y < QR_IMAGE_HEIGHT; y++) {
                if (matrix.get(x, y)) {
                    graphics.fillRect(x, y, 1, 1);
                }
            }
        }
        ImageIO.write(image, "png", servletResponse.getOutputStream());
        servletResponse.getOutputStream().flush();
        servletResponse.getOutputStream().close();
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
    return null;
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) BufferedImage(java.awt.image.BufferedImage) Action(org.apache.struts2.convention.annotation.Action)

Example 19 with QRCodeWriter

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

the class ChartServlet method doEncode.

private static void doEncode(ServletRequest request, HttpServletResponse response, boolean isPost) throws IOException {
    ChartServletRequestParameters parameters;
    try {
        parameters = doParseParameters(request, isPost);
    } catch (IllegalArgumentException | NullPointerException e) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.toString());
        return;
    }
    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    hints.put(EncodeHintType.MARGIN, parameters.getMargin());
    if (!StandardCharsets.ISO_8859_1.equals(parameters.getOutputEncoding())) {
        // Only set if not QR code default
        hints.put(EncodeHintType.CHARACTER_SET, parameters.getOutputEncoding().name());
    }
    hints.put(EncodeHintType.ERROR_CORRECTION, parameters.getEcLevel());
    BitMatrix matrix;
    try {
        matrix = new QRCodeWriter().encode(parameters.getText(), BarcodeFormat.QR_CODE, parameters.getWidth(), parameters.getHeight(), hints);
    } catch (WriterException we) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, we.toString());
        return;
    }
    ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
    MatrixToImageWriter.writeToStream(matrix, "PNG", pngOut);
    byte[] pngData = pngOut.toByteArray();
    response.setContentType("image/png");
    response.setContentLength(pngData.length);
    response.setHeader("Cache-Control", "public");
    response.getOutputStream().write(pngData);
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) EnumMap(java.util.EnumMap) WriterException(com.google.zxing.WriterException)

Example 20 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project JustAndroid by chinaltz.

the class CodeUtils method createImage.

/**
     * 生成二维码图片
     * @param text
     * @param w
     * @param h
     * @param logo
     * @return
     */
public static Bitmap createImage(String text, int w, int h, Bitmap logo) {
    if (TextUtils.isEmpty(text)) {
        return null;
    }
    try {
        Bitmap scaleLogo = getScaleLogo(logo, w, h);
        int offsetX = w / 2;
        int offsetY = h / 2;
        int scaleWidth = 0;
        int scaleHeight = 0;
        if (scaleLogo != null) {
            scaleWidth = scaleLogo.getWidth();
            scaleHeight = scaleLogo.getHeight();
            offsetX = (w - scaleWidth) / 2;
            offsetY = (h - scaleHeight) / 2;
        }
        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, 0);
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, w, h, hints);
        int[] pixels = new int[w * h];
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (x >= offsetX && x < offsetX + scaleWidth && y >= offsetY && y < offsetY + scaleHeight) {
                    int pixel = scaleLogo.getPixel(x - offsetX, y - offsetY);
                    if (pixel == 0) {
                        if (bitMatrix.get(x, y)) {
                            pixel = 0xff000000;
                        } else {
                            pixel = 0xffffffff;
                        }
                    }
                    pixels[y * w + x] = pixel;
                } else {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * w + x] = 0xff000000;
                    } else {
                        pixels[y * w + x] = 0xffffffff;
                    }
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) 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