Search in sources :

Example 1 with BufferedImageLuminanceSource

use of com.google.zxing.BufferedImageLuminanceSource in project zxing by zxing.

the class RSSExpandedInternalTestCase method testFindFinderPatterns.

@Test
public void testFindFinderPatterns() throws Exception {
    BufferedImage image = readImage("2.png");
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    List<ExpandedPair> previousPairs = new ArrayList<>();
    RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
    ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
    previousPairs.add(pair1);
    FinderPattern finderPattern = pair1.getFinderPattern();
    assertNotNull(finderPattern);
    assertEquals(0, finderPattern.getValue());
    ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
    previousPairs.add(pair2);
    finderPattern = pair2.getFinderPattern();
    assertNotNull(finderPattern);
    assertEquals(1, finderPattern.getValue());
    ExpandedPair pair3 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
    previousPairs.add(pair3);
    finderPattern = pair3.getFinderPattern();
    assertNotNull(finderPattern);
    assertEquals(1, finderPattern.getValue());
    try {
        rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
        //   the previous was the last pair
        fail(NotFoundException.class.getName() + " expected");
    } catch (NotFoundException nfe) {
    // ok
    }
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) FinderPattern(com.google.zxing.oned.rss.FinderPattern) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) ArrayList(java.util.ArrayList) NotFoundException(com.google.zxing.NotFoundException) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 2 with BufferedImageLuminanceSource

use of com.google.zxing.BufferedImageLuminanceSource in project zxing by zxing.

the class AbstractNegativeBlackBoxTestCase method checkForFalsePositives.

/**
   * Make sure ZXing does NOT find a barcode in the image.
   *
   * @param image The image to test
   * @param rotationInDegrees The amount of rotation to apply
   * @return true if nothing found, false if a non-existent barcode was detected
   */
private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
    BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
    LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    try {
        result = getReader().decode(bitmap);
        log.info(String.format("Found false positive: '%s' with format '%s' (rotation: %d)", result.getText(), result.getBarcodeFormat(), (int) rotationInDegrees));
        return false;
    } catch (ReaderException re) {
    // continue
    }
    // Try "try harder" getMode
    Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    try {
        result = getReader().decode(bitmap, hints);
        log.info(String.format("Try harder found false positive: '%s' with format '%s' (rotation: %d)", result.getText(), result.getBarcodeFormat(), (int) rotationInDegrees));
        return false;
    } catch (ReaderException re) {
    // continue
    }
    return true;
}
Also used : LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) DecodeHintType(com.google.zxing.DecodeHintType) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) EnumMap(java.util.EnumMap) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 3 with BufferedImageLuminanceSource

use of com.google.zxing.BufferedImageLuminanceSource in project zxing by zxing.

the class RSSExpandedImage2binaryTestCase method assertCorrectImage2binary.

private static void assertCorrectImage2binary(String fileName, String expected) throws IOException, NotFoundException {
    Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
    BufferedImage image = ImageIO.read(path.toFile());
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    List<ExpandedPair> pairs;
    try {
        RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
        pairs = rssExpandedReader.decodeRow2pairs(rowNumber, row);
    } catch (ReaderException re) {
        fail(re.toString());
        return;
    }
    BitArray binary = BitArrayBuilder.buildBitArray(pairs);
    assertEquals(expected, binary.toString());
}
Also used : Path(java.nio.file.Path) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) ReaderException(com.google.zxing.ReaderException)

Example 4 with BufferedImageLuminanceSource

use of com.google.zxing.BufferedImageLuminanceSource in project zxing by zxing.

the class RSSExpandedImage2resultTestCase method assertCorrectImage2result.

private static void assertCorrectImage2result(String fileName, ExpandedProductParsedResult expected) throws IOException, NotFoundException {
    Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
    BufferedImage image = ImageIO.read(path.toFile());
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    Result theResult;
    try {
        RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
        theResult = rssExpandedReader.decodeRow(rowNumber, row, null);
    } catch (ReaderException re) {
        fail(re.toString());
        return;
    }
    assertSame(BarcodeFormat.RSS_EXPANDED, theResult.getBarcodeFormat());
    ParsedResult result = ResultParser.parseResult(theResult);
    assertEquals(expected, result);
}
Also used : Path(java.nio.file.Path) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) ParsedResult(com.google.zxing.client.result.ParsedResult) ExpandedProductParsedResult(com.google.zxing.client.result.ExpandedProductParsedResult) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) ExpandedProductParsedResult(com.google.zxing.client.result.ExpandedProductParsedResult) ReaderException(com.google.zxing.ReaderException)

Example 5 with BufferedImageLuminanceSource

use of com.google.zxing.BufferedImageLuminanceSource in project zxing by zxing.

the class RSSExpandedImage2stringTestCase method assertCorrectImage2string.

private static void assertCorrectImage2string(String fileName, String expected) throws IOException, NotFoundException {
    Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
    BufferedImage image = ImageIO.read(path.toFile());
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    Result result;
    try {
        RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
        result = rssExpandedReader.decodeRow(rowNumber, row, null);
    } catch (ReaderException re) {
        fail(re.toString());
        return;
    }
    assertSame(BarcodeFormat.RSS_EXPANDED, result.getBarcodeFormat());
    assertEquals(expected, result.getText());
}
Also used : Path(java.nio.file.Path) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)13 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 BufferedImage (java.awt.image.BufferedImage)13 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)8 BitArray (com.google.zxing.common.BitArray)7 Path (java.nio.file.Path)7 ReaderException (com.google.zxing.ReaderException)6 Result (com.google.zxing.Result)6 Test (org.junit.Test)6 LuminanceSource (com.google.zxing.LuminanceSource)5 FinderPattern (com.google.zxing.oned.rss.FinderPattern)4 HybridBinarizer (com.google.zxing.common.HybridBinarizer)3 ArrayList (java.util.ArrayList)3 DataCharacter (com.google.zxing.oned.rss.DataCharacter)2 DecodeHintType (com.google.zxing.DecodeHintType)1 MultiFormatReader (com.google.zxing.MultiFormatReader)1 NotFoundException (com.google.zxing.NotFoundException)1 ResultMetadataType (com.google.zxing.ResultMetadataType)1 ExpandedProductParsedResult (com.google.zxing.client.result.ExpandedProductParsedResult)1 ParsedResult (com.google.zxing.client.result.ParsedResult)1