Improved the command line test app to accept multiple arguments, a --try_harder flag...
[zxing.git] / javase / src / com / google / zxing / client / j2se / CommandLineRunner.java
1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.j2se;
18
19 import com.google.zxing.DecodeHintType;
20 import com.google.zxing.MultiFormatReader;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.MonochromeBitmapSource;
23
24 import javax.imageio.ImageIO;
25 import java.awt.image.BufferedImage;
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.URI;
29 import java.util.Hashtable;
30
31 /**
32  * <p>This simple command line utility decodes files, directories of files, or URIs which are passed
33  * as arguments. By default it uses the normal decoding algorithms, but you can pass --try_harder to
34  * request that hint. The raw text of each barcode is printed, and when running against directories,
35  * summary statistics are also displayed.</p>
36  *
37  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
38  */
39 public final class CommandLineRunner {
40
41   private static Hashtable<DecodeHintType, Object> hints = null;
42
43   private CommandLineRunner() {
44   }
45
46   public static void main(String[] args) throws Exception {
47     for (int x = 0; x < args.length; x++) {
48       if (args[x].equals("--try_harder")) {
49         hints = new Hashtable<DecodeHintType, Object>(3);
50         hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
51       } else if (args[x].startsWith("--")) {
52         System.out.println("Unknown command line option " + args[x]);
53         return;
54       }
55     }
56     for (int x = 0; x < args.length; x++) {
57       if (!args[x].startsWith("--")) {
58         decodeOneArgument(args[x]);
59       }
60     }
61   }
62
63   private static void decodeOneArgument(String argument) throws Exception {
64     File inputFile = new File(argument);
65     if (inputFile.exists()) {
66       if (inputFile.isDirectory()) {
67         int successful = 0;
68         int total = 0;
69         for (File input : inputFile.listFiles()) {
70           String filename = input.getName().toLowerCase();
71           // Skip hidden files and text files (the latter is found in the blackbox tests).
72           if (filename.startsWith(".") || filename.endsWith(".txt")) {
73             continue;
74           }
75           if (decode(input.toURI())) {
76             successful++;
77           }
78           total++;
79         }
80         System.out.println("\nDecoded " + successful + " files out of " + total +
81             " successfully (" + (successful * 100 / total) + "%)\n");
82       } else {
83         decode(inputFile.toURI());
84       }
85     } else {
86       decode(new URI(argument));
87     }
88   }
89
90   private static boolean decode(URI uri) throws IOException {
91     BufferedImage image = ImageIO.read(uri.toURL());
92     if (image == null) {
93       System.err.println(uri.toString() + ": Could not load image");
94       return false;
95     }
96     try {
97       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
98       String result = new MultiFormatReader().decode(source, hints).getText();
99       System.out.println(uri.toString() + ": " + result);
100       return true;
101     } catch (ReaderException e) {
102       System.out.println(uri.toString() + ": No barcode found");
103       return false;
104     }
105   }
106
107 }