Alternate multi QR Code reader from Hannes
[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.MonochromeBitmapSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.Result;
23 import com.google.zxing.ResultMetadataType;
24 import com.google.zxing.ResultPoint;
25 import com.google.zxing.common.DecoderResult;
26 import com.google.zxing.common.DetectorResult;
27 import com.google.zxing.multi.MultipleBarcodeReader;
28 import com.google.zxing.multi.qrcode.detector.MultiDetector;
29 import com.google.zxing.qrcode.QRCodeReader;
30
31 import java.util.Hashtable;
32 import java.util.Vector;
33
34 /**
35  * This implementation can detect and decode multiple QR Codes in an image.
36  *
37  * @author Sean Owen
38  * @author Hannes Erven
39  */
40 public final class QRCodeMultiReader extends QRCodeReader implements MultipleBarcodeReader {
41
42   private static final Result[] EMPTY_RESULT_ARRAY = new Result[0];
43
44   public Result[] decodeMultiple(MonochromeBitmapSource image) throws ReaderException {
45     return decodeMultiple(image, null);
46   }
47
48   public Result[] decodeMultiple(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
49     Vector results = new Vector();
50     DetectorResult[] detectorResult = new MultiDetector(image).detectMulti(hints);
51     for (int i = 0; i < detectorResult.length; i++) {
52       try {
53         DecoderResult decoderResult = getDecoder().decode(detectorResult[i].getBits());
54         ResultPoint[] points = detectorResult[i].getPoints();
55         Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
56         if (decoderResult.getByteSegments() != null) {
57           result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
58         }
59         results.addElement(result);
60       } catch (ReaderException re) {
61         // ignore and continue 
62       }
63     }
64     if (results.isEmpty()) {
65       return EMPTY_RESULT_ARRAY;
66     } else {
67       Result[] resultArray = new Result[results.size()];
68       for (int i = 0; i < results.size(); i++) {
69         resultArray[i] = (Result) results.elementAt(i);
70       }
71       return resultArray;
72     }
73   }
74
75 }