diff --git a/core/src/com/google/zxing/aztec/detector/Detector.java b/core/src/com/google/zxing/aztec/detector/Detector.java index 413a41101..41eadefd9 100644 --- a/core/src/com/google/zxing/aztec/detector/Detector.java +++ b/core/src/com/google/zxing/aztec/detector/Detector.java @@ -28,8 +28,8 @@ import com.google.zxing.common.reedsolomon.ReedSolomonDecoder; import com.google.zxing.common.reedsolomon.ReedSolomonException; /** - *

Encapsulates logic that can detect an Aztec Code in an image, even if the Aztec Code - * is rotated or skewed, or partially obscured.

+ * Encapsulates logic that can detect an Aztec Code in an image, even if the Aztec Code + * is rotated or skewed, or partially obscured. * * @author David Olivier */ @@ -48,7 +48,7 @@ public final class Detector { } /** - *

Detects an Aztec Code in an image.

+ * Detects an Aztec Code in an image. * * @return {@link AztecDetectorResult} encapsulating results of detecting an Aztec Code * @throws NotFoundException if no Aztec Code can be found @@ -74,28 +74,29 @@ public final class Detector { } /** - *

Extracts the number of data layers and data blocks from the layer around the bull's eye

+ * Extracts the number of data layers and data blocks from the layer around the bull's eye. * * @param bullEyeCornerPoints the array of bull's eye corners * @throws NotFoundException in case of too many errors or invalid parameters */ - private void extractParameters(Point[] bullEyeCornerPoints) - throws NotFoundException { + private void extractParameters(Point[] bullEyeCornerPoints) throws NotFoundException { + + int twoCenterLayers = 2 * nbCenterLayers; // Get the bits around the bull's eye - boolean[] resab = sampleLine(bullEyeCornerPoints[0], bullEyeCornerPoints[1], 2*nbCenterLayers+1); - boolean[] resbc = sampleLine(bullEyeCornerPoints[1], bullEyeCornerPoints[2], 2*nbCenterLayers+1); - boolean[] rescd = sampleLine(bullEyeCornerPoints[2], bullEyeCornerPoints[3], 2*nbCenterLayers+1); - boolean[] resda = sampleLine(bullEyeCornerPoints[3], bullEyeCornerPoints[0], 2*nbCenterLayers+1); + boolean[] resab = sampleLine(bullEyeCornerPoints[0], bullEyeCornerPoints[1], twoCenterLayers+1); + boolean[] resbc = sampleLine(bullEyeCornerPoints[1], bullEyeCornerPoints[2], twoCenterLayers+1); + boolean[] rescd = sampleLine(bullEyeCornerPoints[2], bullEyeCornerPoints[3], twoCenterLayers+1); + boolean[] resda = sampleLine(bullEyeCornerPoints[3], bullEyeCornerPoints[0], twoCenterLayers+1); // Determine the orientation of the matrix - if (resab[0] && resab[2 * nbCenterLayers]) { + if (resab[0] && resab[twoCenterLayers]) { shift = 0; - } else if (resbc[0] && resbc[2 * nbCenterLayers]) { + } else if (resbc[0] && resbc[twoCenterLayers]) { shift = 1; - } else if (rescd[0] && rescd[2 * nbCenterLayers]) { + } else if (rescd[0] && rescd[twoCenterLayers]) { shift = 2; - } else if (resda[0] && resda[2 * nbCenterLayers]) { + } else if (resda[0] && resda[twoCenterLayers]) { shift = 3; } else { throw NotFoundException.getNotFoundInstance(); @@ -118,9 +119,9 @@ public final class Detector { } parameterData = new boolean[28]; - for (int i = 0; i < 28; i++) { - parameterData[i] = shiftedParameterData[(i+shift*7)%28]; - } + for (int i = 0; i < 28; i++) { + parameterData[i] = shiftedParameterData[(i+shift*7)%28]; + } } else { shiftedParameterData = new boolean[40]; for (int i = 0; i < 11; i++) { @@ -132,16 +133,16 @@ public final class Detector { } if (i > 5) { shiftedParameterData[i-1] = resab[2+i]; - shiftedParameterData[i+10-1] = resbc[2+i]; - shiftedParameterData[i+20-1] = rescd[2+i]; - shiftedParameterData[i+30-1] = resda[2+i]; + shiftedParameterData[i+9] = resbc[2+i]; + shiftedParameterData[i+19] = rescd[2+i]; + shiftedParameterData[i+29] = resda[2+i]; } } parameterData = new boolean[40]; - for (int i = 0; i < 40; i++) { - parameterData[i] = shiftedParameterData[(i+shift*10)%40]; - } + for (int i = 0; i < 40; i++) { + parameterData[i] = shiftedParameterData[(i+shift*10)%40]; + } } // corrects the error using RS algorithm @@ -152,8 +153,7 @@ public final class Detector { } /** - * - *

Gets the Aztec code corners from the bull's eye corners and the parameters

+ * Gets the Aztec code corners from the bull's eye corners and the parameters. * * @param bullEyeCornerPoints the array of bull's eye corners * @return the array of aztec code corners @@ -185,18 +185,25 @@ public final class Detector { int targetbx = MathUtils.round(bullEyeCornerPoints[1].x + ratio * dx); int targetby = MathUtils.round(bullEyeCornerPoints[1].y+ratio*dy); - if (!isValid(targetax, targetay) || !isValid(targetbx, targetby) || !isValid(targetcx, targetcy) || !isValid(targetdx, targetdy)) { + if (!isValid(targetax, targetay) || + !isValid(targetbx, targetby) || + !isValid(targetcx, targetcy) || + !isValid(targetdx, targetdy)) { throw NotFoundException.getNotFoundInstance(); } - return new ResultPoint[]{new ResultPoint(targetax, targetay), new ResultPoint(targetbx, targetby), new ResultPoint(targetcx, targetcy), new ResultPoint(targetdx, targetdy)}; + return new ResultPoint[]{ + new ResultPoint(targetax, targetay), + new ResultPoint(targetbx, targetby), + new ResultPoint(targetcx, targetcy), + new ResultPoint(targetdx, targetdy) + }; } /** + * Corrects the parameter bits using Reed-Solomon algorithm. * - *

Corrects the parameter bits using Reed-Solomon algorithm

- * - * @param parameterData paremeter bits + * @param parameterData parameter bits * @param compact true if this is a compact Aztec code * @throws NotFoundException if the array contains too many errors */ @@ -235,17 +242,16 @@ public final class Detector { } for (int i = 0; i < numDataCodewords; i ++) { - int flag = 1; - for (int j = 1; j <= codewordSize; j++) { - parameterData[i*codewordSize+codewordSize-j] = (parameterWords[i] & flag) == flag; - flag <<= 1; - } + int flag = 1; + for (int j = 1; j <= codewordSize; j++) { + parameterData[i*codewordSize+codewordSize-j] = (parameterWords[i] & flag) == flag; + flag <<= 1; + } } } /** - * - *

Finds the corners of a bull-eye centered on the passed point

+ * Finds the corners of a bull-eye centered on the passed point. * * @param pCenter Center point * @return The corners of the bull-eye @@ -308,21 +314,22 @@ public final class Detector { int targetbx = MathUtils.round(pinb.x+ratio*dx); int targetby = MathUtils.round(pinb.y+ratio*dy); - if (!isValid(targetax, targetay) || !isValid(targetbx, targetby) - || !isValid(targetcx, targetcy) || !isValid(targetdx, targetdy)) { + if (!isValid(targetax, targetay) || + !isValid(targetbx, targetby) || + !isValid(targetcx, targetcy) || + !isValid(targetdx, targetdy)) { throw NotFoundException.getNotFoundInstance(); } - Point pa = new Point(targetax,targetay); - Point pb = new Point(targetbx,targetby); - Point pc = new Point(targetcx,targetcy); - Point pd = new Point(targetdx,targetdy); - - return new Point[]{pa, pb, pc, pd}; + return new Point[] { + new Point(targetax,targetay), + new Point(targetbx,targetby), + new Point(targetcx,targetcy), + new Point(targetdx,targetdy) + }; } /** - * * Finds a candidate center point of an Aztec code from an image * * @return the center point @@ -349,16 +356,16 @@ public final class Detector { // In that case, surely in the bull's eye, we try to expand the rectangle. int cx = image.getWidth()/2; int cy = image.getHeight()/2; - pointA = getFirstDifferent(new Point(cx+15/2, cy-15/2), false, 1, -1).toResultPoint(); - pointB = getFirstDifferent(new Point(cx+15/2, cy+15/2), false, 1, 1).toResultPoint(); - pointC = getFirstDifferent(new Point(cx-15/2, cy+15/2), false, -1, 1).toResultPoint(); - pointD = getFirstDifferent(new Point(cx-15/2, cy-15/2), false, -1, -1).toResultPoint(); + pointA = getFirstDifferent(new Point(cx+7, cy-7), false, 1, -1).toResultPoint(); + pointB = getFirstDifferent(new Point(cx+7, cy+7), false, 1, 1).toResultPoint(); + pointC = getFirstDifferent(new Point(cx-7, cy+7), false, -1, 1).toResultPoint(); + pointD = getFirstDifferent(new Point(cx-7, cy-7), false, -1, -1).toResultPoint(); } //Compute the center of the rectangle - int cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX())/4); - int cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY())/4); + int cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f); + int cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f); // Redetermine the white rectangle starting from previously computed center. // This will ensure that we end up with a white rectangle in center bull's eye @@ -373,16 +380,16 @@ public final class Detector { // This exception can be in case the initial rectangle is white // In that case we try to expand the rectangle. - pointA = getFirstDifferent(new Point(cx+15/2, cy-15/2), false, 1, -1).toResultPoint(); - pointB = getFirstDifferent(new Point(cx+15/2, cy+15/2), false, 1, 1).toResultPoint(); - pointC = getFirstDifferent(new Point(cx-15/2, cy+15/2), false, -1, 1).toResultPoint(); - pointD = getFirstDifferent(new Point(cx-15/2, cy-15/2), false, -1, -1).toResultPoint(); + pointA = getFirstDifferent(new Point(cx+7, cy-7), false, 1, -1).toResultPoint(); + pointB = getFirstDifferent(new Point(cx+7, cy+7), false, 1, 1).toResultPoint(); + pointC = getFirstDifferent(new Point(cx-7, cy+7), false, -1, 1).toResultPoint(); + pointD = getFirstDifferent(new Point(cx-7, cy-7), false, -1, -1).toResultPoint(); } // Recompute the center of the rectangle - cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX())/4); - cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY())/4); + cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f); + cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f); return new Point(cx, cy); } @@ -431,7 +438,7 @@ public final class Detector { } /** - * Sets number of layers and number of datablocks from parameter bits + * Sets number of layers and number of data blocks from parameter bits */ private void getParameters(boolean[] parameterData) { @@ -449,24 +456,22 @@ public final class Detector { for (int i = 0; i < nbBitsForNbLayers; i++) { nbLayers <<= 1; if (parameterData[i]) { - nbLayers += 1; + nbLayers++; } } for (int i = nbBitsForNbLayers; i < nbBitsForNbLayers + nbBitsForNbDatablocks; i++) { nbDataBlocks <<= 1; if (parameterData[i]) { - nbDataBlocks += 1; + nbDataBlocks++; } } - nbLayers ++; - nbDataBlocks ++; - + nbLayers++; + nbDataBlocks++; } /** - * * Samples a line * * @param p1 first point @@ -487,8 +492,8 @@ public final class Detector { for (int i = 0; i < size; i++) { res[i] = image.get(MathUtils.round(px), MathUtils.round(py)); - px+=dx; - py+=dy; + px += dx; + py += dy; } return res; @@ -560,15 +565,11 @@ public final class Detector { float errRatio = (float)error/d; - if (errRatio > 0.1 && errRatio < 0.9) { + if (errRatio > 0.1f && errRatio < 0.9f) { return 0; } - if (errRatio <= 0.1) { - return colorModel?1:-1; - } else { - return colorModel?-1:1; - } + return (errRatio <= 0.1f) == colorModel ? 1 : -1; } /** @@ -598,20 +599,6 @@ public final class Detector { return new Point(x,y); } - - private static final class Point { - public final int x; - public final int y; - - public ResultPoint toResultPoint() { - return new ResultPoint(x, y); - } - - private Point(int x, int y) { - this.x = x; - this.y = y; - } - } private boolean isValid(int x, int y) { return x >= 0 && x < image.getWidth() && y > 0 && y < image.getHeight(); @@ -621,4 +608,18 @@ public final class Detector { return MathUtils.distance(a.x, a.y, b.x, b.y); } + static final class Point { + final int x; + final int y; + + ResultPoint toResultPoint() { + return new ResultPoint(x, y); + } + + Point(int x, int y) { + this.x = x; + this.y = y; + } + } + } diff --git a/core/src/com/google/zxing/client/result/ParsedResult.java b/core/src/com/google/zxing/client/result/ParsedResult.java index cf55b9c00..d9eb3e85f 100644 --- a/core/src/com/google/zxing/client/result/ParsedResult.java +++ b/core/src/com/google/zxing/client/result/ParsedResult.java @@ -16,12 +16,10 @@ package com.google.zxing.client.result; -import com.google.zxing.Result; - /** *

Abstract class representing the result of decoding a barcode, as more than * a String -- as some type of structured data. This might be a subclass which represents - * a URL, or an e-mail address. {@link ResultParser#parseResult(Result)} will turn a raw + * a URL, or an e-mail address. {@link ResultParser#parseResult(com.google.zxing.Result)} will turn a raw * decoded string into the most appropriate type of structured representation.

* *

Thanks to Jeff Griffin for proposing rewrite of these classes that relies less diff --git a/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java b/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java index 79b80637d..ddc1a5c59 100644 --- a/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java +++ b/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java @@ -287,7 +287,7 @@ public final class WhiteRectangleDetector { float ti = t.getX(); float tj = t.getY(); - if (yi < width / 2) { + if (yi < width / 2.0f) { return new ResultPoint[]{ new ResultPoint(ti - CORR, tj + CORR), new ResultPoint(zi + CORR, zj + CORR), diff --git a/core/src/com/google/zxing/oned/CodaBarWriter.java b/core/src/com/google/zxing/oned/CodaBarWriter.java index e615bbc59..ac9d849b6 100644 --- a/core/src/com/google/zxing/oned/CodaBarWriter.java +++ b/core/src/com/google/zxing/oned/CodaBarWriter.java @@ -16,12 +16,10 @@ package com.google.zxing.oned; -import com.google.zxing.common.BitMatrix; - import java.util.Arrays; /** - * This class renders CodaBar as {@link BitMatrix}. + * This class renders CodaBar as {@code boolean[]}. * * @author dsbnatut@gmail.com (Kazuki Nishiura) */ @@ -30,9 +28,6 @@ public final class CodaBarWriter extends OneDimensionalCodeWriter { private static final char[] START_CHARS = {'A', 'B', 'C', 'D'}; private static final char[] END_CHARS = {'T', 'N', '*', 'E'}; - /* - * @see OneDimensionalCodeWriter#encode(String) - */ @Override public boolean[] encode(String contents) { diff --git a/core/src/com/google/zxing/oned/OneDimensionalCodeWriter.java b/core/src/com/google/zxing/oned/OneDimensionalCodeWriter.java index 87ac46aff..2735b0e7f 100644 --- a/core/src/com/google/zxing/oned/OneDimensionalCodeWriter.java +++ b/core/src/com/google/zxing/oned/OneDimensionalCodeWriter.java @@ -120,7 +120,7 @@ public abstract class OneDimensionalCodeWriter implements Writer { } /** - * Encode the contents to byte array expression of one-dimensional barcode. + * Encode the contents to boolean array expression of one-dimensional barcode. * Start code and end code should be included in result, and side margins should not be included. * * @return a {@code boolean[]} of horizontal pixels (false = white, true = black) diff --git a/core/src/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java b/core/src/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java index 6f84550fc..a73d585f3 100644 --- a/core/src/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java +++ b/core/src/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java @@ -449,10 +449,8 @@ public final class RSSExpandedReader extends AbstractRSSReader { DataCharacter leftChar = this.decodeDataCharacter(row, pattern, isOddPattern, true); - if (!previousPairs.isEmpty()){ - if (previousPairs.get(previousPairs.size()-1).mustBeLast()){ - throw NotFoundException.getNotFoundInstance(); - } + if (!previousPairs.isEmpty() && previousPairs.get(previousPairs.size()-1).mustBeLast()) { + throw NotFoundException.getNotFoundInstance(); } DataCharacter rightChar; diff --git a/core/src/com/google/zxing/pdf417/decoder/BitMatrixParser.java b/core/src/com/google/zxing/pdf417/decoder/BitMatrixParser.java index 9e5139fc1..ccaa30d24 100644 --- a/core/src/com/google/zxing/pdf417/decoder/BitMatrixParser.java +++ b/core/src/com/google/zxing/pdf417/decoder/BitMatrixParser.java @@ -212,10 +212,8 @@ final class BitMatrixParser { } else { // Left row indicator column int cw = getCodeword(symbol); - if (ecLevel < 0) { - if (rowNumber % 3 == 1) { - leftColumnECData = cw; - } + if (ecLevel < 0 && rowNumber % 3 == 1) { + leftColumnECData = cw; } } symbol = 0; @@ -227,12 +225,10 @@ final class BitMatrixParser { //columns--; // Overwrite the last codeword i.e. Right Row Indicator --next; - if (ecLevel < 0) { - if (rowNumber % 3 == 2) { - rightColumnECData = codewords[next]; - if (rightColumnECData == leftColumnECData && leftColumnECData != 0) { - ecLevel = ((rightColumnECData % 30) - rows % 3) / 3; - } + if (ecLevel < 0 && rowNumber % 3 == 2) { + rightColumnECData = codewords[next]; + if (rightColumnECData == leftColumnECData && leftColumnECData != 0) { + ecLevel = ((rightColumnECData % 30) - rows % 3) / 3; } } codewords[next] = 0; @@ -243,7 +239,6 @@ final class BitMatrixParser { /** * Translate the symbol into a codeword. * - * @param symbol * @return the codeword corresponding to the symbol. */ private static int getCodeword(long symbol) { diff --git a/core/src/com/google/zxing/pdf417/encoder/PDF417.java b/core/src/com/google/zxing/pdf417/encoder/PDF417.java index fc9819de9..9b89b29e6 100644 --- a/core/src/com/google/zxing/pdf417/encoder/PDF417.java +++ b/core/src/com/google/zxing/pdf417/encoder/PDF417.java @@ -535,35 +535,6 @@ final class PDF417 { return barcodeMatrix; } - /** - * Calculates the necessary number of rows as described in annex Q of ISO/IEC 15438:2001(E). - * - * @param m the number of source codewords prior to the additional of the Symbol Length - * Descriptor and any pad codewords - * @param k the number of error correction codewords - * @param c the number of columns in the symbol in the data region (excluding start, stop and - * row indicator codewords) - * @return the number of rows in the symbol (r) - */ - private static int getNumberOfRows(int m, int k, int c) throws WriterException { - int r = calculateNumberOfRows(m, k, c); - if (r > 90) { - throw new WriterException( - "The message doesn't fit in the configured symbol size." - + " The resultant number of rows for this barcode exceeds 90." - + " Please increase the number of columns or decrease the error correction" - + " level to reduce the number of rows."); - } - if (r < 2) { - throw new WriterException( - "The message is too short for the configured symbol size." - + " The resultant number of rows is less than 3." - + " Please decrease the number of columns or increase the error correction" - + " level to increase the number of rows."); - } - return r; - } - /** * Calculates the necessary number of rows as described in annex Q of ISO/IEC 15438:2001(E). * diff --git a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java index 4849e37b1..1748c2494 100644 --- a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java +++ b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java @@ -27,6 +27,11 @@ import java.util.EnumMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; /** * This simple command line utility decodes files, directories of files, or URIs which are passed @@ -48,7 +53,7 @@ public final class CommandLineRunner { } Config config = new Config(); - Inputs inputs = new Inputs(); + Queue inputs = new ConcurrentLinkedQueue(); for (String arg : args) { if ("--try_harder".equals(arg)) { @@ -74,11 +79,6 @@ public final class CommandLineRunner { crop[i] = Integer.parseInt(tokens[i]); } config.setCrop(crop); - } else if (arg.startsWith("--threads") && arg.length() >= 10) { - int threads = Integer.parseInt(arg.substring(10)); - if (threads > 1) { - config.setThreads(threads); - } } else if (arg.startsWith("-")) { System.err.println("Unknown command line option " + arg); printUsage(); @@ -93,19 +93,20 @@ public final class CommandLineRunner { } } - List threads = new ArrayList(config.getThreads()); - for (int x = 0; x < config.getThreads(); x++) { - DecodeThread thread = new DecodeThread(config, inputs); - threads.add(thread); - thread.start(); + int numThreads = Runtime.getRuntime().availableProcessors(); + ExecutorService executor = Executors.newFixedThreadPool(numThreads); + List> futures = new ArrayList>(numThreads); + for (int x = 0; x < numThreads; x++) { + futures.add(executor.submit(new DecodeWorker(config, inputs))); } + executor.shutdown(); int successful = 0; - for (int x = 0; x < config.getThreads(); x++) { - threads.get(x).join(); - successful += threads.get(x).getSuccessful(); + for (Future future : futures) { + successful += future.get(); } - int total = inputs.getInputCount(); + + int total = inputs.size(); if (total > 1) { System.out.println("\nDecoded " + successful + " files out of " + total + " successfully (" + (successful * 100 / total) + "%)\n"); @@ -114,7 +115,7 @@ public final class CommandLineRunner { // Build all the inputs up front into a single flat list, so the threads can atomically pull // paths/URLs off the queue. - private static void addArgumentToInputs(String argument, Config config, Inputs inputs) throws IOException { + private static void addArgumentToInputs(String argument, Config config, Queue inputs) throws IOException { File inputFile = new File(argument); if (inputFile.exists()) { if (inputFile.isDirectory()) { @@ -124,7 +125,7 @@ public final class CommandLineRunner { if (filename.startsWith(".")) { continue; } - // Recurse on nested directories if requested, otherwise skip them. + // Recur on nested directories if requested, otherwise skip them. if (singleFile.isDirectory()) { if (config.isRecursive()) { addArgumentToInputs(singleFile.getAbsolutePath(), config, inputs); @@ -135,13 +136,13 @@ public final class CommandLineRunner { if (filename.endsWith(".txt") || filename.contains(".mono.png")) { continue; } - inputs.addInput(singleFile.getCanonicalPath()); + inputs.add(singleFile.getCanonicalPath()); } } else { - inputs.addInput(inputFile.getCanonicalPath()); + inputs.add(inputFile.getCanonicalPath()); } } else { - inputs.addInput(argument); + inputs.add(argument); } } diff --git a/javase/src/com/google/zxing/client/j2se/Config.java b/javase/src/com/google/zxing/client/j2se/Config.java index 476999205..4e46566a5 100644 --- a/javase/src/com/google/zxing/client/j2se/Config.java +++ b/javase/src/com/google/zxing/client/j2se/Config.java @@ -32,7 +32,6 @@ final class Config { private boolean brief; private boolean recursive; private int[] crop; - private int threads = 1; Map getHints() { return hints; @@ -114,11 +113,4 @@ final class Config { this.crop = crop; } - int getThreads() { - return threads; - } - - void setThreads(int threads) { - this.threads = threads; - } } diff --git a/javase/src/com/google/zxing/client/j2se/DecodeThread.java b/javase/src/com/google/zxing/client/j2se/DecodeWorker.java similarity index 91% rename from javase/src/com/google/zxing/client/j2se/DecodeThread.java rename to javase/src/com/google/zxing/client/j2se/DecodeWorker.java index 3a6f0827e..271054ff4 100644 --- a/javase/src/com/google/zxing/client/j2se/DecodeThread.java +++ b/javase/src/com/google/zxing/client/j2se/DecodeWorker.java @@ -42,68 +42,58 @@ import java.io.Writer; import java.net.URI; import java.nio.charset.Charset; import java.util.Map; +import java.util.Queue; +import java.util.concurrent.Callable; /** * One of a pool of threads which pulls images off the Inputs queue and decodes them in parallel. * * @see CommandLineRunner */ -final class DecodeThread extends Thread { +final class DecodeWorker implements Callable { - private int successful; private final Config config; - private final Inputs inputs; + private final Queue inputs; - DecodeThread(Config config, Inputs inputs) { + DecodeWorker(Config config, Queue inputs) { this.config = config; this.inputs = inputs; } @Override - public void run() { - while (true) { - String input = inputs.getNextInput(); - if (input == null) { - break; - } - + public Integer call() throws IOException { + int successful = 0; + String input; + while ((input = inputs.poll()) != null) { File inputFile = new File(input); if (inputFile.exists()) { - try { - if (config.isMulti()) { - Result[] results = decodeMulti(inputFile.toURI(), config.getHints()); - if (results != null) { - successful++; - if (config.isDumpResults()) { - dumpResultMulti(inputFile, results); - } - } - } else { - Result result = decode(inputFile.toURI(), config.getHints()); - if (result != null) { - successful++; - if (config.isDumpResults()) { - dumpResult(inputFile, result); - } + if (config.isMulti()) { + Result[] results = decodeMulti(inputFile.toURI(), config.getHints()); + if (results != null) { + successful++; + if (config.isDumpResults()) { + dumpResultMulti(inputFile, results); + } + } + } else { + Result result = decode(inputFile.toURI(), config.getHints()); + if (result != null) { + successful++; + if (config.isDumpResults()) { + dumpResult(inputFile, result); } } - } catch (IOException e) { } } else { - try { - if (decode(new URI(input), config.getHints()) != null) { - successful++; - } - } catch (Exception e) { + if (decode(URI.create(input), config.getHints()) != null) { + successful++; } } } - } - - public int getSuccessful() { return successful; } + private static void dumpResult(File input, Result result) throws IOException { String name = input.getCanonicalPath(); int pos = name.lastIndexOf('.'); diff --git a/javase/src/com/google/zxing/client/j2se/Inputs.java b/javase/src/com/google/zxing/client/j2se/Inputs.java deleted file mode 100644 index 46dc37e51..000000000 --- a/javase/src/com/google/zxing/client/j2se/Inputs.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2011 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 java.util.ArrayList; -import java.util.List; - -/** - * Represents the collection of all images files/URLs to decode. - */ -final class Inputs { - - private final List inputs = new ArrayList(10); - private int position = 0; - - public synchronized void addInput(String pathOrUrl) { - inputs.add(pathOrUrl); - } - - public synchronized String getNextInput() { - if (position < inputs.size()) { - String result = inputs.get(position); - position++; - return result; - } else { - return null; - } - } - - public synchronized int getInputCount() { - return inputs.size(); - } -}