Search in sources :

Example 6 with HybridBinarizer

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

the class DecodeHandler method decode.

/**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data   The YUV preview frame.
   * @param width  The width of the preview frame.
   * @param height The height of the preview frame.
   */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    //    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width;
    width = height;
    height = tmp;
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = activity.getHandler();
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode in " + (end - start) + " ms");
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) Handler(android.os.Handler) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 7 with HybridBinarizer

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

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

Example 9 with HybridBinarizer

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

the class BenchmarkAsyncTask method decode.

private static BenchmarkItem decode(MultiFormatReader reader, File file) {
    Bitmap imageBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    if (imageBitmap == null) {
        Log.e(TAG, "Couldn't open " + file);
        return null;
    }
    int width = imageBitmap.getWidth();
    int height = imageBitmap.getHeight();
    int[] pixels = new int[width * height];
    imageBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
    BenchmarkItem item = new BenchmarkItem(file, RUNS);
    for (int x = 0; x < RUNS; x++) {
        boolean success;
        Result result = null;
        // Using this call instead of getting the time should eliminate a lot of variability due to
        // scheduling and what else is happening in the system.
        long now = Debug.threadCpuTimeNanos();
        try {
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            result = reader.decodeWithState(bitmap);
            success = true;
        } catch (ReaderException ignored) {
            success = false;
        }
        now = Debug.threadCpuTimeNanos() - now;
        if (x == 0) {
            item.setDecoded(success);
            item.setFormat(result != null ? result.getBarcodeFormat() : null);
        }
        item.addResult((int) (now / 1000));
    }
    return item;
}
Also used : Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 10 with HybridBinarizer

use of com.google.zxing.common.HybridBinarizer in project BGAQRCode-Android by bingoogolapple.

the class ZXingView method processData.

@Override
public String processData(byte[] data, int width, int height, boolean isRetry) {
    String result = null;
    Result rawResult = null;
    try {
        PlanarYUVLuminanceSource source = null;
        Rect rect = mScanBoxView.getScanBoxAreaRect(height);
        if (rect != null) {
            source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
        } else {
            source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
        }
        rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(source)));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        mMultiFormatReader.reset();
    }
    if (rawResult != null) {
        result = rawResult.getText();
    }
    return result;
}
Also used : Rect(android.graphics.Rect) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)30 HybridBinarizer (com.google.zxing.common.HybridBinarizer)30 Result (com.google.zxing.Result)29 ReaderException (com.google.zxing.ReaderException)21 Bundle (android.os.Bundle)13 Message (android.os.Message)13 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)11 MultiFormatReader (com.google.zxing.MultiFormatReader)10 Handler (android.os.Handler)8 LuminanceSource (com.google.zxing.LuminanceSource)6 BufferedImage (java.awt.image.BufferedImage)5 Bitmap (android.graphics.Bitmap)3 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)3 ChecksumException (com.google.zxing.ChecksumException)3 FormatException (com.google.zxing.FormatException)3 NotFoundException (com.google.zxing.NotFoundException)3 RGBLuminanceSource (com.google.zxing.RGBLuminanceSource)3 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)3 MultipleBarcodeReader (com.google.zxing.multi.MultipleBarcodeReader)3 Path (java.nio.file.Path)3