Search in sources :

Example 1 with GlobalHistogramBinarizer

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

the class DecodeServlet method processImage.

private static void processImage(BufferedImage image, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
    Collection<Result> results = new ArrayList<>(1);
    try {
        Reader reader = new MultiFormatReader();
        ReaderException savedException = null;
        try {
            // Look for multiple barcodes
            MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
            Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
            if (theResults != null) {
                results.addAll(Arrays.asList(theResults));
            }
        } catch (ReaderException re) {
            savedException = re;
        }
        if (results.isEmpty()) {
            try {
                // Look for pure barcode
                Result theResult = reader.decode(bitmap, HINTS_PURE);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                // Look for normal barcode in photo
                Result theResult = reader.decode(bitmap, HINTS);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                // Try again with other binarizer
                BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
                Result theResult = reader.decode(hybridBitmap, HINTS);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;
            } catch (FormatException | ChecksumException e) {
                log.info(e.toString());
                errorResponse(request, response, "format");
            } catch (ReaderException e) {
                // Including NotFoundException
                log.info(e.toString());
                errorResponse(request, response, "notfound");
            }
            return;
        }
    } catch (RuntimeException re) {
        // Call out unexpected errors in the log clearly
        log.log(Level.WARNING, "Unexpected exception from library", re);
        throw new ServletException(re);
    }
    String fullParameter = request.getParameter("full");
    boolean minimalOutput = fullParameter != null && !Boolean.parseBoolean(fullParameter);
    if (minimalOutput) {
        response.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try (Writer out = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) {
            for (Result result : results) {
                out.write(result.getText());
                out.write('\n');
            }
        }
    } else {
        request.setAttribute("results", results);
        request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
    }
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) MultiFormatReader(com.google.zxing.MultiFormatReader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) ChecksumException(com.google.zxing.ChecksumException) ArrayList(java.util.ArrayList) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) ImageReader(com.google.zxing.client.j2se.ImageReader) MultiFormatReader(com.google.zxing.MultiFormatReader) Reader(com.google.zxing.Reader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException) ServletException(javax.servlet.ServletException) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) OutputStreamWriter(java.io.OutputStreamWriter) BinaryBitmap(com.google.zxing.BinaryBitmap) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with GlobalHistogramBinarizer

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

use of com.google.zxing.common.GlobalHistogramBinarizer 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 GlobalHistogramBinarizer

use of com.google.zxing.common.GlobalHistogramBinarizer 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 GlobalHistogramBinarizer

use of com.google.zxing.common.GlobalHistogramBinarizer 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

GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)12 BinaryBitmap (com.google.zxing.BinaryBitmap)11 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)8 BufferedImage (java.awt.image.BufferedImage)8 BitArray (com.google.zxing.common.BitArray)7 Result (com.google.zxing.Result)6 ReaderException (com.google.zxing.ReaderException)4 HybridBinarizer (com.google.zxing.common.HybridBinarizer)4 FinderPattern (com.google.zxing.oned.rss.FinderPattern)4 Test (org.junit.Test)4 MultiFormatReader (com.google.zxing.MultiFormatReader)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 LuminanceSource (com.google.zxing.LuminanceSource)2 DataCharacter (com.google.zxing.oned.rss.DataCharacter)2 PointF (android.graphics.PointF)1 Rect (android.graphics.Rect)1 ScanResult (cn.bingoogolapple.qrcode.core.ScanResult)1 BarcodeFormat (com.google.zxing.BarcodeFormat)1 ChecksumException (com.google.zxing.ChecksumException)1