X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=javase%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2se%2FCommandLineRunner.java;h=8ac1de4d91e22bb5a7ebb59d5bb47df3157a0d04;hb=9aeefe94e7fe5485a83f16f4aa1997c610e5804f;hp=78f7626577b894049c49536a26cae8a9edcd27fa;hpb=c4c6e602140cee090e8ee0ed8299d15d8a8ebde6;p=zxing.git
diff --git a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java
index 78f76265..8ac1de4d 100644
--- a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java
+++ b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java
@@ -16,25 +16,34 @@
package com.google.zxing.client.j2se;
+import com.google.zxing.BarcodeFormat;
+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.NotFoundException;
import com.google.zxing.Result;
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.HybridBinarizer;
-import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URI;
+import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Hashtable;
+import java.util.Vector;
+
+import javax.imageio.ImageIO;
/**
*
This simple command line utility decodes files, directories of files, or URIs which are passed
@@ -51,28 +60,95 @@ public final class CommandLineRunner {
}
public static void main(String[] args) throws Exception {
- Hashtable hints = null;
+ if (args == null || args.length == 0) {
+ printUsage();
+ return;
+ }
+
+ boolean tryHarder = false;
+ boolean pureBarcode = false;
+ boolean productsOnly = false;
boolean dumpResults = false;
+ boolean dumpBlackPoint = false;
+ int[] crop = null;
for (String arg : args) {
if ("--try_harder".equals(arg)) {
- hints = new Hashtable(3);
- hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
+ tryHarder = true;
+ } else if ("--pure_barcode".equals(arg)) {
+ pureBarcode = true;
+ } else if ("--products_only".equals(arg)) {
+ productsOnly = true;
} else if ("--dump_results".equals(arg)) {
dumpResults = true;
- } else if (arg.startsWith("--")) {
- System.out.println("Unknown command line option " + arg);
+ } else if ("--dump_black_point".equals(arg)) {
+ dumpBlackPoint = true;
+ } else if (arg.startsWith("--crop")) {
+ crop = new int[4];
+ String[] tokens = arg.substring(7).split(",");
+ for (int i = 0; i < crop.length; i++) {
+ crop[i] = Integer.parseInt(tokens[i]);
+ }
+ } else if (arg.startsWith("-")) {
+ System.err.println("Unknown command line option " + arg);
+ printUsage();
return;
}
}
+
+ Hashtable hints = buildHints(tryHarder, pureBarcode, productsOnly);
for (String arg : args) {
if (!arg.startsWith("--")) {
- decodeOneArgument(arg, hints, dumpResults);
+ decodeOneArgument(arg, hints, dumpResults, dumpBlackPoint, crop);
}
}
}
- private static void decodeOneArgument(String argument, Hashtable hints,
- boolean dumpResults) throws Exception {
+ // Manually turn on all formats, even those not yet considered production quality.
+ private static Hashtable buildHints(boolean tryHarder,
+ boolean pureBarcode,
+ boolean productsOnly) {
+ Hashtable hints = new Hashtable(3);
+ Vector vector = new Vector(8);
+ vector.addElement(BarcodeFormat.UPC_A);
+ vector.addElement(BarcodeFormat.UPC_E);
+ vector.addElement(BarcodeFormat.EAN_13);
+ vector.addElement(BarcodeFormat.EAN_8);
+ vector.addElement(BarcodeFormat.RSS14);
+ if (!productsOnly) {
+ 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);
+ if (tryHarder) {
+ hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
+ }
+ if (pureBarcode) {
+ hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
+ }
+ return hints;
+ }
+
+ private static void printUsage() {
+ System.err.println("Decode barcode images using the ZXing library\n");
+ System.err.println("usage: CommandLineRunner { file | dir | url } [ options ]");
+ System.err.println(" --try_harder: Use the TRY_HARDER hint, default is normal (mobile) mode");
+ System.err.println(" --pure_barcode: Input image is a pure monochrome barcode image, not a photo");
+ System.err.println(" --products_only: Only decode the UPC and EAN families of barcodes");
+ System.err.println(" --dump_results: Write the decoded contents to input.txt");
+ System.err.println(" --dump_black_point: Compare black point algorithms as input.mono.png");
+ System.err.println(" --crop=left,top,width,height: Only examine cropped region of input image(s)");
+ }
+
+ private static void decodeOneArgument(String argument,
+ Hashtable hints,
+ boolean dumpResults,
+ boolean dumpBlackPoint,
+ int[] crop) throws IOException,
+ URISyntaxException {
File inputFile = new File(argument);
if (inputFile.exists()) {
@@ -85,7 +161,11 @@ public final class CommandLineRunner {
if (filename.startsWith(".") || filename.endsWith(".txt")) {
continue;
}
- Result result = decode(input.toURI(), hints);
+ // Skip the results of dumping the black point.
+ if (filename.contains(".mono.png")) {
+ continue;
+ }
+ Result result = decode(input.toURI(), hints, dumpBlackPoint, crop);
if (result != null) {
successful++;
if (dumpResults) {
@@ -97,13 +177,13 @@ public final class CommandLineRunner {
System.out.println("\nDecoded " + successful + " files out of " + total +
" successfully (" + (successful * 100 / total) + "%)\n");
} else {
- Result result = decode(inputFile.toURI(), hints);
+ Result result = decode(inputFile.toURI(), hints, dumpBlackPoint, crop);
if (dumpResults) {
dumpResult(inputFile, result);
}
}
} else {
- decode(new URI(argument), hints);
+ decode(new URI(argument), hints, dumpBlackPoint, crop);
}
}
@@ -126,7 +206,10 @@ public final class CommandLineRunner {
}
}
- private static Result decode(URI uri, Hashtable hints) throws IOException {
+ private static Result decode(URI uri,
+ Hashtable hints,
+ boolean dumpBlackPoint,
+ int[] crop) throws IOException {
BufferedImage image;
try {
image = ImageIO.read(uri.toURL());
@@ -138,16 +221,127 @@ public final class CommandLineRunner {
return null;
}
try {
- MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
- Result result = new MultiFormatReader().decode(source, hints);
+ LuminanceSource source;
+ if (crop == null) {
+ source = new BufferedImageLuminanceSource(image);
+ } else {
+ source = new BufferedImageLuminanceSource(image, crop[0], crop[1], crop[2], crop[3]);
+ }
+ BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
+ if (dumpBlackPoint) {
+ dumpBlackPoint(uri, image, bitmap);
+ }
+ 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() +
"\nParsed result:\n" + parsedResult.getDisplayResult());
return result;
- } catch (ReaderException e) {
+ } catch (NotFoundException nfe) {
System.out.println(uri.toString() + ": No barcode found");
return null;
+ } finally {
+ // Uncomment these lines when turning on exception tracking in ReaderException.
+ //System.out.println("Threw " + ReaderException.getExceptionCountAndReset() + " exceptions");
+ //System.out.println("Throwers:\n" + ReaderException.getThrowersAndReset());
+ }
+ }
+
+ // 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.
+ // 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 = bitmap.getWidth();
+ int height = bitmap.getHeight();
+ int stride = width * 3;
+ int[] pixels = new int[stride * height];
+
+ // The original image
+ int[] argb = new int[width];
+ for (int y = 0; y < height; y++) {
+ image.getRGB(0, y, width, 1, argb, 0, width);
+ System.arraycopy(argb, 0, pixels, y * stride, width);
+ }
+
+ // Row sampling
+ BitArray row = new BitArray(width);
+ for (int y = 0; y < height; y++) {
+ try {
+ row = bitmap.getBlackRow(y, row);
+ } catch (NotFoundException nfe) {
+ // 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;
+ }
+
+ int offset = y * stride + width;
+ for (int x = 0; x < width; x++) {
+ if (row.get(x)) {
+ pixels[offset + x] = 0xff000000;
+ } else {
+ pixels[offset + x] = 0xffffffff;
+ }
+ }
+ }
+
+ // 2D sampling
+ try {
+ for (int y = 0; y < height; y++) {
+ BitMatrix matrix = bitmap.getBlackMatrix();
+ int offset = y * stride + width * 2;
+ for (int x = 0; x < width; x++) {
+ if (matrix.get(x, y)) {
+ pixels[offset + x] = 0xff000000;
+ } else {
+ pixels[offset + x] = 0xffffffff;
+ }
+ }
+ }
+ } catch (NotFoundException nfe) {
+ }
+
+ // Write the result
+ BufferedImage result = new BufferedImage(stride, height, BufferedImage.TYPE_INT_ARGB);
+ result.setRGB(0, 0, stride, height, pixels, 0, stride);
+
+ // Use the current working directory for URLs
+ String resultName = inputName;
+ if ("http".equals(uri.getScheme())) {
+ int pos = resultName.lastIndexOf('/');
+ if (pos > 0) {
+ resultName = '.' + resultName.substring(pos);
+ }
+ }
+ int pos = resultName.lastIndexOf('.');
+ if (pos > 0) {
+ resultName = resultName.substring(0, pos);
+ }
+ resultName += ".mono.png";
+ OutputStream outStream = null;
+ try {
+ outStream = new FileOutputStream(resultName);
+ ImageIO.write(result, "png", outStream);
+ } catch (FileNotFoundException e) {
+ System.err.println("Could not create " + resultName);
+ } catch (IOException e) {
+ System.err.println("Could not write to " + resultName);
+ } finally {
+ try {
+ if (outStream != null) {
+ outStream.close();
+ }
+ } catch (IOException ioe) {
+ // continue
+ }
}
}