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