Small style stuff
[zxing.git] / core / src / com / google / zxing / qrcode / QRCodeReader.java
index 52002ae..2a12c6d 100644 (file)
 package com.google.zxing.qrcode;
 
 import com.google.zxing.BarcodeFormat;
+import com.google.zxing.BinaryBitmap;
+import com.google.zxing.ChecksumException;
 import com.google.zxing.DecodeHintType;
+import com.google.zxing.FormatException;
+import com.google.zxing.NotFoundException;
 import com.google.zxing.Reader;
-import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
-import com.google.zxing.ResultPoint;
 import com.google.zxing.ResultMetadataType;
-import com.google.zxing.BinaryBitmap;
+import com.google.zxing.ResultPoint;
 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.common.DecoderResult;
 import com.google.zxing.common.DetectorResult;
@@ -51,14 +53,16 @@ public class QRCodeReader implements Reader {
    * Locates and decodes a QR code in an image.
    *
    * @return a String representing the content encoded by the QR code
-   * @throws ReaderException if a QR code cannot be found, or cannot be decoded
+   * @throws NotFoundException if a QR code cannot be found
+   * @throws FormatException if a QR code cannot be decoded
+   * @throws ChecksumException if error correction fails
    */
-  public Result decode(BinaryBitmap image) throws ReaderException {
+  public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException {
     return decode(image, null);
   }
 
   public Result decode(BinaryBitmap image, Hashtable hints)
-      throws ReaderException {
+      throws NotFoundException, ChecksumException, FormatException {
     DecoderResult decoderResult;
     ResultPoint[] points;
     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
@@ -91,65 +95,67 @@ 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 ReaderException {
-    // 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) {
-      throw ReaderException.getInstance();
-    }
-
     // 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 moduleEnd = borderWidth;
+    int[] leftTopBlack = image.getTopLeftOnBit();
+    if (leftTopBlack == null) {
+      throw NotFoundException.getNotFoundInstance();
+    }
+    int x = leftTopBlack[0];
+    int y = leftTopBlack[1];
+    while (x < minDimension && y < minDimension && image.get(x, y)) {
+      x++;
+      y++;
     }
-    if (moduleEnd == minDimension) {
-      throw ReaderException.getInstance();
+    if (x == minDimension || y == minDimension) {
+      throw NotFoundException.getNotFoundInstance();
     }
 
-    int moduleSize = moduleEnd - borderWidth;
+    int moduleSize = x - leftTopBlack[0];
+    if (moduleSize == 0) {
+      throw NotFoundException.getNotFoundInstance();
+    }
 
     // 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 > x && !image.get(rowEndOfSymbol, y)) {
       rowEndOfSymbol--;
     }
-    if (rowEndOfSymbol < 0) {
-      throw ReaderException.getInstance();
+    if (rowEndOfSymbol <= x) {
+      throw NotFoundException.getNotFoundInstance();
     }
     rowEndOfSymbol++;
 
     // Make sure width of barcode is a multiple of module size
-    if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
-      throw ReaderException.getInstance();
+    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;
-
-    int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
-    if (sampleDimension >= width || sampleDimension >= height) {
-      throw ReaderException.getInstance();
+    // little off, this will help recover. Need to back up at least 1.
+    int backOffAmount = moduleSize == 1 ? 1 : moduleSize >> 1;
+    x -= backOffAmount;
+    y -= backOffAmount;
+
+    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);
         }
       }
@@ -157,4 +163,4 @@ public class QRCodeReader implements Reader {
     return bits;
   }
 
-}
\ No newline at end of file
+}