Issue 331
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / Decoder.java
1 /*\r
2  * Copyright 2007 ZXing authors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.google.zxing.qrcode.decoder;\r
18 \r
19 import com.google.zxing.ReaderException;\r
20 import com.google.zxing.common.BitMatrix;\r
21 import com.google.zxing.common.DecoderResult;\r
22 import com.google.zxing.common.reedsolomon.GF256;\r
23 import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;\r
24 import com.google.zxing.common.reedsolomon.ReedSolomonException;\r
25 \r
26 import java.util.Hashtable;\r
27 \r
28 /**\r
29  * <p>The main class which implements QR Code decoding -- as opposed to locating and extracting\r
30  * the QR Code from an image.</p>\r
31  *\r
32  * @author Sean Owen\r
33  */\r
34 public final class Decoder {\r
35 \r
36   private final ReedSolomonDecoder rsDecoder;\r
37 \r
38   public Decoder() {\r
39     rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);\r
40   }\r
41 \r
42   public DecoderResult decode(boolean[][] image) throws ReaderException {\r
43     return decode(image, null);\r
44   }\r
45 \r
46   /**\r
47    * <p>Convenience method that can decode a QR Code represented as a 2D array of booleans.\r
48    * "true" is taken to mean a black module.</p>\r
49    *\r
50    * @param image booleans representing white/black QR Code modules\r
51    * @return text and bytes encoded within the QR Code\r
52    * @throws ReaderException if the QR Code cannot be decoded\r
53    */\r
54   public DecoderResult decode(boolean[][] image, Hashtable hints) throws ReaderException {\r
55     int dimension = image.length;\r
56     BitMatrix bits = new BitMatrix(dimension);\r
57     for (int i = 0; i < dimension; i++) {\r
58       for (int j = 0; j < dimension; j++) {\r
59         if (image[i][j]) {\r
60           bits.set(j, i);\r
61         }\r
62       }\r
63     }\r
64     return decode(bits, hints);\r
65   }\r
66 \r
67   public DecoderResult decode(BitMatrix bits) throws ReaderException {\r
68     return decode(bits, null);\r
69   }\r
70 \r
71   /**\r
72    * <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>\r
73    *\r
74    * @param bits booleans representing white/black QR Code modules\r
75    * @return text and bytes encoded within the QR Code\r
76    * @throws ReaderException if the QR Code cannot be decoded\r
77    */\r
78   public DecoderResult decode(BitMatrix bits, Hashtable hints) throws ReaderException {\r
79 \r
80     // Construct a parser and read version, error-correction level\r
81     BitMatrixParser parser = new BitMatrixParser(bits);\r
82     Version version = parser.readVersion();\r
83     ErrorCorrectionLevel ecLevel = parser.readFormatInformation().getErrorCorrectionLevel();\r
84 \r
85     // Read codewords\r
86     byte[] codewords = parser.readCodewords();\r
87     // Separate into data blocks\r
88     DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel);\r
89 \r
90     // Count total number of data bytes\r
91     int totalBytes = 0;\r
92     for (int i = 0; i < dataBlocks.length; i++) {\r
93       totalBytes += dataBlocks[i].getNumDataCodewords();\r
94     }\r
95     byte[] resultBytes = new byte[totalBytes];\r
96     int resultOffset = 0;\r
97 \r
98     // Error-correct and copy data blocks together into a stream of bytes\r
99     for (int j = 0; j < dataBlocks.length; j++) {\r
100       DataBlock dataBlock = dataBlocks[j];\r
101       byte[] codewordBytes = dataBlock.getCodewords();\r
102       int numDataCodewords = dataBlock.getNumDataCodewords();\r
103       correctErrors(codewordBytes, numDataCodewords);\r
104       for (int i = 0; i < numDataCodewords; i++) {\r
105         resultBytes[resultOffset++] = codewordBytes[i];\r
106       }\r
107     }\r
108 \r
109     // Decode the contents of that stream of bytes\r
110     return DecodedBitStreamParser.decode(resultBytes, version, ecLevel, hints);\r
111   }\r
112 \r
113   /**\r
114    * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to\r
115    * correct the errors in-place using Reed-Solomon error correction.</p>\r
116    *\r
117    * @param codewordBytes data and error correction codewords\r
118    * @param numDataCodewords number of codewords that are data bytes\r
119    * @throws ReaderException if error correction fails\r
120    */\r
121   private void correctErrors(byte[] codewordBytes, int numDataCodewords) throws ReaderException {\r
122     int numCodewords = codewordBytes.length;\r
123     // First read into an array of ints\r
124     int[] codewordsInts = new int[numCodewords];\r
125     for (int i = 0; i < numCodewords; i++) {\r
126       codewordsInts[i] = codewordBytes[i] & 0xFF;\r
127     }\r
128     int numECCodewords = codewordBytes.length - numDataCodewords;\r
129     try {\r
130       rsDecoder.decode(codewordsInts, numECCodewords);\r
131     } catch (ReedSolomonException rse) {\r
132       throw ReaderException.getInstance();\r
133     }\r
134     // Copy back into array of bytes -- only need to worry about the bytes that were data\r
135     // We don't care about errors in the error-correction codewords\r
136     for (int i = 0; i < numDataCodewords; i++) {\r
137       codewordBytes[i] = (byte) codewordsInts[i];\r
138     }\r
139   }\r
140 \r
141 }\r