Updates from sanfordsquires to fix RS decoding for Datamatrix
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / Decoder.java
index 8279eea..0ee7954 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
- * Copyright 2007 Google Inc.\r
+ * Copyright 2007 ZXing authors\r
  *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -18,6 +18,8 @@ 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
 \r
@@ -29,7 +31,10 @@ import com.google.zxing.common.reedsolomon.ReedSolomonException;
  */\r
 public final class Decoder {\r
 \r
-  private Decoder() {\r
+  private final ReedSolomonDecoder rsDecoder;\r
+\r
+  public Decoder() {\r
+    rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);\r
   }\r
 \r
   /**\r
@@ -37,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 static 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
@@ -57,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 static 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
@@ -92,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
@@ -103,7 +109,7 @@ public final class Decoder {
    * @param numDataCodewords number of codewords that are data bytes\r
    * @throws ReaderException if error correction fails\r
    */\r
-  private static void correctErrors(byte[] codewordBytes, int numDataCodewords) throws ReaderException {\r
+  private void correctErrors(byte[] codewordBytes, int numDataCodewords) throws ReaderException {\r
     int numCodewords = codewordBytes.length;\r
     // First read into an array of ints\r
     int[] codewordsInts = new int[numCodewords];\r
@@ -112,7 +118,7 @@ public final class Decoder {
     }\r
     int numECCodewords = codewordBytes.length - numDataCodewords;\r
     try {\r
-      ReedSolomonDecoder.decode(codewordsInts, numECCodewords);\r
+      rsDecoder.decode(codewordsInts, numECCodewords);\r
     } catch (ReedSolomonException rse) {\r
       throw new ReaderException(rse.toString());\r
     }\r