Search in sources :

Example 36 with BitMatrix

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

the class MultiDetector method detectMulti.

public DetectorResult[] detectMulti(Map<DecodeHintType, ?> hints) throws NotFoundException {
    BitMatrix image = getImage();
    ResultPointCallback resultPointCallback = hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
    MultiFinderPatternFinder finder = new MultiFinderPatternFinder(image, resultPointCallback);
    FinderPatternInfo[] infos = finder.findMulti(hints);
    if (infos.length == 0) {
        throw NotFoundException.getNotFoundInstance();
    }
    List<DetectorResult> result = new ArrayList<>();
    for (FinderPatternInfo info : infos) {
        try {
            result.add(processFinderPatternInfo(info));
        } catch (ReaderException e) {
        // ignore
        }
    }
    if (result.isEmpty()) {
        return EMPTY_DETECTOR_RESULTS;
    } else {
        return result.toArray(new DetectorResult[result.size()]);
    }
}
Also used : ResultPointCallback(com.google.zxing.ResultPointCallback) ArrayList(java.util.ArrayList) DetectorResult(com.google.zxing.common.DetectorResult) BitMatrix(com.google.zxing.common.BitMatrix) FinderPatternInfo(com.google.zxing.qrcode.detector.FinderPatternInfo) ReaderException(com.google.zxing.ReaderException)

Example 37 with BitMatrix

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

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)

Example 38 with BitMatrix

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

the class OneDimensionalCodeWriter method renderResult.

/**
   * @return a byte array of horizontal pixels (0 = white, 1 = black)
   */
private static BitMatrix renderResult(boolean[] code, int width, int height, int sidesMargin) {
    int inputWidth = code.length;
    // Add quiet zone on both sides.
    int fullWidth = inputWidth + sidesMargin;
    int outputWidth = Math.max(width, fullWidth);
    int outputHeight = Math.max(1, height);
    int multiple = outputWidth / fullWidth;
    int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
    BitMatrix output = new BitMatrix(outputWidth, outputHeight);
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
        if (code[inputX]) {
            output.setRegion(outputX, 0, multiple, outputHeight);
        }
    }
    return output;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 39 with BitMatrix

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

the class Detector method detect.

/**
   * <p>Detects a PDF417 Code in an image. Only checks 0 and 180 degree rotations.</p>
   *
   * @param image barcode image to decode
   * @param hints optional hints to detector
   * @param multiple if true, then the image is searched for multiple codes. If false, then at most one code will
   * be found and returned
   * @return {@link PDF417DetectorResult} encapsulating results of detecting a PDF417 code
   * @throws NotFoundException if no PDF417 Code can be found
   */
public static PDF417DetectorResult detect(BinaryBitmap image, Map<DecodeHintType, ?> hints, boolean multiple) throws NotFoundException {
    // TODO detection improvement, tryHarder could try several different luminance thresholds/blackpoints or even 
    // different binarizers
    //boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
    BitMatrix bitMatrix = image.getBlackMatrix();
    List<ResultPoint[]> barcodeCoordinates = detect(multiple, bitMatrix);
    if (barcodeCoordinates.isEmpty()) {
        bitMatrix = bitMatrix.clone();
        bitMatrix.rotate180();
        barcodeCoordinates = detect(multiple, bitMatrix);
    }
    return new PDF417DetectorResult(bitMatrix, barcodeCoordinates);
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 40 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)

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