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.ChecksumException;
21 import com.google.zxing.FormatException;
22 import com.google.zxing.NotFoundException;
23 import com.google.zxing.Reader;
24 import com.google.zxing.Result;
26 import java.util.Hashtable;
29 * This class attempts to decode a barcode from an image, not by scanning the whole image,
30 * but by scanning subsets of the image. This is important when there may be multiple barcodes in
31 * an image, and detecting a barcode may find parts of multiple barcode and fail to decode
32 * (e.g. QR Codes). Instead this scans the four quadrants of the image -- and also the center
33 * 'quadrant' to cover the case where a barcode is found in the center.
35 * @see GenericMultipleBarcodeReader
37 public final class ByQuadrantReader implements Reader {
39 private final Reader delegate;
41 public ByQuadrantReader(Reader delegate) {
42 this.delegate = delegate;
45 public Result decode(BinaryBitmap image)
46 throws NotFoundException, ChecksumException, FormatException {
47 return decode(image, null);
50 public Result decode(BinaryBitmap image, Hashtable hints)
51 throws NotFoundException, ChecksumException, FormatException {
53 int width = image.getWidth();
54 int height = image.getHeight();
55 int halfWidth = width / 2;
56 int halfHeight = height / 2;
58 BinaryBitmap topLeft = image.crop(0, 0, halfWidth, halfHeight);
60 return delegate.decode(topLeft, hints);
61 } catch (NotFoundException re) {
65 BinaryBitmap topRight = image.crop(halfWidth, 0, halfWidth, halfHeight);
67 return delegate.decode(topRight, hints);
68 } catch (NotFoundException re) {
72 BinaryBitmap bottomLeft = image.crop(0, halfHeight, halfWidth, halfHeight);
74 return delegate.decode(bottomLeft, hints);
75 } catch (NotFoundException re) {
79 BinaryBitmap bottomRight = image.crop(halfWidth, halfHeight, halfWidth, halfHeight);
81 return delegate.decode(bottomRight, hints);
82 } catch (NotFoundException re) {
86 int quarterWidth = halfWidth / 2;
87 int quarterHeight = halfHeight / 2;
88 BinaryBitmap center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
89 return delegate.decode(center, hints);