* decides which bucket of values corresponds to the black point -- which bucket contains the\r
* count of the brightest luminance values that should be considered "black".</p>\r
*\r
- * @param luminanceBuckets an array of <em>counts</em> of luminance values\r
+ * @param histogram an array of <em>counts</em> of luminance values\r
* @return index within argument of bucket corresponding to brightest values which should be\r
* considered "black"\r
*/\r
- public static int estimate(int[] luminanceBuckets) {\r
+ public static int estimate(int[] histogram) {\r
\r
- int numBuckets = luminanceBuckets.length;\r
+ int numBuckets = histogram.length;\r
\r
// Find tallest peak in histogram\r
int firstPeak = 0;\r
int firstPeakSize = 0;\r
for (int i = 0; i < numBuckets; i++) {\r
- if (luminanceBuckets[i] > firstPeakSize) {\r
+ if (histogram[i] > firstPeakSize) {\r
firstPeak = i;\r
- firstPeakSize = luminanceBuckets[i];\r
+ firstPeakSize = histogram[i];\r
}\r
}\r
\r
for (int i = 0; i < numBuckets; i++) {\r
int distanceToBiggest = i - firstPeak;\r
// Encourage more distant second peaks by multiplying by square of distance\r
- int score = luminanceBuckets[i] * distanceToBiggest * distanceToBiggest;\r
+ int score = histogram[i] * distanceToBiggest * distanceToBiggest;\r
if (score > secondPeakScore) {\r
secondPeak = i;\r
secondPeakScore = score;\r
secondPeak = temp;\r
}\r
\r
- // Find a valley between them that is low and close to the midpoint of the two peaks\r
- int bestValley = firstPeak;\r
- int bestValleyScore = 0;\r
- for (int i = firstPeak + 1; i < secondPeak; i++) {\r
- // Encourage low valleys near the mid point between peaks\r
- int score = (firstPeakSize - luminanceBuckets[i]) * (i - firstPeak) * (secondPeak - i);\r
- if (score > bestValleyScore) {\r
+ // Find a valley between them that is low and closer to the white peak\r
+ int bestValley = secondPeak;\r
+ int bestValleyScore = Integer.MAX_VALUE;\r
+ for (int i = secondPeak; i > firstPeak; i--) {\r
+ int distance = secondPeak - i + 3;\r
+ int score = distance * histogram[i];\r
+ if (score < bestValleyScore) {\r
bestValley = i;\r
bestValleyScore = score;\r
}\r
package com.google.zxing.upc;
+import com.google.zxing.BlackPointEstimationMethod;
import com.google.zxing.common.BitArray;
import com.google.zxing.MonochromeBitmapSource;
int found = -1;
for (int x = 0; x < BITMAP_SEARCH_PATTERN.length; x++) {
int row = height * BITMAP_SEARCH_PATTERN[x] / 100;
+ bitmap.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, row);
bitmap.getBlackRow(row, rowData, 0, width);
if (decodeRow(rowData)) {
private final int height;
private int blackPoint;
private BlackPointEstimationMethod lastMethod;
+ private int lastArgument;
+
+ private static final int LUMINANCE_BITS = 5;
+ private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
+ private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
LCDUIImageMonochromeBitmapSource(final Image image) {
width = image.getWidth();
rgbPixels = new int[width * height];
image.getRGB(rgbPixels, 0, width, 0, 0, width, height);
blackPoint = 0x7F;
+ lastMethod = null;
+ lastArgument = 0;
}
public boolean isBlack(int x, int y) {
}
public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
- if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
- if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(lastMethod)) {
- int[] luminanceBuckets = new int[32];
+ if (!method.equals(lastMethod) || argument != lastArgument) {
+ int[] histogram = new int[LUMINANCE_BUCKETS];
+ if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
int minDimension = width < height ? width : height;
for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) {
- luminanceBuckets[computeRGBLuminance(rgbPixels[offset]) >> 3]++;
+ histogram[computeRGBLuminance(rgbPixels[offset]) >> LUMINANCE_SHIFT]++;
}
- blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3;
+ } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
+ if (argument < 0 || argument >= height) {
+ throw new IllegalArgumentException("Row is not within the image: " + argument);
+ }
+ int offset = argument * width;
+ for (int x = 0; x < width; x++) {
+ histogram[computeRGBLuminance(rgbPixels[offset + x]) >> LUMINANCE_SHIFT]++;
+ }
+ } else {
+ throw new IllegalArgumentException("Unknown method: " + method);
}
- } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
- // TODO
- } else {
- throw new IllegalArgumentException("Unknown method: " + method);
+ blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
+ lastMethod = method;
+ lastArgument = argument;
}
- lastMethod = method;
}
public BlackPointEstimationMethod getLastEstimationMethod() {
private final BufferedImage image;
private int blackPoint;
private BlackPointEstimationMethod lastMethod;
+ private int lastArgument;
+
+ private static final int LUMINANCE_BITS = 5;
+ private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
+ private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
public BufferedImageMonochromeBitmapSource(BufferedImage image) {
this.image = image;
blackPoint = 0x7F;
+ lastMethod = null;
+ lastArgument = 0;
}
public boolean isBlack(int x, int y) {
}
public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
- if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
- if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(lastMethod)) {
- int width = image.getWidth();
- int height = image.getHeight();
- int[] luminanceBuckets = new int[32];
+ if (!method.equals(lastMethod) || argument != lastArgument) {
+ int width = image.getWidth();
+ int height = image.getHeight();
+ int[] histogram = new int[LUMINANCE_BUCKETS];
+ if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
int minDimension = width < height ? width : height;
int startI = height == minDimension ? 0 : (height - width) >> 1;
int startJ = width == minDimension ? 0 : (width - height) >> 1;
for (int n = 0; n < minDimension; n++) {
int pixel = image.getRGB(startJ + n, startI + n);
- luminanceBuckets[computeRGBLuminance(pixel) >> 3]++;
+ histogram[computeRGBLuminance(pixel) >> LUMINANCE_SHIFT]++;
}
- blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3;
+ } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
+ if (argument < 0 || argument >= height) {
+ throw new IllegalArgumentException("Row is not within the image: " + argument);
+ }
+ int[] rgbArray = new int[width];
+ image.getRGB(0, argument, width, 1, rgbArray, 0, width);
+ for (int x = 0; x < width; x++) {
+ histogram[computeRGBLuminance(rgbArray[x]) >> LUMINANCE_SHIFT]++;
+ }
+ } else {
+ throw new IllegalArgumentException("Unknown method: " + method);
}
- } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
- // TODO
- } else {
- throw new IllegalArgumentException("Unknown method: " + method);
+ blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
+ lastMethod = method;
+ lastArgument = argument;
}
- lastMethod = method;
}
public BlackPointEstimationMethod getLastEstimationMethod() {
File inputFile = new File(args[0]);
if (inputFile.exists()) {
if (inputFile.isDirectory()) {
+ int successful = 0;
for (File input : inputFile.listFiles()) {
- decode(input.toURI());
+ if (decode(input.toURI())) successful++;
}
+ System.out.println("Decoded " + successful + " files successfully");
} else {
decode(inputFile.toURI());
}
}
}
- private static void decode(URI uri) throws IOException {
+ private static boolean decode(URI uri) throws IOException {
BufferedImage image = ImageIO.read(uri.toURL());
if (image == null) {
System.err.println(uri.toString() + ": Could not load image");
- return;
+ return false;
}
try {
String result = new MultiFormatReader().decode(new BufferedImageMonochromeBitmapSource(image)).getText();
System.out.println(uri.toString() + ": " + result);
+ return true;
} catch (ReaderException e) {
System.out.println(uri.toString() + ": No barcode found");
+ return false;
}
}