2 * Copyright 2007 ZXing authors
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package com.google.zxing.datamatrix.decoder;
\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
27 * <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting
\r
28 * the Data Matrix Code from an image.</p>
\r
30 * @author bbrown@google.com (Brian Brown)
\r
32 public final class Decoder {
\r
34 private final ReedSolomonDecoder rsDecoder;
\r
37 rsDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);
\r
41 * <p>Convenience method that can decode a Data Matrix Code represented as a 2D array of booleans.
\r
42 * "true" is taken to mean a black module.</p>
\r
44 * @param image booleans representing white/black Data Matrix Code modules
\r
45 * @return text and bytes encoded within the Data Matrix Code
\r
46 * @throws ReaderException if the Data Matrix Code cannot be decoded
\r
48 public DecoderResult decode(boolean[][] image) throws ReaderException {
\r
49 int dimension = image.length;
\r
50 BitMatrix bits = new BitMatrix(dimension);
\r
51 for (int i = 0; i < dimension; i++) {
\r
52 for (int j = 0; j < dimension; j++) {
\r
58 return decode(bits);
\r
62 * <p>Decodes a Data Matrix Code represented as a {@link BitMatrix}. A 1 or "true" is taken
\r
63 * to mean a black module.</p>
\r
65 * @param bits booleans representing white/black Data Matrix Code modules
\r
66 * @return text and bytes encoded within the Data Matrix Code
\r
67 * @throws ReaderException if the Data Matrix Code cannot be decoded
\r
69 public DecoderResult decode(BitMatrix bits) throws ReaderException {
\r
71 // Construct a parser and read version, error-correction level
\r
72 BitMatrixParser parser = new BitMatrixParser(bits);
\r
73 Version version = parser.readVersion(bits);
\r
76 byte[] codewords = parser.readCodewords();
\r
77 // Separate into data blocks
\r
78 DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version);
\r
80 // Count total number of data bytes
\r
82 for (int i = 0; i < dataBlocks.length; i++) {
\r
83 totalBytes += dataBlocks[i].getNumDataCodewords();
\r
85 byte[] resultBytes = new byte[totalBytes];
\r
86 int resultOffset = 0;
\r
88 // Error-correct and copy data blocks together into a stream of bytes
\r
89 for (int j = 0; j < dataBlocks.length; j++) {
\r
90 DataBlock dataBlock = dataBlocks[j];
\r
91 byte[] codewordBytes = dataBlock.getCodewords();
\r
92 int numDataCodewords = dataBlock.getNumDataCodewords();
\r
93 correctErrors(codewordBytes, numDataCodewords);
\r
94 for (int i = 0; i < numDataCodewords; i++) {
\r
95 resultBytes[resultOffset++] = codewordBytes[i];
\r
99 // Decode the contents of that stream of bytes
\r
100 String text = DecodedBitStreamParser.decode(resultBytes);
\r
101 return new DecoderResult(resultBytes, text);
\r
105 * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to
\r
106 * correct the errors in-place using Reed-Solomon error correction.</p>
\r
108 * @param codewordBytes data and error correction codewords
\r
109 * @param numDataCodewords number of codewords that are data bytes
\r
110 * @throws ReaderException if error correction fails
\r
112 private void correctErrors(byte[] codewordBytes, int numDataCodewords) throws ReaderException {
\r
113 int numCodewords = codewordBytes.length;
\r
114 // First read into an array of ints
\r
115 int[] codewordsInts = new int[numCodewords];
\r
116 for (int i = 0; i < numCodewords; i++) {
\r
117 codewordsInts[i] = codewordBytes[i] & 0xFF;
\r
119 int numECCodewords = codewordBytes.length - numDataCodewords;
\r
121 rsDecoder.decode(codewordsInts, numECCodewords);
\r
122 } catch (ReedSolomonException rse) {
\r
123 throw new ReaderException(rse.toString());
\r
125 // Copy back into array of bytes -- only need to worry about the bytes that were data
\r
126 // We don't care about errors in the error-correction codewords
\r
127 for (int i = 0; i < numDataCodewords; i++) {
\r
128 codewordBytes[i] = (byte) codewordsInts[i];
\r