2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.j2se;
19 import com.google.zxing.DecodeHintType;
20 import com.google.zxing.MultiFormatReader;
21 import com.google.zxing.ReaderException;
23 import javax.imageio.ImageIO;
24 import java.awt.image.BufferedImage;
26 import java.io.IOException;
28 import java.util.Hashtable;
31 * <p>Simply attempts to decode the barcode in the image indicated by the single argument
32 * to this program, which may be file or a URI. The raw text is printed.</p>
34 * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
36 public final class CommandLineRunner {
38 private CommandLineRunner() {
41 public static void main(String[] args) throws Exception {
42 File inputFile = new File(args[0]);
43 if (inputFile.exists()) {
44 if (inputFile.isDirectory()) {
46 for (File input : inputFile.listFiles()) {
47 if (decode(input.toURI())) {
51 System.out.println("Decoded " + successful + " files successfully");
53 decode(inputFile.toURI());
56 decode(new URI(args[0]));
60 private static boolean decode(URI uri) throws IOException {
61 BufferedImage image = ImageIO.read(uri.toURL());
63 System.err.println(uri.toString() + ": Could not load image");
67 Hashtable hints = new Hashtable();
68 hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
69 BufferedImageMonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
70 String result = new MultiFormatReader().decode(source, hints).getText();
71 System.out.println(uri.toString() + ": " + result);
73 } catch (ReaderException e) {
74 System.out.println(uri.toString() + ": No barcode found");
80 public BufferedImage toBufferedImage() {
81 BufferedImage image = new BufferedImage(dimension, dimension, BufferedImage.TYPE_BYTE_BINARY);
82 for (int j = 0; j < dimension; j++) {
83 for (int i = 0; i < dimension; i++) {
84 image.setRGB(j, i, get(i, j) ? 0x00000000 : 0x00FFFFFF);