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