package com.google.zxing.client.j2se;
-import com.google.zxing.BlackPointEstimationMethod;
+import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
-import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
+import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ResultParser;
import com.google.zxing.common.BitArray;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.common.GlobalHistogramBinarizer;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Hashtable;
+import java.util.Vector;
import javax.imageio.ImageIO;
printUsage();
return;
}
- Hashtable<DecodeHintType, Object> hints = null;
+ Hashtable<DecodeHintType, Object> hints = buildHints();
boolean dumpResults = false;
boolean dumpBlackPoint = false;
for (String arg : args) {
if ("--try_harder".equals(arg)) {
- hints = new Hashtable<DecodeHintType, Object>(3);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
} else if ("--dump_results".equals(arg)) {
dumpResults = true;
}
}
+ // Manually turn on all formats, even those not yet considered production quality.
+ private static Hashtable<DecodeHintType, Object> buildHints() {
+ Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
+ Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(8);
+ vector.addElement(BarcodeFormat.UPC_A);
+ vector.addElement(BarcodeFormat.UPC_E);
+ vector.addElement(BarcodeFormat.EAN_13);
+ vector.addElement(BarcodeFormat.EAN_8);
+ vector.addElement(BarcodeFormat.CODE_39);
+ vector.addElement(BarcodeFormat.CODE_128);
+ vector.addElement(BarcodeFormat.ITF);
+ vector.addElement(BarcodeFormat.QR_CODE);
+ vector.addElement(BarcodeFormat.DATAMATRIX);
+ vector.addElement(BarcodeFormat.PDF417);
+ hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
+ return hints;
+ }
+
private static void printUsage() {
System.out.println("Decode barcode images using the ZXing library\n");
System.out.println("usage: CommandLineRunner { file | dir | url } [ options ]");
return null;
}
try {
- MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
+ LuminanceSource source = new BufferedImageLuminanceSource(image);
+ BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
if (dumpBlackPoint) {
- dumpBlackPoint(uri, image, source);
+ dumpBlackPoint(uri, image, bitmap);
}
- Result result = new MultiFormatReader().decode(source, hints);
+ Result result = new MultiFormatReader().decode(bitmap, hints);
ParsedResult parsedResult = ResultParser.parseResult(result);
System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() +
", type: " + parsedResult.getType() + "):\nRaw result:\n" + result.getText() +
// Writes out a single PNG which is three times the width of the input image, containing from left
// to right: the original image, the row sampling monochrome version, and the 2D sampling
// monochrome version.
- private static void dumpBlackPoint(URI uri, BufferedImage image, MonochromeBitmapSource source) {
+ // TODO: Update to compare different Binarizer implementations.
+ private static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) {
String inputName = uri.getPath();
if (inputName.contains(".mono.png")) {
return;
}
- int width = source.getWidth();
- int height = source.getHeight();
+ int width = bitmap.getWidth();
+ int height = bitmap.getHeight();
int stride = width * 3;
int[] pixels = new int[stride * height];
BitArray row = new BitArray(width);
for (int y = 0; y < height; y++) {
try {
- source.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, y);
+ row = bitmap.getBlackRow(y, row);
} catch (ReaderException e) {
- // If the row histogram failed, draw a red line and keep going
+ // If fetching the row failed, draw a red line and keep going.
int offset = y * stride + width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = 0xffff0000;
}
continue;
}
- row = source.getBlackRow(y, row, 0, width);
+
int offset = y * stride + width;
for (int x = 0; x < width; x++) {
if (row.get(x)) {
// 2D sampling
try {
- source.estimateBlackPoint(BlackPointEstimationMethod.TWO_D_SAMPLING, 0);
for (int y = 0; y < height; y++) {
- row = source.getBlackRow(y, row, 0, width);
+ BitMatrix matrix = bitmap.getBlackMatrix();
int offset = y * stride + width * 2;
for (int x = 0; x < width; x++) {
- if (row.get(x)) {
+ if (matrix.get(x, y)) {
pixels[offset + x] = 0xff000000;
} else {
pixels[offset + x] = 0xffffffff;