Created
July 11, 2009 14:40
-
-
Save mwalling/145265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under 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.Result; | |
import java.io.FileNotFoundException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.ArrayList; | |
import java.util.Hashtable; | |
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
/** | |
* <p>This simple command line utility decodes files, directories of files, or URIs which are passed | |
* as arguments. By default it uses the normal decoding algorithms, but you can pass --try_harder to | |
* request that hint. The raw text of each barcode is printed, and when running against directories, | |
* summary statistics are also displayed.</p> | |
* | |
* @author Sean Owen | |
* @author dswitkin@google.com (Daniel Switkin) | |
*/ | |
public final class MWWCmdLineRunner | |
{ | |
private static ArrayList<String> ValidISBNs; | |
private static ArrayList<File> UnusuableFiles; | |
private MWWCmdLineRunner() | |
{ | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
Hashtable<DecodeHintType, Object> hints = null; | |
ValidISBNs = new ArrayList<String>(); | |
UnusuableFiles = new ArrayList<File>(); | |
for (String arg : args) | |
{ | |
if ("--try_harder".equals(arg)) | |
{ | |
hints = new Hashtable<DecodeHintType, Object>(3); | |
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); | |
} | |
else if (arg.startsWith("--")) | |
{ | |
System.out.println("Unknown command line option " + arg); | |
return; | |
} | |
} | |
for (String arg : args) | |
{ | |
if (!arg.startsWith("--")) | |
{ | |
decodeOneArgument(arg, hints); | |
} | |
} | |
} | |
private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints) throws IOException, URISyntaxException | |
{ | |
ImgDisplay imgViewer = null; | |
File inputFile = new File(argument); | |
if (inputFile.exists() && inputFile.isDirectory()) | |
{ | |
for (File input : inputFile.listFiles()) | |
{ | |
String filename = input.getName().toLowerCase(); | |
// Skip hidden files and text files (the latter is found in the blackbox tests). | |
if (filename.startsWith(".") || filename.endsWith(".txt")) | |
{ | |
continue; | |
} | |
Result result = decode(input.toURI(), hints); | |
if (result != null) | |
{ | |
ValidISBNs.add(result.getText()); | |
} | |
else | |
{ | |
UnusuableFiles.add(input); | |
} | |
} | |
System.out.println("Complete: Valid ISBNs:"); | |
for (String isbn : ValidISBNs) | |
{ | |
System.out.println(isbn); | |
} | |
System.out.println(); | |
System.out.println("Couldn't process these files:"); | |
for (File badFile : UnusuableFiles) | |
{ | |
System.out.println(badFile.toString()); | |
imgViewer = new ImgDisplay(badFile); | |
imgViewer.setVisible(true); | |
} | |
} | |
else | |
{ | |
System.err.println("Input is not a directory"); | |
} | |
} | |
private static Result decode(URI uri, Hashtable<DecodeHintType, Object> hints) throws IOException | |
{ | |
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 null; | |
} | |
try | |
{ | |
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image); | |
return new MultiFormatReader().decode(source, hints); | |
} | |
catch (ReaderException e) | |
{ | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment