Search in sources :

Example 1 with BitMatrix

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

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

use of com.google.zxing.common.BitMatrix in project android-zxingLibrary by yipianfengye.

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)

Example 4 with BitMatrix

use of com.google.zxing.common.BitMatrix in project android-zxingLibrary by yipianfengye.

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;
            }
        }
    }
    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 5 with BitMatrix

use of com.google.zxing.common.BitMatrix in project weex-example by KalicyZhou.

the class MaxiCodeReader method extractPureBits.

/**
   * This method detects a code in a "pure" image -- that is, pure monochrome image
   * which contains only an unrotated, unskewed, image of a code, with some white border
   * around it. This is a specialized method that works exceptionally fast in this special
   * case.
   *
   * @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix)
   * @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix)
   */
private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
    int[] enclosingRectangle = image.getEnclosingRectangle();
    if (enclosingRectangle == null) {
        throw NotFoundException.getNotFoundInstance();
    }
    int left = enclosingRectangle[0];
    int top = enclosingRectangle[1];
    int width = enclosingRectangle[2];
    int height = enclosingRectangle[3];
    // Now just read off the bits
    BitMatrix bits = new BitMatrix(MATRIX_WIDTH, MATRIX_HEIGHT);
    for (int y = 0; y < MATRIX_HEIGHT; y++) {
        int iy = top + (y * height + height / 2) / MATRIX_HEIGHT;
        for (int x = 0; x < MATRIX_WIDTH; x++) {
            int ix = left + (x * width + width / 2 + (y & 0x01) * width / 2) / MATRIX_WIDTH;
            if (image.get(ix, iy)) {
                bits.set(x, y);
            }
        }
    }
    return bits;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) ResultPoint(com.google.zxing.ResultPoint)

Aggregations

BitMatrix (com.google.zxing.common.BitMatrix)114 ResultPoint (com.google.zxing.ResultPoint)26 EncodeHintType (com.google.zxing.EncodeHintType)23 Test (org.junit.Test)20 Bitmap (android.graphics.Bitmap)15 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)14 DecoderResult (com.google.zxing.common.DecoderResult)12 WriterException (com.google.zxing.WriterException)10 DetectorResult (com.google.zxing.common.DetectorResult)10 MultiFormatWriter (com.google.zxing.MultiFormatWriter)9 Hashtable (java.util.Hashtable)9 AztecDetectorResult (com.google.zxing.aztec.AztecDetectorResult)8 EnumMap (java.util.EnumMap)8 Result (com.google.zxing.Result)7 ArrayList (java.util.ArrayList)6 Point (com.google.zxing.aztec.detector.Detector.Point)5 SymbolShapeHint (com.google.zxing.datamatrix.encoder.SymbolShapeHint)5 NotFoundException (com.google.zxing.NotFoundException)4 BitArray (com.google.zxing.common.BitArray)4 FinderPatternInfo (com.google.zxing.qrcode.detector.FinderPatternInfo)4