296f27d91e8f8c9565c3a7a1a37ab6170d520674
[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
122     // And now find where the rightmost black module on the first row ends
123     int rowEndOfSymbol = width - 1;
124     while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, y)) {
125       rowEndOfSymbol--;
126     }
127     if (rowEndOfSymbol < 0) {
128       throw NotFoundException.getNotFoundInstance();
129     }
130     rowEndOfSymbol++;
131
132     // Make sure width of barcode is a multiple of module size
133     if ((rowEndOfSymbol - x) % moduleSize != 0) {
134       throw NotFoundException.getNotFoundInstance();
135     }
136     int dimension = 1 + ((rowEndOfSymbol - x) / moduleSize);
137
138     // Push in the "border" by half the module width so that we start
139     // sampling in the middle of the module. Just in case the image is a
140     // little off, this will help recover.
141     x -= moduleSize >> 1;
142     y -= moduleSize >> 1;
143
144     if ((x + (dimension - 1) * moduleSize) >= width ||
145         (y + (dimension - 1) * moduleSize) >= height) {
146       throw NotFoundException.getNotFoundInstance();
147     }
148
149     // Now just read off the bits
150     BitMatrix bits = new BitMatrix(dimension);
151     for (int i = 0; i < dimension; i++) {
152       int iOffset = y + i * moduleSize;
153       for (int j = 0; j < dimension; j++) {
154         if (image.get(x + j * moduleSize, iOffset)) {
155           bits.set(j, i);
156         }
157       }
158     }
159     return bits;
160   }
161
162 }