Search in sources :

Example 56 with BitMatrix

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

the class Decoder method decode.

/**
   * <p>Convenience method that can decode a QR Code represented as a 2D array of booleans.
   * "true" is taken to mean a black module.</p>
   *
   * @param image booleans representing white/black QR Code modules
   * @param hints decoding hints that should be used to influence decoding
   * @return text and bytes encoded within the QR Code
   * @throws FormatException if the QR Code cannot be decoded
   * @throws ChecksumException if error correction fails
   */
public DecoderResult decode(boolean[][] image, Map<DecodeHintType, ?> hints) throws ChecksumException, FormatException {
    int dimension = image.length;
    BitMatrix bits = new BitMatrix(dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j < dimension; j++) {
            if (image[i][j]) {
                bits.set(j, i);
            }
        }
    }
    return decode(bits, hints);
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 57 with BitMatrix

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

the class Version method buildFunctionPattern.

/**
   * See ISO 18004:2006 Annex E
   */
BitMatrix buildFunctionPattern() {
    int dimension = getDimensionForVersion();
    BitMatrix bitMatrix = new BitMatrix(dimension);
    // Top left finder pattern + separator + format
    bitMatrix.setRegion(0, 0, 9, 9);
    // Top right finder pattern + separator + format
    bitMatrix.setRegion(dimension - 8, 0, 8, 9);
    // Bottom left finder pattern + separator + format
    bitMatrix.setRegion(0, dimension - 8, 9, 8);
    // Alignment patterns
    int max = alignmentPatternCenters.length;
    for (int x = 0; x < max; x++) {
        int i = alignmentPatternCenters[x] - 2;
        for (int y = 0; y < max; y++) {
            if ((x == 0 && (y == 0 || y == max - 1)) || (x == max - 1 && y == 0)) {
                // No alignment patterns near the three finder paterns
                continue;
            }
            bitMatrix.setRegion(alignmentPatternCenters[y] - 2, i, 5, 5);
        }
    }
    // Vertical timing pattern
    bitMatrix.setRegion(6, 9, 1, dimension - 17);
    // Horizontal timing pattern
    bitMatrix.setRegion(9, 6, dimension - 17, 1);
    if (versionNumber > 6) {
        // Version info, top right
        bitMatrix.setRegion(dimension - 11, 0, 3, 6);
        // Version info, bottom left
        bitMatrix.setRegion(0, dimension - 11, 6, 3);
    }
    return bitMatrix;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 58 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 59 with BitMatrix

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

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

the class MultiFinderPatternFinder method findMulti.

public FinderPatternInfo[] findMulti(Map<DecodeHintType, ?> hints) throws NotFoundException {
    boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
    boolean pureBarcode = hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE);
    BitMatrix image = getImage();
    int maxI = image.getHeight();
    int maxJ = image.getWidth();
    // We are looking for black/white/black/white/black modules in
    // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far
    // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the
    // image, and then account for the center being 3 modules in size. This gives the smallest
    // number of pixels the center could be, so skip this often. When trying harder, look for all
    // QR versions regardless of how dense they are.
    int iSkip = (int) (maxI / (MAX_MODULES * 4.0f) * 3);
    if (iSkip < MIN_SKIP || tryHarder) {
        iSkip = MIN_SKIP;
    }
    int[] stateCount = new int[5];
    for (int i = iSkip - 1; i < maxI; i += iSkip) {
        // Get a row of black/white values
        stateCount[0] = 0;
        stateCount[1] = 0;
        stateCount[2] = 0;
        stateCount[3] = 0;
        stateCount[4] = 0;
        int currentState = 0;
        for (int j = 0; j < maxJ; j++) {
            if (image.get(j, i)) {
                // Black pixel
                if ((currentState & 1) == 1) {
                    // Counting white pixels
                    currentState++;
                }
                stateCount[currentState]++;
            } else {
                // White pixel
                if ((currentState & 1) == 0) {
                    // Counting black pixels
                    if (currentState == 4) {
                        // A winner?
                        if (foundPatternCross(stateCount) && handlePossibleCenter(stateCount, i, j, pureBarcode)) {
                            // Yes
                            // Clear state to start looking again
                            currentState = 0;
                            stateCount[0] = 0;
                            stateCount[1] = 0;
                            stateCount[2] = 0;
                            stateCount[3] = 0;
                            stateCount[4] = 0;
                        } else {
                            // No, shift counts back by two
                            stateCount[0] = stateCount[2];
                            stateCount[1] = stateCount[3];
                            stateCount[2] = stateCount[4];
                            stateCount[3] = 1;
                            stateCount[4] = 0;
                            currentState = 3;
                        }
                    } else {
                        stateCount[++currentState]++;
                    }
                } else {
                    // Counting white pixels
                    stateCount[currentState]++;
                }
            }
        }
        if (foundPatternCross(stateCount)) {
            handlePossibleCenter(stateCount, i, maxJ, pureBarcode);
        }
    // end if foundPatternCross
    }
    // for i=iSkip-1 ...
    FinderPattern[][] patternInfo = selectMutipleBestPatterns();
    List<FinderPatternInfo> result = new ArrayList<>();
    for (FinderPattern[] pattern : patternInfo) {
        ResultPoint.orderBestPatterns(pattern);
        result.add(new FinderPatternInfo(pattern));
    }
    if (result.isEmpty()) {
        return EMPTY_RESULT_ARRAY;
    } else {
        return result.toArray(new FinderPatternInfo[result.size()]);
    }
}
Also used : FinderPattern(com.google.zxing.qrcode.detector.FinderPattern) ArrayList(java.util.ArrayList) BitMatrix(com.google.zxing.common.BitMatrix) FinderPatternInfo(com.google.zxing.qrcode.detector.FinderPatternInfo) ResultPoint(com.google.zxing.ResultPoint)

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