"Split" ReaderException into subclasses to enable more useful error reporting
[zxing.git] / core / src / com / google / zxing / qrcode / QRCodeReader.java
index 604d103..d9abc93 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.MonochromeBitmapSource;
+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.ResultPoint;
 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.common.DecoderResult;
 import com.google.zxing.common.DetectorResult;
@@ -51,23 +53,25 @@ 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(MonochromeBitmapSource image) throws ReaderException {
+  public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException {
     return decode(image, null);
   }
 
-  public Result decode(MonochromeBitmapSource image, Hashtable hints)
-      throws ReaderException {
+  public Result decode(BinaryBitmap image, Hashtable hints)
+      throws NotFoundException, ChecksumException, FormatException {
     DecoderResult decoderResult;
     ResultPoint[] points;
     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
-      BitMatrix bits = extractPureBits(image);
-      decoderResult = decoder.decode(bits);
+      BitMatrix bits = extractPureBits(image.getBlackMatrix());
+      decoderResult = decoder.decode(bits, hints);
       points = NO_POINTS;
     } else {
-      DetectorResult detectorResult = new Detector(image).detect(hints);
-      decoderResult = decoder.decode(detectorResult.getBits());
+      DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
+      decoderResult = decoder.decode(detectorResult.getBits(), hints);
       points = detectorResult.getPoints();
     }
 
@@ -75,16 +79,23 @@ public class QRCodeReader implements Reader {
     if (decoderResult.getByteSegments() != null) {
       result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
     }
+    if (decoderResult.getECLevel() != null) {
+      result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
+    }
     return result;
   }
 
+  public void reset() {
+    // do nothing
+  }
+
   /**
    * This method detects a barcode in a "pure" image -- that is, pure monochrome image
    * which contains only an unrotated, unskewed, image of a barcode, with some white border
    * around it. This is a specialized method that works exceptionally fast in this special
    * case.
    */
-  private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
+  private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
     // Now need to determine module size in pixels
 
     int height = image.getHeight();
@@ -93,37 +104,37 @@ public class QRCodeReader implements Reader {
 
     // First, skip white border by tracking diagonally from the top left down and to the right:
     int borderWidth = 0;
-    while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
+    while (borderWidth < minDimension && !image.get(borderWidth, borderWidth)) {
       borderWidth++;
     }
     if (borderWidth == minDimension) {
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
 
     // And then keep tracking across the top-left black module to determine module size
     int moduleEnd = borderWidth;
-    while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {
+    while (moduleEnd < minDimension && image.get(moduleEnd, moduleEnd)) {
       moduleEnd++;
     }
     if (moduleEnd == minDimension) {
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
 
     int moduleSize = moduleEnd - borderWidth;
 
     // And now find where the rightmost black module on the first row ends
     int rowEndOfSymbol = width - 1;
-    while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
+    while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, borderWidth)) {
       rowEndOfSymbol--;
     }
     if (rowEndOfSymbol < 0) {
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
     rowEndOfSymbol++;
 
     // Make sure width of barcode is a multiple of module size
     if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
     int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
 
@@ -134,7 +145,7 @@ public class QRCodeReader implements Reader {
 
     int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
     if (sampleDimension >= width || sampleDimension >= height) {
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
 
     // Now just read off the bits
@@ -142,7 +153,7 @@ public class QRCodeReader implements Reader {
     for (int i = 0; i < dimension; i++) {
       int iOffset = borderWidth + i * moduleSize;
       for (int j = 0; j < dimension; j++) {
-        if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {
+        if (image.get(borderWidth + j * moduleSize, iOffset)) {
           bits.set(j, i);
         }
       }