import com.google.zxing.BlackPointEstimationMethod;
import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.ReaderException;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.BlackPointEstimator;
}
public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
- if (row == null) {
+ if (row == null || row.getSize() < getWidth) {
row = new BitArray(getWidth);
} else {
row.clear();
}
- for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
- if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
- row.set(i);
+
+ // If the current decoder calculated the blackPoint based on one row, assume we're trying to
+ // decode a 1D barcode, and apply some sharpening.
+ // TODO: We may want to add a fifth parameter to request the amount of shapening to be done.
+ if (lastMethod.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
+ int offset = y * width + startX;
+ int left = computeRGBLuminance(rgbPixels[offset]);
+ offset++;
+ int center = computeRGBLuminance(rgbPixels[offset]);
+ for (int i = 1; i < getWidth - 1; i++, offset++) {
+ int right = computeRGBLuminance(rgbPixels[offset + 1]);
+ // Simple -1 4 -1 box filter with a weight of 2
+ int luminance = ((center << 2) - left - right) >> 1;
+ if (luminance < blackPoint) {
+ row.set(i);
+ }
+ left = center;
+ center = right;
+ }
+ } else {
+ for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
+ if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
+ row.set(i);
+ }
}
}
return row;
return width;
}
- public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
+ public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
if (!method.equals(lastMethod) || argument != lastArgument) {
int[] histogram = new int[LUMINANCE_BUCKETS];
- float biasTowardsWhite = 1.0f;
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) {
if (argument < 0 || argument >= height) {
throw new IllegalArgumentException("Row is not within the image: " + argument);
}
- biasTowardsWhite = 2.0f;
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);
}
- blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
+ blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
lastMethod = method;
lastArgument = argument;
}
// corrupts the conversion. Not significant for our purposes.
//
// But we can get even cleverer and eliminate a few shifts:
- return (((pixel & 0x00FF0000) >> 8) +
- ((pixel & 0x0000FF00) << 1) +
- ((pixel & 0x000000FF) << 8)) >> 10;
+ return (((pixel & 0x00FF0000) >> 16) +
+ ((pixel & 0x0000FF00) >> 7) +
+ ( pixel & 0x000000FF )) >> 2;
}
}
\ No newline at end of file