Added rendering fix from beyonddeath
[zxing.git] / csharp / qrcode / QRCodeReader.cs
1 /*\r
2 * Copyright 2007 ZXing authors\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 namespace com.google.zxing.qrcode\r
17 {\r
18     using System;\r
19     using System.Collections;\r
20     using com.google.zxing.common;\r
21     using com.google.zxing.qrcode.decoder;\r
22     using com.google.zxing.qrcode.detector;\r
23 \r
24     public sealed class QRCodeReader: Reader\r
25     { \r
26           private static  ResultPoint[] NO_POINTS = new ResultPoint[0];\r
27           private  Decoder decoder = new Decoder();\r
28 \r
29           /**\r
30            * Locates and decodes a QR code in an image.\r
31            *\r
32            * @return a String representing the content encoded by the QR code\r
33            * @throws ReaderException if a QR code cannot be found, or cannot be decoded\r
34            */\r
35           public Result decode(MonochromeBitmapSource image) {\r
36               try{\r
37                 return decode(image, null);\r
38               }\r
39               catch(Exception e){\r
40                 throw new ReaderException(e.Message);\r
41               }\r
42             \r
43           }\r
44 \r
45           public Result decode(MonochromeBitmapSource image, Hashtable hints){  \r
46               try{\r
47                 DecoderResult decoderResult;\r
48                 ResultPoint[] points;\r
49                 if (hints != null && hints.ContainsKey(DecodeHintType.PURE_BARCODE)) {\r
50                   BitMatrix bits = extractPureBits(image);\r
51                   decoderResult = decoder.decode(bits);\r
52                   points = NO_POINTS;\r
53                 } else {\r
54                   DetectorResult detectorResult = new Detector(image).detect(hints);\r
55                   decoderResult = decoder.decode(detectorResult.getBits());\r
56                   points = detectorResult.getPoints();\r
57                 }\r
58 \r
59                 Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);\r
60                 if (decoderResult.getByteSegments() != null) {\r
61                   result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());\r
62                 }\r
63                 return result;\r
64               }catch(Exception e){             \r
65                 throw new ReaderException(e.Message);\r
66               }\r
67             \r
68           }\r
69 \r
70           /**\r
71            * This method detects a barcode in a "pure" image -- that is, pure monochrome image\r
72            * which contains only an unrotated, unskewed, image of a barcode, with some white border\r
73            * around it. This is a specialized method that works exceptionally fast in this special\r
74            * case.\r
75            */\r
76           private static BitMatrix extractPureBits(MonochromeBitmapSource image){\r
77             // Now need to determine module size in pixels\r
78 \r
79             int height = image.getHeight();\r
80             int width = image.getWidth();\r
81             int minDimension = Math.Min(height, width);\r
82 \r
83             // First, skip white border by tracking diagonally from the top left down and to the right:\r
84             int borderWidth = 0;\r
85             while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {\r
86               borderWidth++;\r
87             }\r
88             if (borderWidth == minDimension) {\r
89               throw new ReaderException();\r
90             }\r
91 \r
92             // And then keep tracking across the top-left black module to determine module size\r
93             int moduleEnd = borderWidth;\r
94             while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {\r
95               moduleEnd++;\r
96             }\r
97             if (moduleEnd == minDimension) {\r
98               throw new ReaderException();\r
99             }\r
100 \r
101             int moduleSize = moduleEnd - borderWidth;\r
102 \r
103             // And now find where the rightmost black module on the first row ends\r
104             int rowEndOfSymbol = width - 1;\r
105             while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {\r
106               rowEndOfSymbol--;\r
107             }\r
108             if (rowEndOfSymbol < 0) {\r
109               throw new ReaderException();\r
110             }\r
111             rowEndOfSymbol++;\r
112 \r
113             // Make sure width of barcode is a multiple of module size\r
114             if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {\r
115               throw new ReaderException();\r
116             }\r
117             int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;\r
118 \r
119             // Push in the "border" by half the module width so that we start\r
120             // sampling in the middle of the module. Just in case the image is a\r
121             // little off, this will help recover.\r
122             borderWidth += moduleSize >> 1;\r
123 \r
124             int sampleDimension = borderWidth + (dimension - 1) * moduleSize;\r
125             if (sampleDimension >= width || sampleDimension >= height) {\r
126               throw new ReaderException();\r
127             }\r
128 \r
129             // Now just read off the bits\r
130             BitMatrix bits = new BitMatrix(dimension);\r
131             for (int i = 0; i < dimension; i++) {\r
132               int iOffset = borderWidth + i * moduleSize;\r
133               for (int j = 0; j < dimension; j++) {\r
134                 if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {\r
135                   bits.set(i, j);\r
136                 }\r
137               }\r
138             }\r
139             return bits;\r
140           }\r
141 \r
142     \r
143     }\r
144 \r
145 }