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;
}
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;
}
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();
}
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;
}
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());
}
Aggregations