2 * Copyright 2009 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.multi.qrcode;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.NotFoundException;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.ResultMetadataType;
25 import com.google.zxing.ResultPoint;
26 import com.google.zxing.common.DecoderResult;
27 import com.google.zxing.common.DetectorResult;
28 import com.google.zxing.multi.MultipleBarcodeReader;
29 import com.google.zxing.multi.qrcode.detector.MultiDetector;
30 import com.google.zxing.qrcode.QRCodeReader;
32 import java.util.Hashtable;
33 import java.util.Vector;
36 * This implementation can detect and decode multiple QR Codes in an image.
39 * @author Hannes Erven
41 public final class QRCodeMultiReader extends QRCodeReader implements MultipleBarcodeReader {
43 private static final Result[] EMPTY_RESULT_ARRAY = new Result[0];
45 public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException {
46 return decodeMultiple(image, null);
49 public Result[] decodeMultiple(BinaryBitmap image, Hashtable hints) throws NotFoundException {
50 Vector results = new Vector();
51 DetectorResult[] detectorResult = new MultiDetector(image.getBlackMatrix()).detectMulti(hints);
52 for (int i = 0; i < detectorResult.length; i++) {
54 DecoderResult decoderResult = getDecoder().decode(detectorResult[i].getBits());
55 ResultPoint[] points = detectorResult[i].getPoints();
56 Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points,
57 BarcodeFormat.QR_CODE);
58 if (decoderResult.getByteSegments() != null) {
59 result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
61 if (decoderResult.getECLevel() != null) {
62 result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
64 results.addElement(result);
65 } catch (ReaderException re) {
66 // ignore and continue
69 if (results.isEmpty()) {
70 return EMPTY_RESULT_ARRAY;
72 Result[] resultArray = new Result[results.size()];
73 for (int i = 0; i < results.size(); i++) {
74 resultArray[i] = (Result) results.elementAt(i);