d3797d9e8ee1af44c751edad3f5026cc783fd128
[zxing.git] / core / src / com / google / zxing / multi / qrcode / detector / MultiDetector.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.detector;
18
19 import com.google.zxing.FormatException;
20 import com.google.zxing.NotFoundException;
21 import com.google.zxing.common.BitMatrix;
22 import com.google.zxing.common.DetectorResult;
23 import com.google.zxing.qrcode.detector.Detector;
24 import com.google.zxing.qrcode.detector.FinderPatternInfo;
25
26 import java.util.Hashtable;
27 import java.util.Vector;
28
29 /**
30  * <p>Encapsulates logic that can detect one or more QR Codes in an image, even if the QR Code
31  * is rotated or skewed, or partially obscured.</p>
32  *
33  * @author Sean Owen
34  * @author Hannes Erven
35  */
36 public final class MultiDetector extends Detector {
37
38   private static final DetectorResult[] EMPTY_DETECTOR_RESULTS = new DetectorResult[0];
39
40   public MultiDetector(BitMatrix image) {
41     super(image);
42   }
43
44   public DetectorResult[] detectMulti(Hashtable hints) throws NotFoundException {
45     BitMatrix image = getImage();
46     MultiFinderPatternFinder finder = new MultiFinderPatternFinder(image);
47     FinderPatternInfo[] info = finder.findMulti(hints);
48
49     if (info == null || info.length == 0) {
50       throw NotFoundException.getNotFoundInstance();
51     }
52
53     Vector result = new Vector();
54     for (int i = 0; i < info.length; i++) {
55       try {
56         result.addElement(processFinderPatternInfo(info[i]));
57       } catch (FormatException e) {
58         // ignore
59       }
60     }
61     if (result.isEmpty()) {
62       return EMPTY_DETECTOR_RESULTS;
63     } else {
64       DetectorResult[] resultArray = new DetectorResult[result.size()];
65       for (int i = 0; i < result.size(); i++) {
66         resultArray[i] = (DetectorResult) result.elementAt(i);
67       }
68       return resultArray;
69     }
70   }
71
72 }