Minor change to add braces (also testing commit notification e-mail)
[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.MultiFormatReader;
20 import com.google.zxing.ReaderException;
21
22 import javax.imageio.ImageIO;
23 import java.awt.image.BufferedImage;
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.URI;
27
28 /**
29  * <p>Simply attempts to decode the barcode in the image indicated by the single argument
30  * to this program, which may be file or a URI. The raw text is printed.</p>
31  *
32  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
33  */
34 public final class CommandLineRunner {
35
36   private CommandLineRunner() {
37   }
38
39   public static void main(String[] args) throws Exception {
40     File inputFile = new File(args[0]);
41     if (inputFile.exists()) {
42       if (inputFile.isDirectory()) {
43         int successful = 0;
44         for (File input : inputFile.listFiles()) {
45           if (decode(input.toURI())) {
46             successful++;
47           }
48         }
49         System.out.println("Decoded " + successful + " files successfully");
50       } else {
51         decode(inputFile.toURI());
52       }
53     } else {
54       decode(new URI(args[0]));
55     }
56   }
57
58   private static boolean decode(URI uri) throws IOException {
59     BufferedImage image = ImageIO.read(uri.toURL());
60     if (image == null) {
61       System.err.println(uri.toString() + ": Could not load image");
62       return false;
63     }
64     try {
65       String result = new MultiFormatReader().decode(new BufferedImageMonochromeBitmapSource(image)).getText();
66       System.out.println(uri.toString() + ": " + result);
67       return true;
68     } catch (ReaderException e) {
69       System.out.println(uri.toString() + ": No barcode found");
70       return false;
71     }
72   }
73
74 }