2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.qrcode;
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.DecoderResult;
28 import com.google.zxing.common.DetectorResult;
29 import com.google.zxing.qrcode.decoder.Decoder;
30 import com.google.zxing.qrcode.detector.Detector;
32 import java.util.Hashtable;
35 * This implementation can detect and decode QR Codes in an image.
37 * @author srowen@google.com (Sean Owen)
39 public final class QRCodeReader implements Reader {
41 private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
43 private final Decoder decoder = new Decoder();
46 * Locates and decodes a QR code in an image.
48 * @return a String representing the content encoded by the QR code
49 * @throws ReaderException if a QR code cannot be found, or cannot be decoded
51 public Result decode(MonochromeBitmapSource image) throws ReaderException {
52 return decode(image, null);
55 public Result decode(MonochromeBitmapSource image, Hashtable hints)
56 throws ReaderException {
57 DecoderResult decoderResult;
59 if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
60 BitMatrix bits = extractPureBits(image);
61 decoderResult = decoder.decode(bits);
64 DetectorResult result = new Detector(image).detect();
65 decoderResult = decoder.decode(result.getBits());
66 points = result.getPoints();
68 return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
72 * This method detects a barcode in a "pure" image -- that is, pure monochrome image
73 * which contains only an unrotated, unskewed, image of a barcode, with some white border
74 * around it. This is a specialized method that works exceptionally fast in this special
77 private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
78 // Now need to determine module size in pixels
80 int height = image.getHeight();
81 int width = image.getWidth();
82 int minDimension = Math.min(height, width);
84 // First, skip white border by tracking diagonally from the top left down and to the right:
86 while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
89 if (borderWidth == minDimension) {
90 throw new ReaderException("No black pixels found along diagonal");
93 // And then keep tracking across the top-left black module to determine module size
94 int moduleEnd = borderWidth;
95 while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {
98 if (moduleEnd == minDimension) {
99 throw new ReaderException("No end to black pixels found along diagonal");
102 int moduleSize = moduleEnd - borderWidth;
104 // And now find where the rightmost black module on the first row ends
105 int rowEndOfSymbol = width - 1;
106 while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
109 if (rowEndOfSymbol < 0) {
110 throw new ReaderException("Can't find end of rightmost black module");
114 // Make sure width of barcode is a multiple of module size
115 if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
116 throw new ReaderException("Bad module size / width: " + moduleSize +
117 " / " + (rowEndOfSymbol - borderWidth));
119 int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
121 // Push in the "border" by half the module width so that we start
122 // sampling in the middle of the module. Just in case the image is a
123 // little off, this will help recover.
124 borderWidth += moduleSize >> 1;
126 int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
127 if (sampleDimension >= width || sampleDimension >= height) {
128 throw new ReaderException("Estimated pure image size is beyond image boundaries");
131 // Now just read off the bits
132 BitMatrix bits = new BitMatrix(dimension);
133 for (int i = 0; i < dimension; i++) {
134 int iOffset = borderWidth + i * moduleSize;
135 for (int j = 0; j < dimension; j++) {
136 if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {