Improved datamatrix reader with new algorithm
[zxing.git] / core / src / com / google / zxing / multi / ByQuadrantReader.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;
18
19 import com.google.zxing.BinaryBitmap;
20 import com.google.zxing.ChecksumException;
21 import com.google.zxing.FormatException;
22 import com.google.zxing.NotFoundException;
23 import com.google.zxing.Reader;
24 import com.google.zxing.Result;
25
26 import java.util.Hashtable;
27
28 /**
29  * This class attempts to decode a barcode from an image, not by scanning the whole image,
30  * but by scanning subsets of the image. This is important when there may be multiple barcodes in
31  * an image, and detecting a barcode may find parts of multiple barcode and fail to decode
32  * (e.g. QR Codes). Instead this scans the four quadrants of the image -- and also the center
33  * 'quadrant' to cover the case where a barcode is found in the center.
34  *
35  * @see GenericMultipleBarcodeReader
36  */
37 public final class ByQuadrantReader implements Reader {
38
39   private final Reader delegate;
40
41   public ByQuadrantReader(Reader delegate) {
42     this.delegate = delegate;
43   }
44
45   public Result decode(BinaryBitmap image)
46       throws NotFoundException, ChecksumException, FormatException {
47     return decode(image, null);
48   }
49
50   public Result decode(BinaryBitmap image, Hashtable hints)
51       throws NotFoundException, ChecksumException, FormatException {
52
53     int width = image.getWidth();
54     int height = image.getHeight();
55     int halfWidth = width / 2;
56     int halfHeight = height / 2;
57
58     BinaryBitmap topLeft = image.crop(0, 0, halfWidth, halfHeight);
59     try {
60       return delegate.decode(topLeft, hints);
61     } catch (NotFoundException re) {
62       // continue
63     }
64
65     BinaryBitmap topRight = image.crop(halfWidth, 0, halfWidth, halfHeight);
66     try {
67       return delegate.decode(topRight, hints);
68     } catch (NotFoundException re) {
69       // continue
70     }
71
72     BinaryBitmap bottomLeft = image.crop(0, halfHeight, halfWidth, halfHeight);
73     try {
74       return delegate.decode(bottomLeft, hints);
75     } catch (NotFoundException re) {
76       // continue
77     }
78
79     BinaryBitmap bottomRight = image.crop(halfWidth, halfHeight, halfWidth, halfHeight);
80     try {
81       return delegate.decode(bottomRight, hints);
82     } catch (NotFoundException re) {
83       // continue
84     }
85
86     int quarterWidth = halfWidth / 2;
87     int quarterHeight = halfHeight / 2;
88     BinaryBitmap center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
89     return delegate.decode(center, hints);
90   }
91
92   public void reset() {
93     delegate.reset();
94   }
95
96 }