72a7d681a6bbd30062382a9f1dab40e1e91aff53
[zxing.git] / core / src / com / google / zxing / multi / qrcode / QRCodeMultiReader.java
1 /*
2  * Copyright 2009 ZXing authors
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.multi.qrcode;
18
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;
31
32 import java.util.Hashtable;
33 import java.util.Vector;
34
35 /**
36  * This implementation can detect and decode multiple QR Codes in an image.
37  *
38  * @author Sean Owen
39  * @author Hannes Erven
40  */
41 public final class QRCodeMultiReader extends QRCodeReader implements MultipleBarcodeReader {
42
43   private static final Result[] EMPTY_RESULT_ARRAY = new Result[0];
44
45   public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException {
46     return decodeMultiple(image, null);
47   }
48
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++) {
53       try {
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());
60         }
61         if (decoderResult.getECLevel() != null) {
62           result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
63         }
64         results.addElement(result);
65       } catch (ReaderException re) {
66         // ignore and continue 
67       }
68     }
69     if (results.isEmpty()) {
70       return EMPTY_RESULT_ARRAY;
71     } else {
72       Result[] resultArray = new Result[results.size()];
73       for (int i = 0; i < results.size(); i++) {
74         resultArray[i] = (Result) results.elementAt(i);
75       }
76       return resultArray;
77     }
78   }
79
80 }