Search in sources :

Example 1 with LuminanceSource

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

the class GlobalHistogramBinarizer method getBlackRow.

// Applies simple sharpening to the row data to improve performance of the 1D Readers.
@Override
public BitArray getBlackRow(int y, BitArray row) throws NotFoundException {
    LuminanceSource source = getLuminanceSource();
    int width = source.getWidth();
    if (row == null || row.getSize() < width) {
        row = new BitArray(width);
    } else {
        row.clear();
    }
    initArrays(width);
    byte[] localLuminances = source.getRow(y, luminances);
    int[] localBuckets = buckets;
    for (int x = 0; x < width; x++) {
        int pixel = localLuminances[x] & 0xff;
        localBuckets[pixel >> LUMINANCE_SHIFT]++;
    }
    int blackPoint = estimateBlackPoint(localBuckets);
    int left = localLuminances[0] & 0xff;
    int center = localLuminances[1] & 0xff;
    for (int x = 1; x < width - 1; x++) {
        int right = localLuminances[x + 1] & 0xff;
        // A simple -1 4 -1 box filter with a weight of 2.
        int luminance = ((center * 4) - left - right) / 2;
        if (luminance < blackPoint) {
            row.set(x);
        }
        left = center;
        center = right;
    }
    return row;
}
Also used : LuminanceSource(com.google.zxing.LuminanceSource)

Example 2 with LuminanceSource

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

the class GlobalHistogramBinarizer method getBlackMatrix.

// Does not sharpen the data, as this call is intended to only be used by 2D Readers.
@Override
public BitMatrix getBlackMatrix() throws NotFoundException {
    LuminanceSource source = getLuminanceSource();
    int width = source.getWidth();
    int height = source.getHeight();
    BitMatrix matrix = new BitMatrix(width, height);
    // Quickly calculates the histogram by sampling four rows from the image. This proved to be
    // more robust on the blackbox tests than sampling a diagonal as we used to do.
    initArrays(width);
    int[] localBuckets = buckets;
    for (int y = 1; y < 5; y++) {
        int row = height * y / 5;
        byte[] localLuminances = source.getRow(row, luminances);
        int right = (width * 4) / 5;
        for (int x = width / 5; x < right; x++) {
            int pixel = localLuminances[x] & 0xff;
            localBuckets[pixel >> LUMINANCE_SHIFT]++;
        }
    }
    int blackPoint = estimateBlackPoint(localBuckets);
    // We delay reading the entire image luminance until the black point estimation succeeds.
    // Although we end up reading four rows twice, it is consistent with our motto of
    // "fail quickly" which is necessary for continuous scanning.
    byte[] localLuminances = source.getMatrix();
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            int pixel = localLuminances[offset + x] & 0xff;
            if (pixel < blackPoint) {
                matrix.set(x, y);
            }
        }
    }
    return matrix;
}
Also used : LuminanceSource(com.google.zxing.LuminanceSource)

Example 3 with LuminanceSource

use of com.google.zxing.LuminanceSource 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 4 with LuminanceSource

use of com.google.zxing.LuminanceSource 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 5 with LuminanceSource

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

the class MultiTestCase method testMulti.

@Test
public void testMulti() throws Exception {
    // Very basic test for now
    Path testBase = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/multi-1");
    Path testImage = testBase.resolve("1.png");
    BufferedImage image = ImageIO.read(testImage.toFile());
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(new MultiFormatReader());
    Result[] results = reader.decodeMultiple(bitmap);
    assertNotNull(results);
    assertEquals(2, results.length);
    assertEquals("031415926531", results[0].getText());
    assertEquals(BarcodeFormat.UPC_A, results[0].getBarcodeFormat());
    assertEquals("www.airtable.com/jobs", results[1].getText());
    assertEquals(BarcodeFormat.QR_CODE, results[1].getBarcodeFormat());
}
Also used : Path(java.nio.file.Path) MultiFormatReader(com.google.zxing.MultiFormatReader) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) Test(org.junit.Test)

Aggregations

LuminanceSource (com.google.zxing.LuminanceSource)15 BinaryBitmap (com.google.zxing.BinaryBitmap)8 Result (com.google.zxing.Result)8 BufferedImage (java.awt.image.BufferedImage)7 HybridBinarizer (com.google.zxing.common.HybridBinarizer)6 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)5 ReaderException (com.google.zxing.ReaderException)5 MultiFormatReader (com.google.zxing.MultiFormatReader)4 Path (java.nio.file.Path)4 MultipleBarcodeReader (com.google.zxing.multi.MultipleBarcodeReader)3 ResultPoint (com.google.zxing.ResultPoint)2 GenericMultipleBarcodeReader (com.google.zxing.multi.GenericMultipleBarcodeReader)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Bundle (android.os.Bundle)1 Message (android.os.Message)1 ChecksumException (com.google.zxing.ChecksumException)1 DecodeHintType (com.google.zxing.DecodeHintType)1 FormatException (com.google.zxing.FormatException)1 NotFoundException (com.google.zxing.NotFoundException)1