Search in sources :

Example 6 with LuminanceSource

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

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 7 with LuminanceSource

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

the class HybridBinarizer method getBlackMatrix.

/**
   * Calculates the final BitMatrix once for all requests. This could be called once from the
   * constructor instead, but there are some advantages to doing it lazily, such as making
   * profiling easier, and not doing heavy lifting when callers don't expect it.
   */
@Override
public BitMatrix getBlackMatrix() throws NotFoundException {
    if (matrix != null) {
        return matrix;
    }
    LuminanceSource source = getLuminanceSource();
    int width = source.getWidth();
    int height = source.getHeight();
    if (width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION) {
        byte[] luminances = source.getMatrix();
        int subWidth = width >> BLOCK_SIZE_POWER;
        if ((width & BLOCK_SIZE_MASK) != 0) {
            subWidth++;
        }
        int subHeight = height >> BLOCK_SIZE_POWER;
        if ((height & BLOCK_SIZE_MASK) != 0) {
            subHeight++;
        }
        int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
        BitMatrix newMatrix = new BitMatrix(width, height);
        calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix);
        matrix = newMatrix;
    } else {
        // If the image is too small, fall back to the global histogram approach.
        matrix = super.getBlackMatrix();
    }
    return matrix;
}
Also used : LuminanceSource(com.google.zxing.LuminanceSource)

Example 8 with LuminanceSource

use of com.google.zxing.LuminanceSource in project zxing-android-embedded by journeyapps.

the class DecoderThread method decode.

private void decode(SourceData sourceData) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    sourceData.setCropRect(cropRect);
    LuminanceSource source = createSource(sourceData);
    if (source != null) {
        rawResult = decoder.decode(source);
    }
    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 (resultHandler != null) {
            BarcodeResult barcodeResult = new BarcodeResult(rawResult, sourceData);
            Message message = Message.obtain(resultHandler, R.id.zxing_decode_succeeded, barcodeResult);
            Bundle bundle = new Bundle();
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (resultHandler != null) {
            Message message = Message.obtain(resultHandler, R.id.zxing_decode_failed);
            message.sendToTarget();
        }
    }
    if (resultHandler != null) {
        List<ResultPoint> resultPoints = decoder.getPossibleResultPoints();
        Message message = Message.obtain(resultHandler, R.id.zxing_possible_result_points, resultPoints);
        message.sendToTarget();
    }
    requestNextPreview();
}
Also used : ResultPoint(com.google.zxing.ResultPoint) Message(android.os.Message) LuminanceSource(com.google.zxing.LuminanceSource) Bundle(android.os.Bundle) Result(com.google.zxing.Result)

Example 9 with LuminanceSource

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

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

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