Some small improvements in error handling based on exceptions observed at zxing.org
[zxing.git] / core / src / com / google / zxing / qrcode / QRCodeReader.java
index 25ecece..4f5105e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * Copyright 2007 ZXing authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
 import com.google.zxing.ResultPoint;
 import com.google.zxing.common.BitMatrix;
+import com.google.zxing.common.DecoderResult;
 import com.google.zxing.common.DetectorResult;
 import com.google.zxing.qrcode.decoder.Decoder;
 import com.google.zxing.qrcode.detector.Detector;
@@ -53,18 +54,18 @@ public final class QRCodeReader implements Reader {
 
   public Result decode(MonochromeBitmapSource image, Hashtable hints)
       throws ReaderException {
-    String text;
+    DecoderResult decoderResult;
     ResultPoint[] points;
     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
       BitMatrix bits = extractPureBits(image);
-      text = decoder.decode(bits);
+      decoderResult = decoder.decode(bits);
       points = NO_POINTS;
     } else {
-      DetectorResult result = new Detector(image).detect();
-      text = decoder.decode(result.getBits());
+      DetectorResult result = new Detector(image).detect(hints);
+      decoderResult = decoder.decode(result.getBits());
       points = result.getPoints();
     }
-    return new Result(text, points, BarcodeFormat.QR_CODE);
+    return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
   }
 
   /**
@@ -76,7 +77,9 @@ public final class QRCodeReader implements Reader {
   private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
     // Now need to determine module size in pixels
 
-    int minDimension = Math.min(image.getHeight(), image.getWidth());
+    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;
@@ -99,7 +102,7 @@ public final class QRCodeReader implements Reader {
     int moduleSize = moduleEnd - borderWidth;
 
     // And now find where the rightmost black module on the first row ends
-    int rowEndOfSymbol = image.getWidth() - 1;
+    int rowEndOfSymbol = width - 1;
     while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
       rowEndOfSymbol--;
     }
@@ -120,6 +123,11 @@ public final class QRCodeReader implements Reader {
     // little off, this will help recover.
     borderWidth += moduleSize >> 1;
 
+    int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
+    if (sampleDimension >= width || sampleDimension >= height) {
+      throw new ReaderException("Estimated pure image size is beyond image boundaries");
+    }
+
     // Now just read off the bits
     BitMatrix bits = new BitMatrix(dimension);
     for (int i = 0; i < dimension; i++) {