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;
19 import com.google.zxing.common.BitArray;
20 import com.google.zxing.common.BitMatrix;
23 * This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
24 * accept a BinaryBitmap and attempt to decode it.
26 * @author dswitkin@google.com (Daniel Switkin)
28 public final class BinaryBitmap {
30 private final Binarizer binarizer;
31 private BitMatrix matrix;
33 public BinaryBitmap(Binarizer binarizer) {
34 if (binarizer == null) {
35 throw new IllegalArgumentException("Binarizer must be non-null.");
37 this.binarizer = binarizer;
42 * @return The width of the bitmap.
44 public int getWidth() {
45 return binarizer.getLuminanceSource().getWidth();
49 * @return The height of the bitmap.
51 public int getHeight() {
52 return binarizer.getLuminanceSource().getHeight();
56 * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
57 * cached data. Callers should assume this method is expensive and call it as seldom as possible.
58 * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
60 * @param y The row to fetch, 0 <= y < bitmap height.
61 * @param row An optional preallocated array. If null or too small, it will be ignored.
62 * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
63 * @return The array of bits for this row (true means black).
65 public BitArray getBlackRow(int y, BitArray row) throws NotFoundException {
66 return binarizer.getBlackRow(y, row);
70 * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
71 * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
72 * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
73 * fetched using getBlackRow(), so don't mix and match between them.
75 * @return The 2D array of bits for the image (true means black).
77 public BitMatrix getBlackMatrix() throws NotFoundException {
78 // The matrix is created on demand the first time it is requested, then cached. There are two
80 // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
81 // 1D Reader finds a barcode before the 2D Readers run.
82 // 2. This work will only be done once even if the caller installs multiple 2D Readers.
84 matrix = binarizer.getBlackMatrix();
90 * @return Whether this bitmap can be cropped.
92 public boolean isCropSupported() {
93 return binarizer.getLuminanceSource().isCropSupported();
97 * Returns a new object with cropped image data. Implementations may keep a reference to the
98 * original data rather than a copy. Only callable if isCropSupported() is true.
100 * @param left The left coordinate, 0 <= left < getWidth().
101 * @param top The top coordinate, 0 <= top <= getHeight().
102 * @param width The width of the rectangle to crop.
103 * @param height The height of the rectangle to crop.
104 * @return A cropped version of this object.
106 public BinaryBitmap crop(int left, int top, int width, int height) {
107 LuminanceSource newSource = binarizer.getLuminanceSource().crop(left, top, width, height);
108 return new BinaryBitmap(binarizer.createBinarizer(newSource));
112 * @return Whether this bitmap supports counter-clockwise rotation.
114 public boolean isRotateSupported() {
115 return binarizer.getLuminanceSource().isRotateSupported();
119 * Returns a new object with rotated image data. Only callable if isRotateSupported() is true.
121 * @return A rotated version of this object.
123 public BinaryBitmap rotateCounterClockwise() {
124 LuminanceSource newSource = binarizer.getLuminanceSource().rotateCounterClockwise();
125 return new BinaryBitmap(binarizer.createBinarizer(newSource));