2 * Copyright 2009 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.multi;
19 import com.google.zxing.BinaryBitmap;
20 import com.google.zxing.Reader;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.Result;
24 import java.util.Hashtable;
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.
33 * @see GenericMultipleBarcodeReader
35 public final class ByQuadrantReader implements Reader {
37 private final Reader delegate;
39 public ByQuadrantReader(Reader delegate) {
40 this.delegate = delegate;
43 public Result decode(BinaryBitmap image) throws ReaderException {
44 return decode(image, null);
47 public Result decode(BinaryBitmap image, Hashtable hints) throws ReaderException {
49 int width = image.getWidth();
50 int height = image.getHeight();
51 int halfWidth = width / 2;
52 int halfHeight = height / 2;
54 BinaryBitmap topLeft = image.crop(0, 0, halfWidth, halfHeight);
56 return delegate.decode(topLeft, hints);
57 } catch (ReaderException re) {
61 BinaryBitmap topRight = image.crop(halfWidth, 0, width, halfHeight);
63 return delegate.decode(topRight, hints);
64 } catch (ReaderException re) {
68 BinaryBitmap bottomLeft = image.crop(0, halfHeight, halfWidth, height);
70 return delegate.decode(bottomLeft, hints);
71 } catch (ReaderException re) {
75 BinaryBitmap bottomRight = image.crop(halfWidth, halfHeight, width, height);
77 return delegate.decode(bottomRight, hints);
78 } catch (ReaderException re) {
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);