git-svn-id: http://zxing.googlecode.com/svn/trunk@6 59b500cc-1b3d-0410-9834-0bbf25fbcc57
[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  * @author srowen@google.com (Sean Owen)\r
26  */\r
27 public final class Decoder {\r
28 \r
29   private Decoder() {\r
30   }\r
31 \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
37         if (image[i][j]) {\r
38           bits.set(i, j);\r
39         }\r
40       }\r
41     }\r
42     return decode(bits);\r
43   }\r
44 \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
51     int totalBytes = 0;\r
52     for (int i = 0; i < dataBlocks.length; i++) {\r
53       totalBytes += dataBlocks[i].getNumDataCodewords();\r
54     }\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
64       }\r
65     }\r
66 \r
67     return DecodedBitStreamParser.decode(resultBytes, version);\r
68   }\r
69 \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
76     }\r
77     int numECCodewords = codewordBytes.length - numDataCodewords;\r
78     try {\r
79       ReedSolomonDecoder.decode(codewordsInts, numECCodewords);\r
80     } catch (ReedSolomonException rse) {\r
81       throw new ReaderException(rse.toString());\r
82     }\r
83     for (int i = 0; i < numDataCodewords; i++) {\r
84       codewordBytes[i] = (byte) codewordsInts[i];\r
85     }\r
86   }\r
87 \r
88 }\r