Added which format was decoded to the CommandLineRunner output.
[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.IOException;
29 import java.net.URI;
30 import java.util.Hashtable;
31
32 /**
33  * <p>This simple command line utility decodes files, directories of files, or URIs which are passed
34  * as arguments. By default it uses the normal decoding algorithms, but you can pass --try_harder to
35  * request that hint. The raw text of each barcode is printed, and when running against directories,
36  * summary statistics are also displayed.</p>
37  *
38  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
39  */
40 public final class CommandLineRunner {
41
42   private CommandLineRunner() {
43   }
44
45   public static void main(String[] args) throws Exception {
46     Hashtable<DecodeHintType, Object> hints = null;
47     for (String arg : args) {
48       if ("--try_harder".equals(arg)) {
49         hints = new Hashtable<DecodeHintType, Object>(3);
50         hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
51       } else if (arg.startsWith("--")) {
52         System.out.println("Unknown command line option " + arg);
53         return;
54       }
55     }
56     for (String arg : args) {
57       if (!arg.startsWith("--")) {
58         decodeOneArgument(arg, hints);
59       }
60     }
61   }
62
63   private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints)
64       throws Exception {
65     File inputFile = new File(argument);
66     if (inputFile.exists()) {
67       if (inputFile.isDirectory()) {
68         int successful = 0;
69         int total = 0;
70         for (File input : inputFile.listFiles()) {
71           String filename = input.getName().toLowerCase();
72           // Skip hidden files and text files (the latter is found in the blackbox tests).
73           if (filename.startsWith(".") || filename.endsWith(".txt")) {
74             continue;
75           }
76           if (decode(input.toURI(), hints)) {
77             successful++;
78           }
79           total++;
80         }
81         System.out.println("\nDecoded " + successful + " files out of " + total +
82             " successfully (" + (successful * 100 / total) + "%)\n");
83       } else {
84         decode(inputFile.toURI(), hints);
85       }
86     } else {
87       decode(new URI(argument), hints);
88     }
89   }
90
91   private static boolean decode(URI uri, Hashtable<DecodeHintType, Object> hints) throws IOException {
92     BufferedImage image = ImageIO.read(uri.toURL());
93     if (image == null) {
94       System.err.println(uri.toString() + ": Could not load image");
95       return false;
96     }
97     try {
98       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
99       Result result = new MultiFormatReader().decode(source, hints);
100       System.out.println(uri.toString() + ": " + result.getText() + " format: " +
101           result.getBarcodeFormat());
102       return true;
103     } catch (ReaderException e) {
104       System.out.println(uri.toString() + ": No barcode found");
105       return false;
106     }
107   }
108
109 }