CommandLineRunner can now handle URIs again; refactored some duplicated code

This commit is contained in:
Sean Owen 2015-05-02 18:00:54 +01:00
parent 560cba85b0
commit 106fd2fc0e
3 changed files with 119 additions and 168 deletions

View file

@ -20,6 +20,7 @@ import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType; import com.google.zxing.DecodeHintType;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream; import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -27,7 +28,6 @@ import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -43,6 +43,7 @@ import java.util.regex.Pattern;
* directories, summary statistics are also displayed. * directories, summary statistics are also displayed.
* *
* @author dswitkin@google.com (Daniel Switkin) * @author dswitkin@google.com (Daniel Switkin)
* @author Sean Owen
*/ */
public final class CommandLineRunner { public final class CommandLineRunner {
@ -58,7 +59,7 @@ public final class CommandLineRunner {
} }
Config config = new Config(); Config config = new Config();
Queue<Path> inputs = new ConcurrentLinkedQueue<>(); Queue<URI> inputs = new ConcurrentLinkedQueue<>();
for (String arg : args) { for (String arg : args) {
String[] argValue = arg.split("="); String[] argValue = arg.split("=");
@ -104,13 +105,25 @@ public final class CommandLineRunner {
printUsage(); printUsage();
return; return;
} }
addArgumentToInputs(Paths.get(arg), config, inputs); URI argURI = URI.create(arg);
if (argURI.getScheme() == null) {
argURI = new URI("file", argURI.getSchemeSpecificPart(), argURI.getFragment());
}
addArgumentToInputs(argURI, config, inputs);
break; break;
} }
} }
int numInputs = inputs.size();
if (numInputs == 0) {
System.err.println("No inputs specified");
printUsage();
return;
}
config.setHints(buildHints(config)); config.setHints(buildHints(config));
int numThreads = Math.min(inputs.size(), Runtime.getRuntime().availableProcessors()); int numThreads = Math.min(numInputs, Runtime.getRuntime().availableProcessors());
int successful = 0; int successful = 0;
if (numThreads > 1) { if (numThreads > 1) {
ExecutorService executor = Executors.newFixedThreadPool(numThreads); ExecutorService executor = Executors.newFixedThreadPool(numThreads);
@ -126,44 +139,38 @@ public final class CommandLineRunner {
successful += new DecodeWorker(config, inputs).call(); successful += new DecodeWorker(config, inputs).call();
} }
int total = inputs.size(); if (numInputs > 1) {
if (total > 1) { System.out.println("\nDecoded " + successful + " files out of " + numInputs +
System.out.println("\nDecoded " + successful + " files out of " + total + " successfully (" + (successful * 100 / numInputs) + "%)\n");
" successfully (" + (successful * 100 / total) + "%)\n");
} }
} }
// Build all the inputs up front into a single flat list, so the threads can atomically pull /**
// paths/URLs off the queue. * Build all the inputs up front into a single flat list, so the threads can atomically pull
private static void addArgumentToInputs(Path inputFile, Config config, Queue<Path> inputs) throws IOException { * paths/URLs off the queue.
if (Files.isDirectory(inputFile)) { */
try (DirectoryStream<Path> paths = Files.newDirectoryStream(inputFile)) { private static void addArgumentToInputs(URI input, Config config, Queue<URI> inputs) throws IOException {
for (Path singleFile : paths) { // Special case: a local directory
String filename = singleFile.getFileName().toString().toLowerCase(Locale.ENGLISH); if ("file".equals(input.getScheme()) && Files.isDirectory(Paths.get(input))) {
try (DirectoryStream<Path> childPaths = Files.newDirectoryStream(Paths.get(input))) {
for (Path childPath : childPaths) {
Path realChildPath = childPath.toRealPath();
// Skip hidden files and directories (e.g. svn stuff). // Skip hidden files and directories (e.g. svn stuff).
if (filename.startsWith(".")) { if (!realChildPath.getFileName().toString().startsWith(".")) {
continue; // Recur on nested directories if requested, otherwise skip them.
} if (config.isRecursive() && Files.isDirectory(realChildPath)) {
// Recur on nested directories if requested, otherwise skip them. addArgumentToInputs(realChildPath.toUri(), config, inputs);
if (Files.isDirectory(singleFile)) { } else {
if (config.isRecursive()) { inputs.add(realChildPath.toUri());
addArgumentToInputs(singleFile, config, inputs);
} }
continue;
} }
// Skip text files and the results of dumping the black point.
if (filename.endsWith(".txt") || filename.contains(".mono.png")) {
continue;
}
inputs.add(singleFile);
} }
} }
} else { } else {
inputs.add(inputFile); inputs.add(input);
} }
} }
// Manually turn on all formats, even those not yet considered production quality.
private static Map<DecodeHintType,?> buildHints(Config config) { private static Map<DecodeHintType,?> buildHints(Config config) {
Collection<BarcodeFormat> possibleFormats = new ArrayList<>(); Collection<BarcodeFormat> possibleFormats = new ArrayList<>();
String[] possibleFormatsNames = config.getPossibleFormats(); String[] possibleFormatsNames = config.getPossibleFormats();

View file

@ -42,7 +42,6 @@ import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -59,9 +58,9 @@ final class DecodeWorker implements Callable<Integer> {
private static final int WHITE = 0xFFFFFFFF; private static final int WHITE = 0xFFFFFFFF;
private final Config config; private final Config config;
private final Queue<Path> inputs; private final Queue<URI> inputs;
DecodeWorker(Config config, Queue<Path> inputs) { DecodeWorker(Config config, Queue<URI> inputs) {
this.config = config; this.config = config;
this.inputs = inputs; this.inputs = inputs;
} }
@ -69,139 +68,101 @@ final class DecodeWorker implements Callable<Integer> {
@Override @Override
public Integer call() throws IOException { public Integer call() throws IOException {
int successful = 0; int successful = 0;
Path input; for (URI input; (input = inputs.poll()) != null;) {
while ((input = inputs.poll()) != null) { Result[] results = decode(input, config.getHints());
if (Files.exists(input)) { if (results != null) {
if (config.isMulti()) { successful++;
Result[] results = decodeMulti(input.toUri(), config.getHints()); if (config.isDumpResults()) {
if (results != null) { dumpResult(input, results);
successful++;
if (config.isDumpResults()) {
dumpResultMulti(input, results);
}
}
} else {
Result result = decode(input.toUri(), config.getHints());
if (result != null) {
successful++;
if (config.isDumpResults()) {
dumpResult(input, result);
}
}
}
} else {
if (decode(input.toUri(), config.getHints()) != null) {
successful++;
} }
} }
} }
return successful; return successful;
} }
private static void dumpResult(Path input, Result result) throws IOException { private static Path buildOutputPath(URI input, String suffix) throws IOException {
String name = input.getFileName().toString(); Path outDir;
int pos = name.lastIndexOf('.'); String inputFileName;
if (pos > 0) { if ("file".equals(input.getScheme())) {
name = name.substring(0, pos) + ".txt"; Path inputPath = Paths.get(input);
outDir = inputPath.getParent();
inputFileName = inputPath.getFileName().toString();
} else {
outDir = Paths.get(".").toRealPath();
String[] pathElements = input.getPath().split("/");
inputFileName = pathElements[pathElements.length - 1];
} }
Path dumpFile = input.getParent().resolve(name);
Files.write(dumpFile, Collections.singleton(result.getText()), StandardCharsets.UTF_8); // Replace/add extension
int pos = inputFileName.lastIndexOf('.');
if (pos > 0) {
inputFileName = inputFileName.substring(0, pos) + suffix;
} else {
inputFileName += suffix;
}
return outDir.resolve(inputFileName);
} }
private static void dumpResultMulti(Path input, Result[] results) throws IOException { private static void dumpResult(URI input, Result... results) throws IOException {
String name = input.getFileName().toString();
int pos = name.lastIndexOf('.');
if (pos > 0) {
name = name.substring(0, pos) + ".txt";
}
Path dumpFile = input.getParent().resolve(name);
Collection<String> resultTexts = new ArrayList<>(); Collection<String> resultTexts = new ArrayList<>();
for (Result result : results) { for (Result result : results) {
resultTexts.add(result.getText()); resultTexts.add(result.getText());
} }
Files.write(dumpFile, resultTexts, StandardCharsets.UTF_8); Files.write(buildOutputPath(input, ".txt"), resultTexts, StandardCharsets.UTF_8);
} }
private Result decode(URI uri, Map<DecodeHintType,?> hints) throws IOException { private Result[] decode(URI uri, Map<DecodeHintType,?> hints) throws IOException {
BufferedImage image = ImageReader.readImage(uri); BufferedImage image = ImageReader.readImage(uri);
try {
LuminanceSource source;
if (config.getCrop() == null) {
source = new BufferedImageLuminanceSource(image);
} else {
int[] crop = config.getCrop();
source = new BufferedImageLuminanceSource(image, crop[0], crop[1], crop[2], crop[3]);
}
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
if (config.isDumpBlackPoint()) {
dumpBlackPoint(uri, image, bitmap);
}
Result result = new MultiFormatReader().decode(bitmap, hints);
if (config.isBrief()) {
System.out.println(uri + ": Success");
} else {
ParsedResult parsedResult = ResultParser.parseResult(result);
System.out.println(uri + " (format: " + result.getBarcodeFormat() + ", type: " +
parsedResult.getType() + "):\nRaw result:\n" + result.getText() + "\nParsed result:\n" +
parsedResult.getDisplayResult());
LuminanceSource source;
if (config.getCrop() == null) {
source = new BufferedImageLuminanceSource(image);
} else {
int[] crop = config.getCrop();
source = new BufferedImageLuminanceSource(image, crop[0], crop[1], crop[2], crop[3]);
}
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
if (config.isDumpBlackPoint()) {
dumpBlackPoint(uri, image, bitmap);
}
MultiFormatReader multiFormatReader = new MultiFormatReader();
Result[] results;
try {
if (config.isMulti()) {
MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(multiFormatReader);
results = reader.decodeMultiple(bitmap, hints);
} else {
results = new Result[]{multiFormatReader.decode(bitmap, hints)};
}
} catch (NotFoundException ignored) {
System.out.println(uri + ": No barcode found");
return null;
}
if (config.isBrief()) {
System.out.println(uri + ": Success");
} else {
for (Result result : results) {
ParsedResult parsedResult = ResultParser.parseResult(result);
System.out.println(uri +
" (format: " + result.getBarcodeFormat() +
", type: " + parsedResult.getType() + "):\n" +
"Raw result:\n" +
result.getText() + "\n" +
"Parsed result:\n" +
parsedResult.getDisplayResult());
System.out.println("Found " + result.getResultPoints().length + " result points."); System.out.println("Found " + result.getResultPoints().length + " result points.");
for (int i = 0; i < result.getResultPoints().length; i++) { for (int i = 0; i < result.getResultPoints().length; i++) {
ResultPoint rp = result.getResultPoints()[i]; ResultPoint rp = result.getResultPoints()[i];
if (rp != null) { System.out.println(" Point " + i + ": (" + rp.getX() + ',' + rp.getY() + ')');
System.out.println(" Point " + i + ": (" + rp.getX() + ',' + rp.getY() + ')');
}
} }
} }
return result;
} catch (NotFoundException ignored) {
System.out.println(uri + ": No barcode found");
return null;
} }
}
private Result[] decodeMulti(URI uri, Map<DecodeHintType,?> hints) throws IOException { return results;
BufferedImage image = ImageReader.readImage(uri);
try {
LuminanceSource source;
if (config.getCrop() == null) {
source = new BufferedImageLuminanceSource(image);
} else {
int[] crop = config.getCrop();
source = new BufferedImageLuminanceSource(image, crop[0], crop[1], crop[2], crop[3]);
}
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
if (config.isDumpBlackPoint()) {
dumpBlackPoint(uri, image, bitmap);
}
MultiFormatReader multiFormatReader = new MultiFormatReader();
MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(multiFormatReader);
Result[] results = reader.decodeMultiple(bitmap, hints);
if (config.isBrief()) {
System.out.println(uri + ": Success");
} else {
for (Result result : results) {
ParsedResult parsedResult = ResultParser.parseResult(result);
System.out.println(uri + " (format: "
+ result.getBarcodeFormat() + ", type: "
+ parsedResult.getType() + "):\nRaw result:\n"
+ result.getText() + "\nParsed result:\n"
+ parsedResult.getDisplayResult());
System.out.println("Found " + result.getResultPoints().length + " result points.");
for (int i = 0; i < result.getResultPoints().length; i++) {
ResultPoint rp = result.getResultPoints()[i];
System.out.println(" Point " + i + ": (" + rp.getX() + ',' + rp.getY() + ')');
}
}
}
return results;
} catch (NotFoundException ignored) {
System.out.println(uri + ": No barcode found");
return null;
}
} }
/** /**
@ -209,11 +170,7 @@ final class DecodeWorker implements Callable<Integer> {
* to right: the original image, the row sampling monochrome version, and the 2D sampling * to right: the original image, the row sampling monochrome version, and the 2D sampling
* monochrome version. * monochrome version.
*/ */
private static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) { private static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) throws IOException {
if (uri.getPath().contains(".mono.png")) {
return;
}
int width = bitmap.getWidth(); int width = bitmap.getWidth();
int height = bitmap.getHeight(); int height = bitmap.getHeight();
int stride = width * 3; int stride = width * 3;
@ -263,30 +220,17 @@ final class DecodeWorker implements Callable<Integer> {
private static void writeResultImage(int stride, private static void writeResultImage(int stride,
int height, int height,
int[] pixels, int[] pixels,
URI uri, URI input,
String suffix) { String suffix) throws IOException {
BufferedImage result = new BufferedImage(stride, height, BufferedImage.TYPE_INT_ARGB); BufferedImage result = new BufferedImage(stride, height, BufferedImage.TYPE_INT_ARGB);
result.setRGB(0, 0, stride, height, pixels, 0, stride); result.setRGB(0, 0, stride, height, pixels, 0, stride);
Path imagePath = buildOutputPath(input, suffix);
// Use the current working directory for URLs
String resultName = uri.getPath();
if ("http".equals(uri.getScheme())) {
int pos = resultName.lastIndexOf('/');
if (pos > 0) {
resultName = '.' + resultName.substring(pos);
}
}
int pos = resultName.lastIndexOf('.');
if (pos > 0) {
resultName = resultName.substring(0, pos);
}
resultName += suffix;
try { try {
if (!ImageIO.write(result, "png", Paths.get(resultName).toFile())) { if (!ImageIO.write(result, "png", imagePath.toFile())) {
System.err.println("Could not encode an image to " + resultName); System.err.println("Could not encode an image to " + imagePath);
} }
} catch (IOException ignored) { } catch (IOException ignored) {
System.err.println("Could not write to " + resultName); System.err.println("Could not write to " + imagePath);
} }
} }

View file

@ -53,8 +53,8 @@ public final class ImageReader {
} }
public static BufferedImage readDataURIImage(URI uri) throws IOException { public static BufferedImage readDataURIImage(URI uri) throws IOException {
String uriString = uri.toString(); String uriString = uri.getSchemeSpecificPart();
if (!uriString.startsWith("data:image/")) { if (!uriString.startsWith("image/")) {
throw new IOException("Unsupported data URI MIME type"); throw new IOException("Unsupported data URI MIME type");
} }
int base64Start = uriString.indexOf(BASE64TOKEN); int base64Start = uriString.indexOf(BASE64TOKEN);