Many more little tweaks from IntelliJ inspections
[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 CommandLineRunner() {
42   }
43
44   public static void main(String[] args) throws Exception {
45     Hashtable<DecodeHintType, Object> hints = null;
46     for (String arg : args) {
47       if ("--try_harder".equals(arg)) {
48         hints = new Hashtable<DecodeHintType, Object>(3);
49         hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
50       } else if (arg.startsWith("--")) {
51         System.out.println("Unknown command line option " + arg);
52         return;
53       }
54     }
55     for (String arg : args) {
56       if (!arg.startsWith("--")) {
57         decodeOneArgument(arg, hints);
58       }
59     }
60   }
61
62   private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints) throws Exception {
63     File inputFile = new File(argument);
64     if (inputFile.exists()) {
65       if (inputFile.isDirectory()) {
66         int successful = 0;
67         int total = 0;
68         for (File input : inputFile.listFiles()) {
69           String filename = input.getName().toLowerCase();
70           // Skip hidden files and text files (the latter is found in the blackbox tests).
71           if (filename.startsWith(".") || filename.endsWith(".txt")) {
72             continue;
73           }
74           if (decode(input.toURI(), hints)) {
75             successful++;
76           }
77           total++;
78         }
79         System.out.println("\nDecoded " + successful + " files out of " + total +
80             " successfully (" + (successful * 100 / total) + "%)\n");
81       } else {
82         decode(inputFile.toURI(), hints);
83       }
84     } else {
85       decode(new URI(argument), hints);
86     }
87   }
88
89   private static boolean decode(URI uri, Hashtable<DecodeHintType, Object> hints) throws IOException {
90     BufferedImage image = ImageIO.read(uri.toURL());
91     if (image == null) {
92       System.err.println(uri.toString() + ": Could not load image");
93       return false;
94     }
95     try {
96       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
97       String result = new MultiFormatReader().decode(source, hints).getText();
98       System.out.println(uri.toString() + ": " + result);
99       return true;
100     } catch (ReaderException e) {
101       System.out.println(uri.toString() + ": No barcode found");
102       return false;
103     }
104   }
105
106 }