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.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;
35 import java.util.Hashtable;
38 * This implementation can detect and decode QR Codes in an image.
42 public class QRCodeReader implements Reader {
44 private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
46 private final Decoder decoder = new Decoder();
48 protected Decoder getDecoder() {
53 * Locates and decodes a QR code in an image.
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
60 public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException {
61 return decode(image, null);
64 public Result decode(BinaryBitmap image, Hashtable hints)
65 throws NotFoundException, ChecksumException, FormatException {
66 DecoderResult decoderResult;
68 if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
69 BitMatrix bits = extractPureBits(image.getBlackMatrix());
70 decoderResult = decoder.decode(bits, hints);
73 DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
74 decoderResult = decoder.decode(detectorResult.getBits(), hints);
75 points = detectorResult.getPoints();
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());
82 if (decoderResult.getECLevel() != null) {
83 result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
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
98 public static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
100 int height = image.getHeight();
101 int width = image.getWidth();
102 int minDimension = Math.min(height, width);
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();
110 int x = leftTopBlack[0];
111 int y = leftTopBlack[1];
112 while (x < minDimension && y < minDimension && image.get(x, y)) {
116 if (x == minDimension || y == minDimension) {
117 throw NotFoundException.getNotFoundInstance();
120 int moduleSize = x - leftTopBlack[0];
121 if (moduleSize == 0) {
122 throw NotFoundException.getNotFoundInstance();
125 // And now find where the rightmost black module on the first row ends
126 int rowEndOfSymbol = width - 1;
127 while (rowEndOfSymbol > x && !image.get(rowEndOfSymbol, y)) {
130 if (rowEndOfSymbol <= x) {
131 throw NotFoundException.getNotFoundInstance();
135 // Make sure width of barcode is a multiple of module size
136 if ((rowEndOfSymbol - x) % moduleSize != 0) {
137 throw NotFoundException.getNotFoundInstance();
139 int dimension = 1 + ((rowEndOfSymbol - x) / moduleSize);
141 // Push in the "border" by half the module width so that we start
142 // sampling in the middle of the module. Just in case the image is a
143 // little off, this will help recover. Need to back up at least 1.
144 int backOffAmount = moduleSize == 1 ? 1 : moduleSize >> 1;
148 if ((x + (dimension - 1) * moduleSize) >= width ||
149 (y + (dimension - 1) * moduleSize) >= height) {
150 throw NotFoundException.getNotFoundInstance();
153 // Now just read off the bits
154 BitMatrix bits = new BitMatrix(dimension);
155 for (int i = 0; i < dimension; i++) {
156 int iOffset = y + i * moduleSize;
157 for (int j = 0; j < dimension; j++) {
158 if (image.get(x + j * moduleSize, iOffset)) {