Alternate multi QR Code reader from Hannes
[zxing.git] / core / src / com / google / zxing / qrcode / QRCodeReader.java
index c36fe87..f3ddb54 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.
@@ -23,6 +23,7 @@ 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.common.BitMatrix;
 import com.google.zxing.common.DecoderResult;
 import com.google.zxing.common.DetectorResult;
@@ -34,14 +35,18 @@ import java.util.Hashtable;
 /**
  * This implementation can detect and decode QR Codes in an image.
  *
- * @author srowen@google.com (Sean Owen)
+ * @author Sean Owen
  */
-public final class QRCodeReader implements Reader {
+public class QRCodeReader implements Reader {
 
   private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
 
   private final Decoder decoder = new Decoder();
 
+  protected Decoder getDecoder() {
+    return decoder;
+  }
+
   /**
    * Locates and decodes a QR code in an image.
    *
@@ -61,11 +66,16 @@ public final class QRCodeReader implements Reader {
       decoderResult = decoder.decode(bits);
       points = NO_POINTS;
     } else {
-      DetectorResult result = new Detector(image).detect(hints);
-      decoderResult = decoder.decode(result.getBits());
-      points = result.getPoints();
+      DetectorResult detectorResult = new Detector(image).detect(hints);
+      decoderResult = decoder.decode(detectorResult.getBits());
+      points = detectorResult.getPoints();
+    }
+
+    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
+    if (decoderResult.getByteSegments() != null) {
+      result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
     }
-    return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
+    return result;
   }
 
   /**
@@ -87,7 +97,7 @@ public final class QRCodeReader implements Reader {
       borderWidth++;
     }
     if (borderWidth == minDimension) {
-      throw new ReaderException("No black pixels found along diagonal");
+      throw ReaderException.getInstance();
     }
 
     // And then keep tracking across the top-left black module to determine module size
@@ -96,7 +106,7 @@ public final class QRCodeReader implements Reader {
       moduleEnd++;
     }
     if (moduleEnd == minDimension) {
-      throw new ReaderException("No end to black pixels found along diagonal");
+      throw ReaderException.getInstance();
     }
 
     int moduleSize = moduleEnd - borderWidth;
@@ -107,14 +117,13 @@ public final class QRCodeReader implements Reader {
       rowEndOfSymbol--;
     }
     if (rowEndOfSymbol < 0) {
-      throw new ReaderException("Can't find end of rightmost black module");
+      throw ReaderException.getInstance();
     }
     rowEndOfSymbol++;
 
     // Make sure width of barcode is a multiple of module size
     if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
-      throw new ReaderException("Bad module size / width: " + moduleSize +
-          " / " + (rowEndOfSymbol - borderWidth));
+      throw ReaderException.getInstance();
     }
     int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
 
@@ -125,7 +134,7 @@ public final class QRCodeReader implements Reader {
 
     int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
     if (sampleDimension >= width || sampleDimension >= height) {
-      throw new ReaderException("Estimated pure image size is beyond image boundaries");
+      throw ReaderException.getInstance();
     }
 
     // Now just read off the bits