Search in sources :

Example 36 with BitArray

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

the class OneDReader method doDecode.

/**
   * We're going to examine rows from the middle outward, searching alternately above and below the
   * middle, and farther out each time. rowStep is the number of rows between each successive
   * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
   * middle + rowStep, then middle - (2 * rowStep), etc.
   * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
   * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
   * image if "trying harder".
   *
   * @param image The image to decode
   * @param hints Any hints that were requested
   * @return The contents of the decoded barcode
   * @throws NotFoundException Any spontaneous errors which occur
   */
private Result doDecode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException {
    int width = image.getWidth();
    int height = image.getHeight();
    BitArray row = new BitArray(width);
    int middle = height >> 1;
    boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
    int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
    int maxLines;
    if (tryHarder) {
        // Look at the whole image, not just the center
        maxLines = height;
    } else {
        // 15 rows spaced 1/32 apart is roughly the middle half of the image
        maxLines = 15;
    }
    for (int x = 0; x < maxLines; x++) {
        // Scanning from the middle out. Determine which row we're looking at next:
        int rowStepsAboveOrBelow = (x + 1) / 2;
        // i.e. is x even?
        boolean isAbove = (x & 0x01) == 0;
        int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
        if (rowNumber < 0 || rowNumber >= height) {
            // Oops, if we run off the top or bottom, stop
            break;
        }
        // Estimate black point for this row and load it:
        try {
            row = image.getBlackRow(rowNumber, row);
        } catch (NotFoundException ignored) {
            continue;
        }
        // handle decoding upside down barcodes.
        for (int attempt = 0; attempt < 2; attempt++) {
            if (attempt == 1) {
                // trying again?
                // reverse the row and continue
                row.reverse();
                // that start on the center line.
                if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
                    Map<DecodeHintType, Object> newHints = new EnumMap<>(DecodeHintType.class);
                    newHints.putAll(hints);
                    newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
                    hints = newHints;
                }
            }
            try {
                // Look for a barcode
                Result result = decodeRow(rowNumber, row, hints);
                // We found our barcode
                if (attempt == 1) {
                    // But it was upside down, so note that
                    result.putMetadata(ResultMetadataType.ORIENTATION, 180);
                    // And remember to flip the result points horizontally.
                    ResultPoint[] points = result.getResultPoints();
                    if (points != null) {
                        points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
                        points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
                    }
                }
                return result;
            } catch (ReaderException re) {
            // continue -- just couldn't decode this row
            }
        }
    }
    throw NotFoundException.getNotFoundInstance();
}
Also used : ResultPoint(com.google.zxing.ResultPoint) DecodeHintType(com.google.zxing.DecodeHintType) NotFoundException(com.google.zxing.NotFoundException) BitArray(com.google.zxing.common.BitArray) EnumMap(java.util.EnumMap) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 37 with BitArray

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

the class State method toBitArray.

BitArray toBitArray(byte[] text) {
    // Reverse the tokens, so that they are in the order that they should
    // be output
    Deque<Token> symbols = new LinkedList<>();
    for (Token token = endBinaryShift(text.length).token; token != null; token = token.getPrevious()) {
        symbols.addFirst(token);
    }
    BitArray bitArray = new BitArray();
    // Add each token to the result.
    for (Token symbol : symbols) {
        symbol.appendTo(bitArray, text);
    }
    //assert bitArray.getSize() == this.bitCount;
    return bitArray;
}
Also used : BitArray(com.google.zxing.common.BitArray) LinkedList(java.util.LinkedList)

Example 38 with BitArray

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

the class DecodeWorker method dumpBlackPoint.

/**
   * Writes out a single PNG which is three times the width of the input image, containing from left
   * to right: the original image, the row sampling monochrome version, and the 2D sampling
   * monochrome version.
   */
private static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) throws IOException {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int stride = width * 3;
    int[] pixels = new int[stride * height];
    // The original image
    int[] argb = new int[width];
    for (int y = 0; y < height; y++) {
        image.getRGB(0, y, width, 1, argb, 0, width);
        System.arraycopy(argb, 0, pixels, y * stride, width);
    }
    // Row sampling
    BitArray row = new BitArray(width);
    for (int y = 0; y < height; y++) {
        try {
            row = bitmap.getBlackRow(y, row);
        } catch (NotFoundException nfe) {
            // If fetching the row failed, draw a red line and keep going.
            int offset = y * stride + width;
            Arrays.fill(pixels, offset, offset + width, RED);
            continue;
        }
        int offset = y * stride + width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = row.get(x) ? BLACK : WHITE;
        }
    }
    // 2D sampling
    try {
        for (int y = 0; y < height; y++) {
            BitMatrix matrix = bitmap.getBlackMatrix();
            int offset = y * stride + width * 2;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
            }
        }
    } catch (NotFoundException ignored) {
    // continue
    }
    writeResultImage(stride, height, pixels, uri, ".mono.png");
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BitArray(com.google.zxing.common.BitArray) BitMatrix(com.google.zxing.common.BitMatrix) ResultPoint(com.google.zxing.ResultPoint)

Example 39 with BitArray

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

the class RSSExpandedInternalTestCase method testDecodeDataCharacter.

@Test
public void testDecodeDataCharacter() throws Exception {
    BufferedImage image = readImage("3.png");
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null);
    //image pixels where the A1 pattern starts (at 124) and ends (at 214)
    int[] startEnd = { 145, 243 };
    // A
    int value = 0;
    FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2);
    //{1, 8, 4, 1, 1};
    RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
    DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, false);
    assertEquals(19, dataCharacter.getValue());
    assertEquals(1007, dataCharacter.getChecksumPortion());
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) FinderPattern(com.google.zxing.oned.rss.FinderPattern) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) DataCharacter(com.google.zxing.oned.rss.DataCharacter) Test(org.junit.Test)

Example 40 with BitArray

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

the class AbstractDecoderTest method assertCorrectBinaryString.

static void assertCorrectBinaryString(CharSequence binaryString, String expectedNumber) throws NotFoundException, FormatException {
    BitArray binary = BinaryUtil.buildBitArrayFromStringWithoutSpaces(binaryString);
    AbstractExpandedDecoder decoder = AbstractExpandedDecoder.createDecoder(binary);
    String result = decoder.parseInformation();
    assertEquals(expectedNumber, result);
}
Also used : BitArray(com.google.zxing.common.BitArray)

Aggregations

BitArray (com.google.zxing.common.BitArray)68 Test (org.junit.Test)28 BinaryBitmap (com.google.zxing.BinaryBitmap)8 Result (com.google.zxing.Result)8 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)7 WriterException (com.google.zxing.WriterException)7 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)7 BufferedImage (java.awt.image.BufferedImage)7 ResultPoint (com.google.zxing.ResultPoint)6 NotFoundException (com.google.zxing.NotFoundException)5 ReaderException (com.google.zxing.ReaderException)5 BitMatrix (com.google.zxing.common.BitMatrix)4 FinderPattern (com.google.zxing.oned.rss.FinderPattern)4 ArrayList (java.util.ArrayList)4 AbstractExpandedDecoder (com.google.zxing.oned.rss.expanded.decoders.AbstractExpandedDecoder)3 Path (java.nio.file.Path)3 DecodeHintType (com.google.zxing.DecodeHintType)2 CharacterSetECI (com.google.zxing.common.CharacterSetECI)2 ReedSolomonEncoder (com.google.zxing.common.reedsolomon.ReedSolomonEncoder)2 DataCharacter (com.google.zxing.oned.rss.DataCharacter)2