Search in sources :

Example 41 with BinaryBitmap

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

the class DecodeWorker method decode.

private Result[] decode(URI uri, Map<DecodeHintType, ?> hints) throws IOException {
    BufferedImage image = ImageReader.readImage(uri);
    LuminanceSource source;
    if (config.crop == null) {
        source = new BufferedImageLuminanceSource(image);
    } else {
        List<Integer> crop = config.crop;
        source = new BufferedImageLuminanceSource(image, crop.get(0), crop.get(1), crop.get(2), crop.get(3));
    }
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    if (config.dumpBlackPoint) {
        dumpBlackPoint(uri, image, bitmap);
    }
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    Result[] results;
    try {
        if (config.multi) {
            MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(multiFormatReader);
            results = reader.decodeMultiple(bitmap, hints);
        } else {
            results = new Result[] { multiFormatReader.decode(bitmap, hints) };
        }
    } catch (NotFoundException ignored) {
        System.out.println(uri + ": No barcode found");
        return null;
    }
    if (config.brief) {
        System.out.println(uri + ": Success");
    } else {
        StringWriter output = new StringWriter();
        for (Result result : results) {
            ParsedResult parsedResult = ResultParser.parseResult(result);
            output.write(uri + " (format: " + result.getBarcodeFormat() + ", type: " + parsedResult.getType() + "):\n" + "Raw result:\n" + result.getText() + "\n" + "Parsed result:\n" + parsedResult.getDisplayResult() + "\n");
            output.write("Found " + result.getResultPoints().length + " result points.\n");
            for (int pointIndex = 0; pointIndex < result.getResultPoints().length; pointIndex++) {
                ResultPoint rp = result.getResultPoints()[pointIndex];
                output.write("  Point " + pointIndex + ": (" + rp.getX() + ',' + rp.getY() + ')');
                if (pointIndex != result.getResultPoints().length - 1) {
                    output.write('\n');
                }
            }
            output.write('\n');
        }
        System.out.println(output);
    }
    return results;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) ResultPoint(com.google.zxing.ResultPoint) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) StringWriter(java.io.StringWriter) LuminanceSource(com.google.zxing.LuminanceSource) ParsedResult(com.google.zxing.client.result.ParsedResult) BinaryBitmap(com.google.zxing.BinaryBitmap)

Example 42 with BinaryBitmap

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

the class GUIRunner method getDecodeText.

private static String getDecodeText(Path file) {
    BufferedImage image;
    try {
        image = ImageReader.readImage(file.toUri());
    } catch (IOException ioe) {
        return ioe.toString();
    }
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    try {
        result = new MultiFormatReader().decode(bitmap);
    } catch (ReaderException re) {
        return re.toString();
    }
    return String.valueOf(result.getText());
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) LuminanceSource(com.google.zxing.LuminanceSource) IOException(java.io.IOException) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 43 with BinaryBitmap

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

the class AbstractBlackBoxTestCase method testBlackBoxCountingResults.

