Issue 331
[zxing.git] / core / src / com / google / zxing / qrcode / QRCodeReader.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.DecodeHintType;
21 import com.google.zxing.Reader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.ResultPoint;
25 import com.google.zxing.ResultMetadataType;
26 import com.google.zxing.BinaryBitmap;
27 import com.google.zxing.common.BitMatrix;
28 import com.google.zxing.common.DecoderResult;
29 import com.google.zxing.common.DetectorResult;
30 import com.google.zxing.qrcode.decoder.Decoder;
31 import com.google.zxing.qrcode.detector.Detector;
32
33 import java.util.Hashtable;
34
35 /**
36  * This implementation can detect and decode QR Codes in an image.
37  *
38  * @author Sean Owen
39  */
40 public class QRCodeReader implements Reader {
41
42   private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
43
44   private final Decoder decoder = new Decoder();
45
46   protected Decoder getDecoder() {
47     return decoder;
48   }
49
50   /**
51    * Locates and decodes a QR code in an image.
52    *
53    * @return a String representing the content encoded by the QR code
54    * @throws ReaderException if a QR code cannot be found, or cannot be decoded
55    */
56   public Result decode(BinaryBitmap image) throws ReaderException {
57     return decode(image, null);
58   }
59
60   public Result decode(BinaryBitmap image, Hashtable hints)
61       throws ReaderException {
62     DecoderResult decoderResult;
63     ResultPoint[] points;
64     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
65       BitMatrix bits = extractPureBits(image.getBlackMatrix());
66       decoderResult = decoder.decode(bits, hints);
67       points = NO_POINTS;
68     } else {
69       DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
70       decoderResult = decoder.decode(detectorResult.getBits(), hints);
71       points = detectorResult.getPoints();
72     }
73
74     Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
75     if (decoderResult.getByteSegments() != null) {
76       result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
77     }
78     if (decoderResult.getECLevel() != null) {
79       result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
80     }
81     return result;
82   }
83
84   public void reset() {
85     // do nothing
86   }
87
88   /**
89    * This method detects a barcode in a "pure" image -- that is, pure monochrome image
90    * which contains only an unrotated, unskewed, image of a barcode, with some white border
91    * around it. This is a specialized method that works exceptionally fast in this special
92    * case.
93    */
94   private static BitMatrix extractPureBits(BitMatrix image) throws ReaderException {
95     // Now need to determine module size in pixels
96
97     int height = image.getHeight();
98     int width = image.getWidth();
99     int minDimension = Math.min(height, width);
100
101     // First, skip white border by tracking diagonally from the top left down and to the right:
102     int borderWidth = 0;
103     while (borderWidth < minDimension && !image.get(borderWidth, borderWidth)) {
104       borderWidth++;
105     }
106     if (borderWidth == minDimension) {
107       throw ReaderException.getInstance();
108     }
109
110     // And then keep tracking across the top-left black module to determine module size
111     int moduleEnd = borderWidth;
112     while (moduleEnd < minDimension && image.get(moduleEnd, moduleEnd)) {
113       moduleEnd++;
114     }
115     if (moduleEnd == minDimension) {
116       throw ReaderException.getInstance();
117     }
118
119     int moduleSize = moduleEnd - borderWidth;
120
121     // And now find where the rightmost black module on the first row ends
122     int rowEndOfSymbol = width - 1;
123     while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, borderWidth)) {
124       rowEndOfSymbol--;
125     }
126     if (rowEndOfSymbol < 0) {
127       throw ReaderException.getInstance();
128     }
129     rowEndOfSymbol++;
130
131     // Make sure width of barcode is a multiple of module size
132     if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
133       throw ReaderException.getInstance();
134     }
135     int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
136
137     // Push in the "border" by half the module width so that we start
138     // sampling in the middle of the module. Just in case the image is a
139     // little off, this will help recover.
140     borderWidth += moduleSize >> 1;
141
142     int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
143     if (sampleDimension >= width || sampleDimension >= height) {
144       throw ReaderException.getInstance();
145     }
146
147     // Now just read off the bits
148     BitMatrix bits = new BitMatrix(dimension);
149     for (int i = 0; i < dimension; i++) {
150       int iOffset = borderWidth + i * moduleSize;
151       for (int j = 0; j < dimension; j++) {
152         if (image.get(borderWidth + j * moduleSize, iOffset)) {
153           bits.set(j, i);
154         }
155       }
156     }
157     return bits;
158   }
159
160 }