Various code tweaks and refactorings suggested by IntelliJ
[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>Simply attempts to decode the barcode in the image indicated by the single argument
33  * to this program, which may be file or a URI. The raw text is printed.</p>
34  *
35  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
36  */
37 public final class CommandLineRunner {
38
39   private CommandLineRunner() {
40   }
41
42   public static void main(String[] args) throws Exception {
43     File inputFile = new File(args[0]);
44     if (inputFile.exists()) {
45       if (inputFile.isDirectory()) {
46         int successful = 0;
47         for (File input : inputFile.listFiles()) {
48           if (decode(input.toURI())) {
49             successful++;
50           }
51         }
52         System.out.println("Decoded " + successful + " files successfully");
53       } else {
54         decode(inputFile.toURI());
55       }
56     } else {
57       decode(new URI(args[0]));
58     }
59   }
60
61   private static boolean decode(URI uri) throws IOException {
62     BufferedImage image = ImageIO.read(uri.toURL());
63     if (image == null) {
64       System.err.println(uri.toString() + ": Could not load image");
65       return false;
66     }
67     try {
68       Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
69       hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
70       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
71       String result = new MultiFormatReader().decode(source, hints).getText();
72       System.out.println(uri.toString() + ": " + result);
73       return true;
74     } catch (ReaderException e) {
75       System.out.println(uri.toString() + ": No barcode found");
76       return false;
77     }
78   }
79
80   /*
81   public BufferedImage toBufferedImage() {
82     BufferedImage image = new BufferedImage(dimension, dimension, BufferedImage.TYPE_BYTE_BINARY);
83     for (int j = 0; j < dimension; j++) {
84       for (int i = 0; i < dimension; i++) {
85         image.setRGB(j, i, get(i, j) ? 0x00000000 : 0x00FFFFFF);
86       }
87     }
88     return image;
89   }
90    */
91
92 }