Merged revisions 321,327,330,332,334,342-343,352-353,355-358,361-363,365,372 via...
[zxing.git] / android / src / com / google / zxing / client / android / RGBMonochromeBitmapSource.java
index 7461d0a..d84a920 100755 (executable)
@@ -24,10 +24,7 @@ import com.google.zxing.common.BitArray;
 import com.google.zxing.common.BlackPointEstimator;
 
 /**
- * This object implements MonochromeBitmapSource around an Android Bitmap. Rather than capturing an
- * RGB image and calculating the grey value at each pixel, we ask the camera driver for YUV data and
- * strip out the luminance channel directly. This should be faster but provides fewer bits, i.e.
- * fewer grey levels.
+ * This object implements MonochromeBitmapSource around an Android Bitmap.
  *
  * @author dswitkin@google.com (Daniel Switkin)
  * @author srowen@google.com (Sean Owen)
@@ -61,10 +58,29 @@ final class RGBMonochromeBitmapSource implements MonochromeBitmapSource {
       row.clear();
     }
     int[] pixelRow = new int[getWidth];
-     image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
-    for (int i = 0; i < getWidth; i++) {
-      if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
-        row.set(i);
+    image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
+
+    // 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 == BlackPointEstimationMethod.ROW_SAMPLING) {
+      int left = computeRGBLuminance(pixelRow[0]);
+      int center = computeRGBLuminance(pixelRow[1]);
+      for (int i = 1; i < getWidth - 1; i++) {
+        int right = computeRGBLuminance(pixelRow[i + 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; i < getWidth; i++) {
+        if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
+          row.set(i);
+        }
       }
     }
     return row;
@@ -124,6 +140,9 @@ final class RGBMonochromeBitmapSource implements MonochromeBitmapSource {
   /**
    * An optimized approximation of a more proper conversion from RGB to luminance which
    * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
+   *
+   * @param pixel An ARGB input pixel
+   * @return An eight bit luminance value
    */
   private static int computeRGBLuminance(int pixel) {
     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that