Search in sources :

Example 66 with BitMatrix

use of com.google.zxing.common.BitMatrix in project BGAQRCode-Android by bingoogolapple.

the class QRCodeEncoder method syncEncodeQRCode.

/**
     * 同步创建指定前景色、指定背景色、带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;
    }
}
Also used : Bitmap(android.graphics.Bitmap) MultiFormatWriter(com.google.zxing.MultiFormatWriter) BitMatrix(com.google.zxing.common.BitMatrix)

Example 67 with BitMatrix

use of com.google.zxing.common.BitMatrix 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 68 with BitMatrix

use of com.google.zxing.common.BitMatrix 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 69 with BitMatrix

use of com.google.zxing.common.BitMatrix in project zxingfragmentlib by mitoyarzun.

the class QRCodeEncoder method encodeAsBitmap.

Bitmap encodeAsBitmap() throws WriterException {
    String contentsToEncode = contents;
    if (contentsToEncode == null) {
        return null;
    }
    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contentsToEncode);
    if (encoding != null) {
        hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    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) ? BLACK : WHITE;
        }
    }
    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) BitMatrix(com.google.zxing.common.BitMatrix)

Example 70 with BitMatrix

use of com.google.zxing.common.BitMatrix in project zxing by zxing.

the class DataMatrixWriter method convertByteMatrixToBitMatrix.

/**
   * Convert the ByteMatrix to BitMatrix.
   *
   * @param matrix The input matrix.
   * @return The output matrix.
   */
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) {
    int matrixWidgth = matrix.getWidth();
    int matrixHeight = matrix.getHeight();
    BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight);
    output.clear();
    for (int i = 0; i < matrixWidgth; i++) {
        for (int j = 0; j < matrixHeight; j++) {
            // Zero is white in the bytematrix
            if (matrix.get(i, j) == 1) {
                output.set(i, j);
            }
        }
    }
    return output;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) SymbolShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint)

Aggregations

BitMatrix (com.google.zxing.common.BitMatrix)119 EncodeHintType (com.google.zxing.EncodeHintType)27 ResultPoint (com.google.zxing.ResultPoint)26 Test (org.junit.Test)20 Bitmap (android.graphics.Bitmap)18 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)17 WriterException (com.google.zxing.WriterException)14 DecoderResult (com.google.zxing.common.DecoderResult)12 EnumMap (java.util.EnumMap)11 MultiFormatWriter (com.google.zxing.MultiFormatWriter)10 DetectorResult (com.google.zxing.common.DetectorResult)10 Hashtable (java.util.Hashtable)10 AztecDetectorResult (com.google.zxing.aztec.AztecDetectorResult)8 Result (com.google.zxing.Result)7 Point (com.google.zxing.aztec.detector.Detector.Point)5 SymbolShapeHint (com.google.zxing.datamatrix.encoder.SymbolShapeHint)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NotFoundException (com.google.zxing.NotFoundException)4