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