X-Git-Url: http://git.rot13.org/?p=zxing.git;a=blobdiff_plain;f=javase%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2se%2FCommandLineRunner.java;h=877733729dc8ce73a3e879e34e20cdf05038176e;hp=f9ccf7c49b5a75001b10f66c38736bc95072f3a1;hb=2d9efc5f0d19d70fb4f240f506bc23992ab3fde6;hpb=53e3a45d8fa5475dfdc3f0cf00ded1895e3cc1d7 diff --git a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java index f9ccf7c4..87773372 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.NotFoundException; 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,44 +64,72 @@ public final class CommandLineRunner { printUsage(); return; } - Hashtable hints = buildHints(); + + 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.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 ("--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, dumpBlackPoint); + decodeOneArgument(arg, hints, dumpResults, dumpBlackPoint, crop); } } } // Manually turn on all formats, even those not yet considered production quality. - private static Hashtable buildHints() { + 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.CODE_39); - vector.addElement(BarcodeFormat.CODE_128); - vector.addElement(BarcodeFormat.ITF); - vector.addElement(BarcodeFormat.QR_CODE); - vector.addElement(BarcodeFormat.DATAMATRIX); - vector.addElement(BarcodeFormat.PDF417); + vector.addElement(BarcodeFormat.RSS14); + if (!productsOnly) { + vector.addElement(BarcodeFormat.CODE_39); + vector.addElement(BarcodeFormat.CODE_93); + 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; } @@ -109,12 +137,18 @@ public final class CommandLineRunner { 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) throws IOException, + private static void decodeOneArgument(String argument, + Hashtable hints, + boolean dumpResults, + boolean dumpBlackPoint, + int[] crop) throws IOException, URISyntaxException { File inputFile = new File(argument); @@ -132,7 +166,7 @@ public final class CommandLineRunner { if (filename.contains(".mono.png")) { continue; } - Result result = decode(input.toURI(), hints, dumpBlackPoint); + Result result = decode(input.toURI(), hints, dumpBlackPoint, crop); if (result != null) { successful++; if (dumpResults) { @@ -144,13 +178,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, dumpBlackPoint); + Result result = decode(inputFile.toURI(), hints, dumpBlackPoint, crop); if (dumpResults) { dumpResult(inputFile, result); } } } else { - decode(new URI(argument), hints, dumpBlackPoint); + decode(new URI(argument), hints, dumpBlackPoint, crop); } } @@ -173,8 +207,10 @@ public final class CommandLineRunner { } } - private static Result decode(URI uri, Hashtable hints, - boolean dumpBlackPoint) throws IOException { + private static Result decode(URI uri, + Hashtable hints, + boolean dumpBlackPoint, + int[] crop) throws IOException { BufferedImage image; try { image = ImageIO.read(uri.toURL()); @@ -186,8 +222,13 @@ public final class CommandLineRunner { return null; } try { - LuminanceSource source = new BufferedImageLuminanceSource(image); - BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); + 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); } @@ -197,7 +238,7 @@ public final class CommandLineRunner { ", 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 { @@ -234,7 +275,7 @@ public final class CommandLineRunner { for (int y = 0; y < height; y++) { try { row = bitmap.getBlackRow(y, row); - } catch (ReaderException e) { + } 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++) { @@ -266,7 +307,7 @@ public final class CommandLineRunner { } } } - } catch (ReaderException e) { + } catch (NotFoundException nfe) { } // Write the result