Updated qrcode-2 with corrected output for some Japanese-language results. Also stand...
[zxing.git] / javase / src / com / google / zxing / client / j2se / CommandLineRunner.java
1 /*
2  * Copyright 2007 ZXing authors
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.MonochromeBitmapSource;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24
25 import javax.imageio.ImageIO;
26 import java.awt.image.BufferedImage;
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStreamWriter;
32 import java.io.Writer;
33 import java.net.URI;
34 import java.util.Hashtable;
35 import java.nio.charset.Charset;
36
37 /**
38  * <p>This simple command line utility decodes files, directories of files, or URIs which are passed
39  * as arguments. By default it uses the normal decoding algorithms, but you can pass --try_harder to
40  * request that hint. The raw text of each barcode is printed, and when running against directories,
41  * summary statistics are also displayed.</p>
42  *
43  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
44  */
45 public final class CommandLineRunner {
46
47   private CommandLineRunner() {
48   }
49
50   public static void main(String[] args) throws Exception {
51     Hashtable<DecodeHintType, Object> hints = null;
52     boolean dumpResults = false;
53     for (String arg : args) {
54       if ("--try_harder".equals(arg)) {
55         hints = new Hashtable<DecodeHintType, Object>(3);
56         hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
57       } else if ("--dump_results".equals(arg)) {
58         dumpResults = true;
59       } else if (arg.startsWith("--")) {
60         System.out.println("Unknown command line option " + arg);
61         return;
62       }
63     }
64     for (String arg : args) {
65       if (!arg.startsWith("--")) {
66         decodeOneArgument(arg, hints, dumpResults);
67       }
68     }
69   }
70
71   private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints,
72       boolean dumpResults) throws Exception {
73
74     File inputFile = new File(argument);
75     if (inputFile.exists()) {
76       if (inputFile.isDirectory()) {
77         int successful = 0;
78         int total = 0;
79         for (File input : inputFile.listFiles()) {
80           String filename = input.getName().toLowerCase();
81           // Skip hidden files and text files (the latter is found in the blackbox tests).
82           if (filename.startsWith(".") || filename.endsWith(".txt")) {
83             continue;
84           }
85           Result result = decode(input.toURI(), hints);
86           if (result != null) {
87             successful++;
88             if (dumpResults) {
89               dumpResult(input, result);
90             }
91           }
92           total++;
93         }
94         System.out.println("\nDecoded " + successful + " files out of " + total +
95             " successfully (" + (successful * 100 / total) + "%)\n");
96       } else {
97         Result result = decode(inputFile.toURI(), hints);
98         if (dumpResults) {
99           dumpResult(inputFile, result);
100         }
101       }
102     } else {
103       decode(new URI(argument), hints);
104     }
105   }
106
107   private static void dumpResult(File input, Result result) throws IOException {
108     String name = input.getAbsolutePath();
109     int pos = name.lastIndexOf('.');
110     if (pos > 0) {
111       name = name.substring(0, pos);
112     }
113     File dump = new File(name + ".txt");
114     writeStringToFile(result.getText(), dump);
115   }
116
117   private static void writeStringToFile(String value, File file) throws IOException {
118     Writer out = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF8"));
119     try {
120       out.write(value);
121     } finally {
122       out.close();
123     }
124   }
125
126   private static Result decode(URI uri, Hashtable<DecodeHintType, Object> hints) throws IOException {
127     BufferedImage image;
128     try {
129       image = ImageIO.read(uri.toURL());
130     } catch (IllegalArgumentException iae) {
131       throw new FileNotFoundException("Resource not found: " + uri);
132     }
133     if (image == null) {
134       System.err.println(uri.toString() + ": Could not load image");
135       return null;
136     }
137     try {
138       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
139       Result result = new MultiFormatReader().decode(source, hints);
140       System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + "):\n" +
141           result.getText());
142       return result;
143     } catch (ReaderException e) {
144       System.out.println(uri.toString() + ": No barcode found");
145       return null;
146     }
147   }
148
149 }