Improved error message when file/URI is mistyped.
[zxing.git] / javase / src / com / google / zxing / client / j2se / CommandLineRunner.java
index b675eef..cac8e4f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * Copyright 2007 ZXing authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package com.google.zxing.client.j2se;
 
 import com.google.zxing.DecodeHintType;
+import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.MultiFormatReader;
 import com.google.zxing.ReaderException;
-import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.Result;
 
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
+import java.io.FileNotFoundException;
 import java.net.URI;
 import java.util.Hashtable;
 
@@ -59,7 +61,8 @@ public final class CommandLineRunner {
     }
   }
 
-  private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints) throws Exception {
+  private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints)
+      throws Exception {
     File inputFile = new File(argument);
     if (inputFile.exists()) {
       if (inputFile.isDirectory()) {
@@ -87,15 +90,21 @@ public final class CommandLineRunner {
   }
 
   private static boolean decode(URI uri, Hashtable<DecodeHintType, Object> hints) throws IOException {
-    BufferedImage image = ImageIO.read(uri.toURL());
+    BufferedImage image;
+    try {
+      image = ImageIO.read(uri.toURL());
+    } catch (IllegalArgumentException iae) {
+      throw new FileNotFoundException("Resource not found: " + uri);
+    }
     if (image == null) {
       System.err.println(uri.toString() + ": Could not load image");
       return false;
     }
     try {
       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
-      String result = new MultiFormatReader().decode(source, hints).getText();
-      System.out.println(uri.toString() + ": " + result);
+      Result result = new MultiFormatReader().decode(source, hints);
+      System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + "):\n" +
+          result.getText());
       return true;
     } catch (ReaderException e) {
       System.out.println(uri.toString() + ": No barcode found");