More javadoc
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / Decoder.java
1 /*\r
2  * Copyright 2007 Google Inc.\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.reedsolomon.ReedSolomonDecoder;\r
22 import com.google.zxing.common.reedsolomon.ReedSolomonException;\r
23 \r
24 /**\r
25  * <p>The main class which implements QR Code decoding -- as opposed to locating and extracting\r
26  * the QR Code from an image.</p>\r
27  *\r
28  * @author srowen@google.com (Sean Owen)\r
29  */\r
30 public final class Decoder {\r
31 \r
32   private Decoder() {\r
33   }\r
34 \r
35   /**\r
36    * <p>Convenience method that can decode a QR Code represented as a 2D array of booleans.\r
37    * "true" is taken to mean a black module.</p>\r
38    *\r
39    * @param image booleans representing white/black QR Code modules\r
40    * @return text encoded within the QR Code\r
41    * @throws ReaderException if the QR Code cannot be decoded\r
42    */\r
43   public static String decode(boolean[][] image) throws ReaderException {\r
44     int dimension = image.length;\r
45     BitMatrix bits = new BitMatrix(dimension);\r
46     for (int i = 0; i < dimension; i++) {\r
47       for (int j = 0; j < dimension; j++) {\r
48         if (image[i][j]) {\r
49           bits.set(i, j);\r
50         }\r
51       }\r
52     }\r
53     return decode(bits);\r
54   }\r
55 \r
56   /**\r
57    * <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>\r
58    *\r
59    * @param bits booleans representing white/black QR Code modules\r
60    * @return text encoded within the QR Code\r
61    * @throws ReaderException if the QR Code cannot be decoded\r
62    */\r
63   public static String decode(BitMatrix bits) throws ReaderException {\r
64 \r
65     // Construct a parser and read version, error-correction level\r
66     BitMatrixParser parser = new BitMatrixParser(bits);\r
67     Version version = parser.readVersion();\r
68     ErrorCorrectionLevel ecLevel = parser.readFormatInformation().getErrorCorrectionLevel();\r
69 \r
70     // Read codewords\r
71     byte[] codewords = parser.readCodewords();\r
72     // Separate into data blocks\r
73     DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel);\r
74 \r
75     // Count total number of data bytes\r
76     int totalBytes = 0;\r
77     for (int i = 0; i < dataBlocks.length; i++) {\r
78       totalBytes += dataBlocks[i].getNumDataCodewords();\r
79     }\r
80     byte[] resultBytes = new byte[totalBytes];\r
81     int resultOffset = 0;\r
82 \r
83     // Error-correct and copy data blocks together into a stream of bytes\r
84     for (int j = 0; j < dataBlocks.length; j++) {\r
85       DataBlock dataBlock = dataBlocks[j];\r
86       byte[] codewordBytes = dataBlock.getCodewords();\r
87       int numDataCodewords = dataBlock.getNumDataCodewords();\r
88       correctErrors(codewordBytes, numDataCodewords);\r
89       for (int i = 0; i < numDataCodewords; i++) {\r
90         resultBytes[resultOffset++] = codewordBytes[i];\r
91       }\r
92     }\r
93 \r
94     // Decode the contents of that stream of bytes\r
95     return DecodedBitStreamParser.decode(resultBytes, version);\r
96   }\r
97 \r
98   /**\r
99    * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to\r
100    * correct the errors in-place using Reed-Solomon error correction.</p>\r
101    *\r
102    * @param codewordBytes data and error correction codewords\r
103    * @param numDataCodewords number of codewords that are data bytes\r
104    * @throws ReaderException if error correction fails\r
105    */\r
106   private static void correctErrors(byte[] codewordBytes, int numDataCodewords) throws ReaderException {\r
107     int numCodewords = codewordBytes.length;\r
108     // First read into an array of ints\r
109     int[] codewordsInts = new int[numCodewords];\r
110     for (int i = 0; i < numCodewords; i++) {\r
111       codewordsInts[i] = codewordBytes[i] & 0xFF;\r
112     }\r
113     int numECCodewords = codewordBytes.length - numDataCodewords;\r
114     try {\r
115       ReedSolomonDecoder.decode(codewordsInts, numECCodewords);\r
116     } catch (ReedSolomonException rse) {\r
117       throw new ReaderException(rse.toString());\r
118     }\r
119     // Copy back into array of bytes -- only need to worry about the bytes that were data\r
120     // We don't care about errors in the error-correction codewords\r
121     for (int i = 0; i < numDataCodewords; i++) {\r
122       codewordBytes[i] = (byte) codewordsInts[i];\r
123     }\r
124   }\r
125 \r
126 }\r