X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=javase%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2se%2FCommandLineRunner.java;h=a7ad9d62aa09b7b461c5cfacac855d3f6f197ea7;hb=608fe313d82bb79f0f5a96856697bdfb4a6305d3;hp=90d525c8c9d7cdbf0f477b6bc4819dd5bdeb4c32;hpb=9768772d037a58fa5e4dee96de41c8cfeabee4c7;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 90d525c8..a7ad9d62 100644 --- a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java +++ b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java @@ -16,18 +16,18 @@ 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.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 com.google.zxing.common.HybridBinarizer; import java.awt.image.BufferedImage; import java.io.File; @@ -64,22 +64,28 @@ public final class CommandLineRunner { printUsage(); return; } - Hashtable hints = buildHints(); + + boolean tryHarder = false; + boolean productsOnly = false; boolean dumpResults = false; boolean dumpBlackPoint = false; for (String arg : args) { if ("--try_harder".equals(arg)) { - hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); + tryHarder = true; + } else if ("--products_only".equals(arg)) { + productsOnly = true; } else if ("--dump_results".equals(arg)) { dumpResults = true; } else if ("--dump_black_point".equals(arg)) { dumpBlackPoint = true; } else if (arg.startsWith("-")) { - System.out.println("Unknown command line option " + arg); + System.err.println("Unknown command line option " + arg); printUsage(); return; } } + + Hashtable hints = buildHints(tryHarder, productsOnly); for (String arg : args) { if (!arg.startsWith("--")) { decodeOneArgument(arg, hints, dumpResults, dumpBlackPoint); @@ -88,29 +94,36 @@ public final class CommandLineRunner { } // Manually turn on all formats, even those not yet considered production quality. - private static Hashtable buildHints() { + private static Hashtable buildHints(boolean tryHarder, + 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.CODE_39); - vector.addElement(BarcodeFormat.CODE_128); - vector.addElement(BarcodeFormat.ITF); - vector.addElement(BarcodeFormat.QR_CODE); - vector.addElement(BarcodeFormat.DATAMATRIX); - vector.addElement(BarcodeFormat.PDF417); + 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); + } 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 ]"); - System.out.println(" --try_harder: Use the TRY_HARDER hint, default is normal (mobile) mode"); - System.out.println(" --dump_results: Write the decoded contents to input.txt"); - System.out.println(" --dump_black_point: Compare black point algorithms as input.mono.png"); + 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(" --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"); } private static void decodeOneArgument(String argument, Hashtable hints, @@ -187,7 +200,7 @@ public final class CommandLineRunner { } try { LuminanceSource source = new BufferedImageLuminanceSource(image); - BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); + BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); if (dumpBlackPoint) { dumpBlackPoint(uri, image, bitmap); } @@ -200,6 +213,10 @@ public final class CommandLineRunner { } catch (ReaderException e) { 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()); } } @@ -224,7 +241,6 @@ public final class CommandLineRunner { image.getRGB(0, y, width, 1, argb, 0, width); System.arraycopy(argb, 0, pixels, y * stride, width); } - argb = null; // Row sampling BitArray row = new BitArray(width); @@ -272,10 +288,10 @@ public final class CommandLineRunner { // Use the current working directory for URLs String resultName = inputName; - if (uri.getScheme().equals("http")) { + if ("http".equals(uri.getScheme())) { int pos = resultName.lastIndexOf('/'); if (pos > 0) { - resultName = "." + resultName.substring(pos); + resultName = '.' + resultName.substring(pos); } } int pos = resultName.lastIndexOf('.'); @@ -283,13 +299,22 @@ public final class CommandLineRunner { resultName = resultName.substring(0, pos); } resultName += ".mono.png"; + OutputStream outStream = null; try { - OutputStream outStream = new FileOutputStream(resultName); + outStream = new FileOutputStream(resultName); ImageIO.write(result, "png", outStream); } catch (FileNotFoundException e) { - System.out.println("Could not create " + resultName); + System.err.println("Could not create " + resultName); } catch (IOException e) { - System.out.println("Could not write to " + resultName); + System.err.println("Could not write to " + resultName); + } finally { + try { + if (outStream != null) { + outStream.close(); + } + } catch (IOException ioe) { + // continue + } } }