a7279e2e92252f0c9920f11af2ad2732ad873a75
[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     BinaryBitmap topLeft = image.crop(0, 0, halfWidth, halfHeight);
55     try {
56       return delegate.decode(topLeft, hints);
57     } catch (ReaderException re) {
58       // continue
59     }
60
61     BinaryBitmap topRight = image.crop(halfWidth, 0, width, halfHeight);
62     try {
63       return delegate.decode(topRight, hints);
64     } catch (ReaderException re) {
65       // continue
66     }
67
68     BinaryBitmap bottomLeft = image.crop(0, halfHeight, halfWidth, height);
69     try {
70       return delegate.decode(bottomLeft, hints);
71     } catch (ReaderException re) {
72       // continue
73     }
74
75     BinaryBitmap bottomRight = image.crop(halfWidth, halfHeight, width, height);
76     try {
77       return delegate.decode(bottomRight, hints);
78     } catch (ReaderException re) {
79       // continue
80     }
81
82     int quarterWidth = halfWidth / 2;
83     int quarterHeight = halfHeight / 2;
84     BinaryBitmap center = image.crop(quarterWidth, quarterHeight, width - quarterWidth,
85         height - quarterHeight);
86     return delegate.decode(center, hints);
87   }
88
89 }