2 * Copyright 2007 ZXing authors
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.ResultMetadataType;
27 import com.google.zxing.common.BitMatrix;
28 import com.google.zxing.common.DecoderResult;
29 import com.google.zxing.common.DetectorResult;
30 import com.google.zxing.qrcode.decoder.Decoder;
31 import com.google.zxing.qrcode.detector.Detector;
33 import java.util.Hashtable;
36 * This implementation can detect and decode QR Codes in an image.
40 public class QRCodeReader implements Reader {
42 private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
44 private final Decoder decoder = new Decoder();
46 protected Decoder getDecoder() {
51 * Locates and decodes a QR code in an image.
53 * @return a String representing the content encoded by the QR code
54 * @throws ReaderException if a QR code cannot be found, or cannot be decoded
56 public Result decode(MonochromeBitmapSource image) throws ReaderException {
57 return decode(image, null);
60 public Result decode(MonochromeBitmapSource image, Hashtable hints)
61 throws ReaderException {
62 DecoderResult decoderResult;
64 if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
65 BitMatrix bits = extractPureBits(image);
66 decoderResult = decoder.decode(bits);
69 DetectorResult detectorResult = new Detector(image).detect(hints);
70 decoderResult = decoder.decode(detectorResult.getBits());
71 points = detectorResult.getPoints();
74 Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
75 if (decoderResult.getByteSegments() != null) {
76 result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
82 * This method detects a barcode in a "pure" image -- that is, pure monochrome image
83 * which contains only an unrotated, unskewed, image of a barcode, with some white border
84 * around it. This is a specialized method that works exceptionally fast in this special
87 private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
88 // Now need to determine module size in pixels
90 int height = image.getHeight();
91 int width = image.getWidth();
92 int minDimension = Math.min(height, width);
94 // First, skip white border by tracking diagonally from the top left down and to the right:
96 while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
99 if (borderWidth == minDimension) {
100 throw ReaderException.getInstance();
103 // And then keep tracking across the top-left black module to determine module size
104 int moduleEnd = borderWidth;
105 while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {
108 if (moduleEnd == minDimension) {
109 throw ReaderException.getInstance();
112 int moduleSize = moduleEnd - borderWidth;
114 // And now find where the rightmost black module on the first row ends
115 int rowEndOfSymbol = width - 1;
116 while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
119 if (rowEndOfSymbol < 0) {
120 throw ReaderException.getInstance();
124 // Make sure width of barcode is a multiple of module size
125 if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
126 throw ReaderException.getInstance();
128 int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
130 // Push in the "border" by half the module width so that we start
131 // sampling in the middle of the module. Just in case the image is a
132 // little off, this will help recover.
133 borderWidth += moduleSize >> 1;
135 int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
136 if (sampleDimension >= width || sampleDimension >= height) {
137 throw ReaderException.getInstance();
140 // Now just read off the bits
141 BitMatrix bits = new BitMatrix(dimension);
142 for (int i = 0; i < dimension; i++) {
143 int iOffset = borderWidth + i * moduleSize;
144 for (int j = 0; j < dimension; j++) {
145 if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {