Refactorings to allow raw bytes to be passed back with reader result, where applicable
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 12 Mar 2008 18:51:46 +0000 (18:51 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 12 Mar 2008 18:51:46 +0000 (18:51 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@270 59b500cc-1b3d-0410-9834-0bbf25fbcc57

15 files changed:
android/src/com/google/zxing/client/android/BarcodeReaderCaptureActivity.java
core/src/com/google/zxing/Result.java
core/src/com/google/zxing/client/result/ParsedReaderResult.java
core/src/com/google/zxing/common/DecoderResult.java [new file with mode: 0644]
core/src/com/google/zxing/datamatrix/DataMatrixReader.java
core/src/com/google/zxing/datamatrix/decoder/Decoder.java
core/src/com/google/zxing/oned/AbstractUPCEANReader.java
core/src/com/google/zxing/oned/Code128Reader.java
core/src/com/google/zxing/oned/Code39Reader.java
core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java
core/src/com/google/zxing/qrcode/QRCodeReader.java
core/src/com/google/zxing/qrcode/decoder/Decoder.java
core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java
javame/src/com/google/zxing/client/j2me/SnapshotThread.java
javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java

index 52c5d68..aefc7b5 100644 (file)
@@ -177,9 +177,9 @@ public final class BarcodeReaderCaptureActivity extends Activity {
   }
 
   private static ParsedReaderResult parseReaderResult(Result rawResult) {
-    String rawText = rawResult.getText();
-    ParsedReaderResult readerResult = ParsedReaderResult.parseReaderResult(rawText);
+    ParsedReaderResult readerResult = ParsedReaderResult.parseReaderResult(rawResult);
     if (readerResult.getType().equals(ParsedReaderResultType.TEXT)) {
+      String rawText = rawResult.getText();
       AndroidIntentParsedResult androidResult = AndroidIntentParsedResult.parse(rawText);
       if (androidResult != null) {
         Intent intent = androidResult.getIntent();
index d63743f..b9d0f5a 100644 (file)
@@ -24,22 +24,31 @@ package com.google.zxing;
 public final class Result {
 
   private final String text;
+  private final byte[] rawBytes;
   private final ResultPoint[] resultPoints;
   private final BarcodeFormat format;
 
-  public Result(String text, ResultPoint[] resultPoints, BarcodeFormat format) {
+  public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format) {
     this.text = text;
+    this.rawBytes = rawBytes;
     this.resultPoints = resultPoints;
     this.format = format;
   }
 
   /**
-   * @return raw text encoded by the barcode, if any
+   * @return raw text encoded by the barcode, if applicable, otherwise <code>null</code>
    */
   public String getText() {
     return text;
   }
 
+  /**
+   * @return raw bytes encoded by the barcode, if applicable, otherwise <code>null</code>
+   */
+  public byte[] getRawBytes() {
+    return rawBytes;
+  }
+
   /**
    * @return points related to the barcode in the image. These are typically points
    *         identifying finder patterns or the corners of the barcode. The exact meaning is
index 69cd954..9e390e7 100644 (file)
@@ -16,6 +16,8 @@
 
 package com.google.zxing.client.result;
 
+import com.google.zxing.Result;
+
 /**
  * <p>Abstract class representing the result of decoding a barcode, as more than
  * a String -- as some type of structured data. This might be a subclass which represents
@@ -41,10 +43,11 @@ public abstract class ParsedReaderResult {
 
   public abstract String getDisplayResult();
 
-  public static ParsedReaderResult parseReaderResult(String rawText) {
+  public static ParsedReaderResult parseReaderResult(Result theResult) {
     // This is a bit messy, but given limited options in MIDP / CLDC, this may well be the simplest
     // way to go about this. For example, we have no reflection available, really.
     // Order is important here.
+    String rawText = theResult.getText();
     ParsedReaderResult result;
     if ((result = BookmarkDoCoMoResult.parse(rawText)) != null) {
       return result;
diff --git a/core/src/com/google/zxing/common/DecoderResult.java b/core/src/com/google/zxing/common/DecoderResult.java
new file mode 100644 (file)
index 0000000..ef1745f
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2007 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.common;
+
+/**
+ * <p>Encapsulates the result of decoding a matrix of bits. This typically
+ * applies to 2D barcode formats. For now it contains the raw bytes obtained,
+ * as well as a String interpretation of those bytes, if applicable.</p>
+ *
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class DecoderResult {
+
+  private final byte[] rawBytes;
+  private final String text;
+
+  public DecoderResult(byte[] rawBytes, String text) {
+    if (rawBytes == null && text == null) {
+      throw new IllegalArgumentException();
+    }
+    this.rawBytes = rawBytes;
+    this.text = text;
+  }
+
+  public byte[] getRawBytes() {
+    return rawBytes;
+  }
+
+  public String getText() {
+    return text;
+  }
+
+}
\ No newline at end of file
index 16f1c02..ac23a15 100644 (file)
@@ -23,6 +23,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.datamatrix.decoder.Decoder;
 
 import java.util.Hashtable;
@@ -50,18 +51,18 @@ public final class DataMatrixReader 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());
+    //  decoderResult = decoder.decode(result.getBits());
     //  points = result.getPoints();
     //}
-    return new Result(text, points, BarcodeFormat.DATAMATRIX);
+    return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.DATAMATRIX);
   }
 
   /**
index 3280eb6..8bb59bc 100644 (file)
@@ -18,6 +18,7 @@ package com.google.zxing.datamatrix.decoder;
 \r
 import com.google.zxing.ReaderException;\r
 import com.google.zxing.common.BitMatrix;\r
+import com.google.zxing.common.DecoderResult;\r
 import com.google.zxing.common.reedsolomon.GF256;\r
 import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;\r
 import com.google.zxing.common.reedsolomon.ReedSolomonException;\r
@@ -41,10 +42,10 @@ public final class Decoder {
    * "true" is taken to mean a black module.</p>\r
    *\r
    * @param image booleans representing white/black Data Matrix Code modules\r
-   * @return text encoded within the Data Matrix Code\r
+   * @return text and bytes encoded within the Data Matrix Code\r
    * @throws ReaderException if the Data Matrix Code cannot be decoded\r
    */\r
-  public String decode(boolean[][] image) throws ReaderException {\r
+  public DecoderResult decode(boolean[][] image) throws ReaderException {\r
     int dimension = image.length;\r
     BitMatrix bits = new BitMatrix(dimension);\r
     for (int i = 0; i < dimension; i++) {\r
@@ -62,10 +63,10 @@ public final class Decoder {
    * to mean a black module.</p>\r
    *\r
    * @param bits booleans representing white/black Data Matrix Code modules\r
-   * @return text encoded within the Data Matrix Code\r
+   * @return text and bytes encoded within the Data Matrix Code\r
    * @throws ReaderException if the Data Matrix Code cannot be decoded\r
    */\r
-  public String decode(BitMatrix bits) throws ReaderException {\r
+  public DecoderResult decode(BitMatrix bits) throws ReaderException {\r
 \r
     // Construct a parser and read version, error-correction level\r
     BitMatrixParser parser = new BitMatrixParser(bits);\r
@@ -96,7 +97,8 @@ public final class Decoder {
     }\r
 \r
     // Decode the contents of that stream of bytes\r
-    return DecodedBitStreamParser.decode(resultBytes);\r
+    String text = DecodedBitStreamParser.decode(resultBytes);\r
+    return new DecoderResult(resultBytes, text);\r
   }\r
 \r
   /**\r
index 26262fd..68132d1 100644 (file)
@@ -123,6 +123,7 @@ public abstract class AbstractUPCEANReader extends AbstractOneDReader implements
 
     return new Result(
         resultString,
+        null, // no natural byte representation for these barcodes
         new ResultPoint[]{
             new GenericResultPoint((float) (startGuardRange[1] - startGuardRange[0]) / 2.0f, (float) rowNumber),
             new GenericResultPoint((float) (endRange[1] - endRange[0]) / 2.0f, (float) rowNumber)},
index 96bf2c9..46d1bd7 100644 (file)
@@ -407,6 +407,7 @@ public final class Code128Reader extends AbstractOneDReader {
     String resultString = result.toString();
     return new Result(
         resultString,
+        null,
         new ResultPoint[]{
             new GenericResultPoint((float) (startPatternInfo[1] - startPatternInfo[0]) / 2.0f, (float) rowNumber),
             new GenericResultPoint((float) (nextStart - lastStart) / 2.0f, (float) rowNumber)},
index 57f1009..6ab9510 100644 (file)
@@ -140,6 +140,7 @@ public final class Code39Reader extends AbstractOneDReader {
     }
     return new Result(
         resultString,
+        null,
         new ResultPoint[]{
             new GenericResultPoint((float) (start[1] - start[0]) / 2.0f, (float) rowNumber),
             new GenericResultPoint((float) (nextStart - lastStart) / 2.0f, (float) rowNumber)},
index 835504b..b31fbf4 100644 (file)
@@ -81,7 +81,7 @@ public final class MultiFormatUPCEANReader extends AbstractOneDReader {
       // Here is, therefore, where we implement this logic:
       if (result.getBarcodeFormat().equals(BarcodeFormat.EAN_13) &&
           result.getText().charAt(0) == '0') {
-        return new Result(result.getText().substring(1), result.getResultPoints(), BarcodeFormat.UPC_A);
+        return new Result(result.getText().substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
       }
       return result;
     }
index 2a58842..34fe999 100644 (file)
@@ -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());
+      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);
   }
 
   /**
index 4f73245..e22fea5 100644 (file)
@@ -18,6 +18,7 @@ package com.google.zxing.qrcode.decoder;
 \r
 import com.google.zxing.ReaderException;\r
 import com.google.zxing.common.BitMatrix;\r
+import com.google.zxing.common.DecoderResult;\r
 import com.google.zxing.common.reedsolomon.GF256;\r
 import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;\r
 import com.google.zxing.common.reedsolomon.ReedSolomonException;\r
@@ -41,10 +42,10 @@ public final class Decoder {
    * "true" is taken to mean a black module.</p>\r
    *\r
    * @param image booleans representing white/black QR Code modules\r
-   * @return text encoded within the QR Code\r
+   * @return text and bytes encoded within the QR Code\r
    * @throws ReaderException if the QR Code cannot be decoded\r
    */\r
-  public String decode(boolean[][] image) throws ReaderException {\r
+  public DecoderResult decode(boolean[][] image) throws ReaderException {\r
     int dimension = image.length;\r
     BitMatrix bits = new BitMatrix(dimension);\r
     for (int i = 0; i < dimension; i++) {\r
@@ -61,10 +62,10 @@ public final class Decoder {
    * <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>\r
    *\r
    * @param bits booleans representing white/black QR Code modules\r
-   * @return text encoded within the QR Code\r
+   * @return text and bytes encoded within the QR Code\r
    * @throws ReaderException if the QR Code cannot be decoded\r
    */\r
-  public String decode(BitMatrix bits) throws ReaderException {\r
+  public DecoderResult decode(BitMatrix bits) throws ReaderException {\r
 \r
     // Construct a parser and read version, error-correction level\r
     BitMatrixParser parser = new BitMatrixParser(bits);\r
@@ -96,7 +97,8 @@ public final class Decoder {
     }\r
 \r
     // Decode the contents of that stream of bytes\r
-    return DecodedBitStreamParser.decode(resultBytes, version);\r
+    String text = DecodedBitStreamParser.decode(resultBytes, version);\r
+    return new DecoderResult(resultBytes, text);\r
   }\r
 \r
   /**\r
index dc28f71..33fdfc2 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.google.zxing.client.result;
 
+import com.google.zxing.Result;
 import junit.framework.TestCase;
 
 /**
@@ -91,7 +92,8 @@ public final class ParsedReaderResultTestCase extends TestCase {
   }
 
   private static void doTestResult(String text, ParsedReaderResultType type) {
-    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
+    Result fakeResult = new Result(text, null, null, null);
+    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(fakeResult);
     assertNotNull(result);
     assertEquals(type, result.getType());
   }
index 5d822ba..6a5c779 100644 (file)
@@ -76,7 +76,7 @@ final class SnapshotThread extends Thread {
         MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
         Reader reader = new MultiFormatReader();
         Result result = reader.decode(source);
-        zXingMIDlet.handleDecodedText(result.getText());
+        zXingMIDlet.handleDecodedText(result);
       } catch (ReaderException re) {
         // Show a friendlier message on a mere failure to read the barcode
         zXingMIDlet.showError("Sorry, no barcode was found.");
index 662a558..fffe2ab 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.google.zxing.client.j2me;
 
+import com.google.zxing.Result;
 import com.google.zxing.client.result.BookmarkDoCoMoResult;
 import com.google.zxing.client.result.EmailAddressResult;
 import com.google.zxing.client.result.EmailDoCoMoResult;
@@ -188,8 +189,8 @@ public final class ZXingMIDlet extends MIDlet {
     display.setCurrent(alert, canvas);
   }
 
-  void handleDecodedText(String text) {
-    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
+  void handleDecodedText(Result theResult) {
+    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult);
     ParsedReaderResultType type = result.getType();
     if (type.equals(ParsedReaderResultType.URI)) {
       String uri = ((URIParsedResult) result).getURI();