Issue 412
[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.BinaryBitmap;
21 import com.google.zxing.ChecksumException;
22 import com.google.zxing.DecodeHintType;
23 import com.google.zxing.FormatException;
24 import com.google.zxing.NotFoundException;
25 import com.google.zxing.Reader;
26 import com.google.zxing.Result;
27 import com.google.zxing.ResultMetadataType;
28 import com.google.zxing.ResultPoint;
29 import com.google.zxing.common.BitMatrix;
30 import com.google.zxing.common.DecoderResult;
31 import com.google.zxing.common.DetectorResult;
32 import com.google.zxing.qrcode.decoder.Decoder;
33 import com.google.zxing.qrcode.detector.Detector;
34
35 import java.util.Hashtable;
36
37 /**
38  * This implementation can detect and decode QR Codes in an image.
39  *
40  * @author Sean Owen
41  */
42 public class QRCodeReader implements Reader {
43
44   private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
45
46   private final Decoder decoder = new Decoder();
47
48   protected Decoder getDecoder() {
49     return decoder;
50   }
51
52   /**
53    * Locates and decodes a QR code in an image.
54    *
55    * @return a String representing the content encoded by the QR code
56    * @throws NotFoundException if a QR code cannot be found
57    * @throws FormatException if a QR code cannot be decoded
58    * @throws ChecksumException if error correction fails
59    */
60   public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException {
61     return decode(image, null);
62   }
63
64   public Result decode(BinaryBitmap image, Hashtable hints)
65       throws NotFoundException, ChecksumException, FormatException {
66     DecoderResult decoderResult;
67     ResultPoint[] points;
68     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
69       BitMatrix bits = extractPureBits(image.getBlackMatrix());
70       decoderResult = decoder.decode(bits, hints);
71       points = NO_POINTS;
72     } else {
73       DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
74       decoderResult = decoder.decode(detectorResult.getBits(), hints);
75       points = detectorResult.getPoints();
76     }
77
78     Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
79     if (decoderResult.getByteSegments() != null) {
80       result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
81     }
82     if (decoderResult.getECLevel() != null) {
83       result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
84     }
85     return result;
86   }
87
88   public void reset() {
89     // do nothing
90   }
91
92   /**
93    * This method detects a barcode in a "pure" image -- that is, pure monochrome image
94    * which contains only an unrotated, unskewed, image of a barcode, with some white border
95    * around it. This is a specialized method that works exceptionally fast in this special
96    * case.
97    */
98   public static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
99
100     int height = image.getHeight();
101     int width = image.getWidth();
102     int minDimension = Math.min(height, width);
103
104     // And then keep tracking across the top-left black module to determine module size
105     //int moduleEnd = borderWidth;
106     int[] leftTopBlack = image.getTopLeftOnBit();
107     if (leftTopBlack == null) {
108       throw NotFoundException.getNotFoundInstance();
109     }
110     int x = leftTopBlack[0];
111     int y = leftTopBlack[1];
112     while (x < minDimension && y < minDimension && image.get(x, y)) {
113       x++;
114       y++;
115     }
116     if (x == minDimension || y == minDimension) {
117       throw NotFoundException.getNotFoundInstance();
118     }
119
120     int moduleSize = x - leftTopBlack[0];
121     if (moduleSize == 0) {
122       throw NotFoundException.getNotFoundInstance();
123     }
124
125     // And now find where the rightmost black module on the first row ends
126     int rowEndOfSymbol = width - 1;
127     while (rowEndOfSymbol > x && !image.get(rowEndOfSymbol, y)) {
128       rowEndOfSymbol--;
129     }
130     if (rowEndOfSymbol <= x) {
131       throw NotFoundException.getNotFoundInstance();
132     }
133     rowEndOfSymbol++;
134
135     // Make sure width of barcode is a multiple of module size
136     if ((rowEndOfSymbol - x) % moduleSize != 0) {
137       throw NotFoundException.getNotFoundInstance();
138     }
139     int dimension = 1 + ((rowEndOfSymbol - x) / moduleSize);
140
141     // Push in the "border" by half the module width so that we start
142     // sampling in the middle of the module. Just in case the image is a
143     // little off, this will help recover.
144     x -= moduleSize >> 1;
145     y -= moduleSize >> 1;
146
147     if ((x + (dimension - 1) * moduleSize) >= width ||
148         (y + (dimension - 1) * moduleSize) >= height) {
149       throw NotFoundException.getNotFoundInstance();
150     }
151
152     // Now just read off the bits
153     BitMatrix bits = new BitMatrix(dimension);
154     for (int i = 0; i < dimension; i++) {
155       int iOffset = y + i * moduleSize;
156       for (int j = 0; j < dimension; j++) {
157         if (image.get(x + j * moduleSize, iOffset)) {
158           bits.set(j, i);
159         }
160       }
161     }
162     return bits;
163   }
164
165 }