2 * Copyright 2007 Google Inc.
\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.qrcode.decoder;
\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
25 * @author srowen@google.com (Sean Owen)
\r
27 public final class Decoder {
\r
32 public static String decode(boolean[][] image) throws ReaderException {
\r
33 int dimension = image.length;
\r
34 BitMatrix bits = new BitMatrix(dimension);
\r
35 for (int i = 0; i < dimension; i++) {
\r
36 for (int j = 0; j < dimension; j++) {
\r
42 return decode(bits);
\r
45 public static String decode(BitMatrix bits) throws ReaderException {
\r
46 BitMatrixParser parser = new BitMatrixParser(bits);
\r
47 Version version = parser.readVersion();
\r
48 ErrorCorrectionLevel ecLevel = parser.readFormatInformation().getErrorCorrectionLevel();
\r
49 byte[] codewords = parser.readCodewords();
\r
50 DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel);
\r
52 for (int i = 0; i < dataBlocks.length; i++) {
\r
53 totalBytes += dataBlocks[i].getNumDataCodewords();
\r
55 byte[] resultBytes = new byte[totalBytes];
\r
56 int resultOffset = 0;
\r
57 for (int j = 0; j < dataBlocks.length; j++) {
\r
58 DataBlock dataBlock = dataBlocks[j];
\r
59 byte[] codewordBytes = dataBlock.getCodewords();
\r
60 int numDataCodewords = dataBlock.getNumDataCodewords();
\r
61 correctErrors(codewordBytes, numDataCodewords);
\r
62 for (int i = 0; i < numDataCodewords; i++) {
\r
63 resultBytes[resultOffset++] = codewordBytes[i];
\r
67 return DecodedBitStreamParser.decode(resultBytes, version);
\r
70 private static void correctErrors(byte[] codewordBytes, int numDataCodewords)
\r
71 throws ReaderException {
\r
72 int numCodewords = codewordBytes.length;
\r
73 int[] codewordsInts = new int[numCodewords];
\r
74 for (int i = 0; i < numCodewords; i++) {
\r
75 codewordsInts[i] = codewordBytes[i] & 0xFF;
\r
77 int numECCodewords = codewordBytes.length - numDataCodewords;
\r
79 ReedSolomonDecoder.decode(codewordsInts, numECCodewords);
\r
80 } catch (ReedSolomonException rse) {
\r
81 throw new ReaderException(rse.toString());
\r
83 for (int i = 0; i < numDataCodewords; i++) {
\r
84 codewordBytes[i] = (byte) codewordsInts[i];
\r