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