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