Pure-barcode mode can now deal with asymmetric borders instead of assuming it's pure...
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 6 Apr 2010 21:30:53 +0000 (21:30 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 6 Apr 2010 21:30:53 +0000 (21:30 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1288 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/common/BitMatrix.java
core/src/com/google/zxing/datamatrix/DataMatrixReader.java
core/src/com/google/zxing/pdf417/PDF417Reader.java
core/src/com/google/zxing/qrcode/QRCodeReader.java

index f1023b8..d1e6974 100755 (executable)
@@ -145,6 +145,31 @@ public final class BitMatrix {
     return row;\r
   }\r
 \r
+  /**\r
+   * This is useful in detecting a corner of a 'pure' barcode.\r
+   * \r
+   * @return {x,y} coordinate of top-left-most 1 bit, or null if it is all white\r
+   */\r
+  public int[] getTopLeftOnBit() {\r
+    int bitsOffset = 0;\r
+    while (bitsOffset < bits.length && bits[bitsOffset] == 0) {\r
+      bitsOffset++;\r
+    }\r
+    if (bitsOffset == bits.length) {\r
+      return null;\r
+    }\r
+    int y = bitsOffset / rowSize;\r
+    int x = (bitsOffset % rowSize) << 5;\r
+    \r
+    int theBits = bits[bitsOffset];\r
+    int bit = 0;\r
+    while ((theBits << (31-bit)) == 0) {\r
+      bit++;\r
+    }\r
+    x += bit;\r
+    return new int[] {x, y};\r
+  }\r
+\r
   /**\r
    * @return The width of the matrix\r
    */\r
index 0154917..477a989 100644 (file)
@@ -89,66 +89,67 @@ public final class DataMatrixReader implements Reader {
    * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border
    * around it. This is a specialized method that works exceptionally fast in this special
    * case.
+   *
+   * @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix) 
    */
   private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
-    // Now need to determine module size in pixels
 
     int height = image.getHeight();
     int width = image.getWidth();
     int minDimension = Math.min(height, width);
 
-    // First, skip white border by tracking diagonally from the top left down and to the right:
-    int borderWidth = 0;
-    while (borderWidth < minDimension && !image.get(borderWidth, borderWidth)) {
-      borderWidth++;
-    }
-    if (borderWidth == minDimension) {
+    // And then keep tracking across the top-left black module to determine module size
+    //int moduleEnd = borderWidth;
+    int[] leftTopBlack = image.getTopLeftOnBit();
+    if (leftTopBlack == null) {
       throw NotFoundException.getNotFoundInstance();
     }
-
-    // And then keep tracking across the top-left black module to determine module size
-    int moduleEnd = borderWidth + 1;
-    while (moduleEnd < width && image.get(moduleEnd, borderWidth)) {
-      moduleEnd++;
+    int x = leftTopBlack[0];
+    int y = leftTopBlack[1];
+    while (x < minDimension && y < minDimension && image.get(x, y)) {
+      x++;
     }
-    if (moduleEnd == width) {
+    if (x == minDimension) {
       throw NotFoundException.getNotFoundInstance();
     }
 
-    int moduleSize = moduleEnd - borderWidth;
+    int moduleSize = x - leftTopBlack[0];
 
-    // And now find where the bottommost black module on the first column ends
-    int columnEndOfSymbol = height - 1;
-    while (columnEndOfSymbol >= 0 && !image.get(borderWidth, columnEndOfSymbol)) {
-       columnEndOfSymbol--;
+    // And now find where the rightmost black module on the first row ends
+    int rowEndOfSymbol = width - 1;
+    while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, y)) {
+      rowEndOfSymbol--;
     }
-    if (columnEndOfSymbol < 0) {
+    if (rowEndOfSymbol < 0) {
       throw NotFoundException.getNotFoundInstance();
     }
-    columnEndOfSymbol++;
+    rowEndOfSymbol++;
 
     // Make sure width of barcode is a multiple of module size
-    if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {
+    if ((rowEndOfSymbol - x) % moduleSize != 0) {
       throw NotFoundException.getNotFoundInstance();
     }
-    int dimension = (columnEndOfSymbol - borderWidth) / moduleSize;
+    int dimension = 2 + ((rowEndOfSymbol - x) / moduleSize);
 
+    y += moduleSize;
+    
     // Push in the "border" by half the module width so that we start
     // sampling in the middle of the module. Just in case the image is a
     // little off, this will help recover.
-    borderWidth += moduleSize >> 1;
+    x -= moduleSize >> 1;
+    y -= moduleSize >> 1;
 
-    int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
-    if (sampleDimension >= width || sampleDimension >= height) {
+    if ((x + (dimension - 1) * moduleSize) >= width ||
+        (y + (dimension - 1) * moduleSize) >= height) {
       throw NotFoundException.getNotFoundInstance();
     }
 
     // Now just read off the bits
     BitMatrix bits = new BitMatrix(dimension);
     for (int i = 0; i < dimension; i++) {
-      int iOffset = borderWidth + i * moduleSize;
+      int iOffset = y + i * moduleSize;
       for (int j = 0; j < dimension; j++) {
-        if (image.get(borderWidth + j * moduleSize, iOffset)) {
+        if (image.get(x + j * moduleSize, iOffset)) {
           bits.set(j, i);
         }
       }
index 1fcd1f1..f362ff1 100644 (file)
@@ -29,6 +29,7 @@ import com.google.zxing.common.DecoderResult;
 import com.google.zxing.common.DetectorResult;\r
 import com.google.zxing.pdf417.decoder.Decoder;\r
 import com.google.zxing.pdf417.detector.Detector;\r
+import com.google.zxing.qrcode.QRCodeReader;\r
 \r
 import java.util.Hashtable;\r
 \r
@@ -59,7 +60,7 @@ public final class PDF417Reader implements Reader {
     DecoderResult decoderResult;\r
     ResultPoint[] points;\r
     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {\r
-      BitMatrix bits = extractPureBits(image);\r
+      BitMatrix bits = QRCodeReader.extractPureBits(image.getBlackMatrix());\r
       decoderResult = decoder.decode(bits);\r
       points = NO_POINTS;\r
     } else {\r
@@ -75,75 +76,4 @@ public final class PDF417Reader implements Reader {
     // do nothing\r
   }\r
 \r
-  /**\r
-   * This method detects a barcode in a "pure" image -- that is, pure monochrome image\r
-   * which contains only an unrotated, unskewed, image of a barcode, with some white border\r
-   * around it. This is a specialized method that works exceptionally fast in this special\r
-   * case.\r
-   */\r
-  private static BitMatrix extractPureBits(BinaryBitmap image) throws NotFoundException {\r
-    // Now need to determine module size in pixels\r
-    BitMatrix matrix = image.getBlackMatrix();\r
-    int height = matrix.getHeight();\r
-    int width = matrix.getWidth();\r
-    int minDimension = Math.min(height, width);\r
-\r
-    // First, skip white border by tracking diagonally from the top left down and to the right:\r
-    int borderWidth = 0;\r
-    while (borderWidth < minDimension && !matrix.get(borderWidth, borderWidth)) {\r
-      borderWidth++;\r
-    }\r
-    if (borderWidth == minDimension) {\r
-      throw NotFoundException.getNotFoundInstance();\r
-    }\r
-\r
-    // And then keep tracking across the top-left black module to determine module size\r
-    int moduleEnd = borderWidth;\r
-    while (moduleEnd < minDimension && matrix.get(moduleEnd, moduleEnd)) {\r
-      moduleEnd++;\r
-    }\r
-    if (moduleEnd == minDimension) {\r
-      throw NotFoundException.getNotFoundInstance();\r
-    }\r
-\r
-    int moduleSize = moduleEnd - borderWidth;\r
-\r
-    // And now find where the rightmost black module on the first row ends\r
-    int rowEndOfSymbol = width - 1;\r
-    while (rowEndOfSymbol >= 0 && !matrix.get(rowEndOfSymbol, borderWidth)) {\r
-      rowEndOfSymbol--;\r
-    }\r
-    if (rowEndOfSymbol < 0) {\r
-      throw NotFoundException.getNotFoundInstance();\r
-    }\r
-    rowEndOfSymbol++;\r
-\r
-    // Make sure width of barcode is a multiple of module size\r
-    if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {\r
-      throw NotFoundException.getNotFoundInstance();\r
-    }\r
-    int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;\r
-\r
-    // Push in the "border" by half the module width so that we start\r
-    // sampling in the middle of the module. Just in case the image is a\r
-    // little off, this will help recover.\r
-    borderWidth += moduleSize >> 1;\r
-\r
-    int sampleDimension = borderWidth + (dimension - 1) * moduleSize;\r
-    if (sampleDimension >= width || sampleDimension >= height) {\r
-      throw NotFoundException.getNotFoundInstance();\r
-    }\r
-\r
-    // Now just read off the bits\r
-    BitMatrix bits = new BitMatrix(dimension);\r
-    for (int y = 0; y < dimension; y++) {\r
-      int iOffset = borderWidth + y * moduleSize;\r
-      for (int x = 0; x < dimension; x++) {\r
-        if (matrix.get(borderWidth + x * moduleSize, iOffset)) {\r
-          bits.set(x, y);\r
-        }\r
-      }\r
-    }\r
-    return bits;\r
-  }\r
 }\r
index d9abc93..296f27d 100644 (file)
@@ -95,36 +95,33 @@ public class QRCodeReader implements Reader {
    * around it. This is a specialized method that works exceptionally fast in this special
    * case.
    */
-  private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
-    // Now need to determine module size in pixels
+  public static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
 
     int height = image.getHeight();
     int width = image.getWidth();
     int minDimension = Math.min(height, width);
 
-    // First, skip white border by tracking diagonally from the top left down and to the right:
-    int borderWidth = 0;
-    while (borderWidth < minDimension && !image.get(borderWidth, borderWidth)) {
-      borderWidth++;
-    }
-    if (borderWidth == minDimension) {
+    // And then keep tracking across the top-left black module to determine module size
+    //int moduleEnd = borderWidth;
+    int[] leftTopBlack = image.getTopLeftOnBit();
+    if (leftTopBlack == null) {
       throw NotFoundException.getNotFoundInstance();
     }
-
-    // And then keep tracking across the top-left black module to determine module size
-    int moduleEnd = borderWidth;
-    while (moduleEnd < minDimension && image.get(moduleEnd, moduleEnd)) {
-      moduleEnd++;
+    int x = leftTopBlack[0];
+    int y = leftTopBlack[1];
+    while (x < minDimension && y < minDimension && image.get(x, y)) {
+      x++;
+      y++;
     }
-    if (moduleEnd == minDimension) {
+    if (x == minDimension || y == minDimension) {
       throw NotFoundException.getNotFoundInstance();
     }
 
-    int moduleSize = moduleEnd - borderWidth;
+    int moduleSize = x - leftTopBlack[0];
 
     // And now find where the rightmost black module on the first row ends
     int rowEndOfSymbol = width - 1;
-    while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, borderWidth)) {
+    while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, y)) {
       rowEndOfSymbol--;
     }
     if (rowEndOfSymbol < 0) {
@@ -133,27 +130,28 @@ public class QRCodeReader implements Reader {
     rowEndOfSymbol++;
 
     // Make sure width of barcode is a multiple of module size
-    if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
+    if ((rowEndOfSymbol - x) % moduleSize != 0) {
       throw NotFoundException.getNotFoundInstance();
     }
-    int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
+    int dimension = 1 + ((rowEndOfSymbol - x) / moduleSize);
 
     // Push in the "border" by half the module width so that we start
     // sampling in the middle of the module. Just in case the image is a
     // little off, this will help recover.
-    borderWidth += moduleSize >> 1;
+    x -= moduleSize >> 1;
+    y -= moduleSize >> 1;
 
-    int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
-    if (sampleDimension >= width || sampleDimension >= height) {
+    if ((x + (dimension - 1) * moduleSize) >= width ||
+        (y + (dimension - 1) * moduleSize) >= height) {
       throw NotFoundException.getNotFoundInstance();
     }
 
     // Now just read off the bits
     BitMatrix bits = new BitMatrix(dimension);
     for (int i = 0; i < dimension; i++) {
-      int iOffset = borderWidth + i * moduleSize;
+      int iOffset = y + i * moduleSize;
       for (int j = 0; j < dimension; j++) {
-        if (image.get(borderWidth + j * moduleSize, iOffset)) {
+        if (image.get(x + j * moduleSize, iOffset)) {
           bits.set(j, i);
         }
       }