private void testBlackBoxCountingResults(boolean assertOnFailure) throws IOException {
    assertFalse(testResults.isEmpty());
    List<Path> imageFiles = getImageFiles();
    int testCount = testResults.size();
    int[] passedCounts = new int[testCount];
    int[] misreadCounts = new int[testCount];
    int[] tryHarderCounts = new int[testCount];
    int[] tryHarderMisreadCounts = new int[testCount];
    for (Path testImage : imageFiles) {
        log.info(String.format("Starting %s", testImage));
        BufferedImage image = ImageIO.read(testImage.toFile());
        String testImageFileName = testImage.getFileName().toString();
        String fileBaseName = testImageFileName.substring(0, testImageFileName.indexOf('.'));
        Path expectedTextFile = testBase.resolve(fileBaseName + ".txt");
        String expectedText;
        if (Files.exists(expectedTextFile)) {
            expectedText = readFileAsString(expectedTextFile, StandardCharsets.UTF_8);
        } else {
            expectedTextFile = testBase.resolve(fileBaseName + ".bin");
            assertTrue(Files.exists(expectedTextFile));
            expectedText = readFileAsString(expectedTextFile, StandardCharsets.ISO_8859_1);
        }
        Path expectedMetadataFile = testBase.resolve(fileBaseName + ".metadata.txt");
        Properties expectedMetadata = new Properties();
        if (Files.exists(expectedMetadataFile)) {
            try (BufferedReader reader = Files.newBufferedReader(expectedMetadataFile, StandardCharsets.UTF_8)) {
                expectedMetadata.load(reader);
            }
        }
        for (int x = 0; x < testCount; x++) {
            float rotation = testResults.get(x).getRotation();
            BufferedImage rotatedImage = rotateImage(image, rotation);
            LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            try {
                if (decode(bitmap, rotation, expectedText, expectedMetadata, false)) {
                    passedCounts[x]++;
                } else {
                    misreadCounts[x]++;
                }
            } catch (ReaderException ignored) {
                log.fine(String.format("could not read at rotation %f", rotation));
            }
            try {
                if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) {
                    tryHarderCounts[x]++;
                } else {
                    tryHarderMisreadCounts[x]++;
                }
            } catch (ReaderException ignored) {
                log.fine(String.format("could not read at rotation %f w/TH", rotation));
            }
        }
    }
    // Print the results of all tests first
    int totalFound = 0;
    int totalMustPass = 0;
    int totalMisread = 0;
    int totalMaxMisread = 0;
    for (int x = 0; x < testResults.size(); x++) {
        TestResult testResult = testResults.get(x);
        log.info(String.format("Rotation %d degrees:", (int) testResult.getRotation()));
        log.info(String.format(" %d of %d images passed (%d required)", passedCounts[x], imageFiles.size(), testResult.getMustPassCount()));
        int failed = imageFiles.size() - passedCounts[x];
        log.info(String.format(" %d failed due to misreads, %d not detected", misreadCounts[x], failed - misreadCounts[x]));
        log.info(String.format(" %d of %d images passed with try harder (%d required)", tryHarderCounts[x], imageFiles.size(), testResult.getTryHarderCount()));
        failed = imageFiles.size() - tryHarderCounts[x];
        log.info(String.format(" %d failed due to misreads, %d not detected", tryHarderMisreadCounts[x], failed - tryHarderMisreadCounts[x]));
        totalFound += passedCounts[x] + tryHarderCounts[x];
        totalMustPass += testResult.getMustPassCount() + testResult.getTryHarderCount();
        totalMisread += misreadCounts[x] + tryHarderMisreadCounts[x];
        totalMaxMisread += testResult.getMaxMisreads() + testResult.getMaxTryHarderMisreads();
    }
    int totalTests = imageFiles.size() * testCount * 2;
    log.info(String.format("Decoded %d images out of %d (%d%%, %d required)", totalFound, totalTests, totalFound * 100 / totalTests, totalMustPass));
    if (totalFound > totalMustPass) {
        log.warning(String.format("+++ Test too lax by %d images", totalFound - totalMustPass));
    } else if (totalFound < totalMustPass) {
        log.warning(String.format("--- Test failed by %d images", totalMustPass - totalFound));
    }
    if (totalMisread < totalMaxMisread) {
        log.warning(String.format("+++ Test expects too many misreads by %d images", totalMaxMisread - totalMisread));
    } else if (totalMisread > totalMaxMisread) {
        log.warning(String.format("--- Test had too many misreads by %d images", totalMisread - totalMaxMisread));
    }
    // Then run through again and assert if any failed
    if (assertOnFailure) {
        for (int x = 0; x < testCount; x++) {
            TestResult testResult = testResults.get(x);
            String label = "Rotation " + testResult.getRotation() + " degrees: Too many images failed";
            assertTrue(label, passedCounts[x] >= testResult.getMustPassCount());
            assertTrue("Try harder, " + label, tryHarderCounts[x] >= testResult.getTryHarderCount());
            label = "Rotation " + testResult.getRotation() + " degrees: Too many images misread";
            assertTrue(label, misreadCounts[x] <= testResult.getMaxMisreads());
            assertTrue("Try harder, " + label, tryHarderMisreadCounts[x] <= testResult.getMaxTryHarderMisreads());
        }
    }
}
Also used : Path(java.nio.file.Path) Properties(java.util.Properties) BufferedImage(java.awt.image.BufferedImage) ReaderException(com.google.zxing.ReaderException) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BufferedReader(java.io.BufferedReader) BinaryBitmap(com.google.zxing.BinaryBitmap)

Example 44 with BinaryBitmap

use of com.google.zxing.BinaryBitmap 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 45 with BinaryBitmap

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

the class TestCaseUtil method getBinaryBitmap.

static BinaryBitmap getBinaryBitmap(String path) throws IOException {
    BufferedImage bufferedImage = getBufferedImage(path);
    BufferedImageLuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);
    return new BinaryBitmap(new GlobalHistogramBinarizer(luminanceSource));
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)78 Result (com.google.zxing.Result)62 HybridBinarizer (com.google.zxing.common.HybridBinarizer)61 ReaderException (com.google.zxing.ReaderException)39 Message (android.os.Message)24 Bundle (android.os.Bundle)23 BufferedImage (java.awt.image.BufferedImage)21 MultiFormatReader (com.google.zxing.MultiFormatReader)20 NotFoundException (com.google.zxing.NotFoundException)20 LuminanceSource (com.google.zxing.LuminanceSource)19 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)19 DecodeHintType (com.google.zxing.DecodeHintType)15 Handler (android.os.Handler)14 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 Hashtable (java.util.Hashtable)11 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)10 Test (org.junit.Test)10 BitArray (com.google.zxing.common.BitArray)8 Bitmap (android.graphics.Bitmap)7