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.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;
25 import java.util.Hashtable;
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.
34 * @see GenericMultipleBarcodeReader
36 public final class ByQuadrantReader implements Reader {
38 private final Reader delegate;
40 public ByQuadrantReader(Reader delegate) {
41 this.delegate = delegate;
44 public Result decode(MonochromeBitmapSource image) throws ReaderException {
45 return decode(image, null);
48 public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
50 int width = image.getWidth();
51 int height = image.getHeight();
52 int halfWidth = width / 2;
53 int halfHeight = height / 2;
55 MonochromeBitmapSource topLeft = new CroppedMonochromeBitmapSource(image, 0, 0, halfWidth,
58 return delegate.decode(topLeft, hints);
59 } catch (ReaderException re) {
63 MonochromeBitmapSource topRight = new CroppedMonochromeBitmapSource(image, halfWidth, 0, width,
66 return delegate.decode(topRight, hints);
67 } catch (ReaderException re) {
71 MonochromeBitmapSource bottomLeft = new CroppedMonochromeBitmapSource(image, 0, halfHeight,
74 return delegate.decode(bottomLeft, hints);
75 } catch (ReaderException re) {
79 MonochromeBitmapSource bottomRight = new CroppedMonochromeBitmapSource(image, halfWidth,
80 halfHeight, width, height);
82 return delegate.decode(bottomRight, hints);
83 } catch (ReaderException re) {
87 int quarterWidth = halfWidth / 2;
88 int quarterHeight = halfHeight / 2;
89 MonochromeBitmapSource center = new CroppedMonochromeBitmapSource(image,
93 height - quarterHeight);
94 return delegate.decode(center, hints);