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