+ 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)) {
+ 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<DecodeHintType, Object> hints = buildHints(tryHarder, pureBarcode, productsOnly);
+ for (String arg : args) {
+ if (!arg.startsWith("--")) {
+ decodeOneArgument(arg, hints, dumpResults, dumpBlackPoint, crop);
+ }
+ }
+ }
+
+ // Manually turn on all formats, even those not yet considered production quality.
+ private static Hashtable<DecodeHintType, Object> buildHints(boolean tryHarder,
+ boolean pureBarcode,
+ boolean productsOnly) {
+ 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.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;
+ }
+
+ 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<DecodeHintType, Object> hints,
+ boolean dumpResults,
+ boolean dumpBlackPoint,
+ int[] crop) throws IOException,
+ URISyntaxException {
+
+ File inputFile = new File(argument);