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;
55 BinaryBitmap topLeft = image.crop(0, 0, halfWidth, halfHeight);
57 return delegate.decode(topLeft, hints);
58 } catch (ReaderException re) {
64 BinaryBitmap topRight = image.crop(halfWidth, 0, width, halfHeight);
66 return delegate.decode(topRight, hints);
67 } catch (ReaderException re) {
73 BinaryBitmap bottomLeft = image.crop(0, halfHeight, halfWidth, height);
75 return delegate.decode(bottomLeft, hints);
76 } catch (ReaderException re) {
82 BinaryBitmap bottomRight = image.crop(halfWidth, halfHeight, width, height);
84 return delegate.decode(bottomRight, hints);
85 } catch (ReaderException re) {
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);