| 1 | /* |
| 2 | * Copyright 2009 ZXing authors |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package com.google.zxing; |
| 18 | |
| 19 | import com.google.zxing.common.BitArray; |
| 20 | import com.google.zxing.common.BitMatrix; |
| 21 | |
| 22 | /** |
| 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. |
| 25 | * |
| 26 | * @author dswitkin@google.com (Daniel Switkin) |
| 27 | */ |
| 28 | public final class BinaryBitmap { |
| 29 | |
| 30 | private final Binarizer binarizer; |
| 31 | private BitMatrix matrix; |
| 32 | |
| 33 | public BinaryBitmap(Binarizer binarizer) { |
| 34 | if (binarizer == null) { |
| 35 | throw new IllegalArgumentException("Binarizer must be non-null."); |
| 36 | } |
| 37 | this.binarizer = binarizer; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return The width of the bitmap. |
| 42 | */ |
| 43 | public int getWidth() { |
| 44 | return binarizer.getWidth(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return The height of the bitmap. |
| 49 | */ |
| 50 | public int getHeight() { |
| 51 | return binarizer.getHeight(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return |
| 56 | * cached data. Callers should assume this method is expensive and call it as seldom as possible. |
| 57 | * This method is intended for decoding 1D barcodes and may choose to apply sharpening. |
| 58 | * |
| 59 | * @param y The row to fetch, which must be in [0, bitmap height) |
| 60 | * @param row An optional preallocated array. If null or too small, it will be ignored. |
| 61 | * If used, the Binarizer will call BitArray.clear(). Always use the returned object. |
| 62 | * @return The array of bits for this row (true means black). |
| 63 | * @throws NotFoundException if row can't be binarized |
| 64 | */ |
| 65 | public BitArray getBlackRow(int y, BitArray row) throws NotFoundException { |
| 66 | return binarizer.getBlackRow(y, row); |
| 67 | } |
| 68 | |
| 69 | /** |
| 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. |
| 74 | * |
| 75 | * @return The 2D array of bits for the image (true means black). |
| 76 | * @throws NotFoundException if image can't be binarized to make a matrix |
| 77 | */ |
| 78 | public BitMatrix getBlackMatrix() throws NotFoundException { |
| 79 | // The matrix is created on demand the first time it is requested, then cached. There are two |
| 80 | // reasons for this: |
| 81 | // 1. This work will never be done if the caller only installs 1D Reader objects, or if a |
| 82 | // 1D Reader finds a barcode before the 2D Readers run. |
| 83 | // 2. This work will only be done once even if the caller installs multiple 2D Readers. |
| 84 | if (matrix == null) { |
| 85 | matrix = binarizer.getBlackMatrix(); |
| 86 | } |
| 87 | return matrix; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return Whether this bitmap can be cropped. |
| 92 | */ |
| 93 | public boolean isCropSupported() { |
| 94 | return binarizer.getLuminanceSource().isCropSupported(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns a new object with cropped image data. Implementations may keep a reference to the |
| 99 | * original data rather than a copy. Only callable if isCropSupported() is true. |
| 100 | * |
| 101 | * @param left The left coordinate, which must be in [0,getWidth()) |
| 102 | * @param top The top coordinate, which must be in [0,getHeight()) |
| 103 | * @param width The width of the rectangle to crop. |
| 104 | * @param height The height of the rectangle to crop. |
| 105 | * @return A cropped version of this object. |
| 106 | */ |
| 107 | public BinaryBitmap crop(int left, int top, int width, int height) { |
| 108 | LuminanceSource newSource = binarizer.getLuminanceSource().crop(left, top, width, height); |
| 109 | return new BinaryBitmap(binarizer.createBinarizer(newSource)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return Whether this bitmap supports counter-clockwise rotation. |
| 114 | */ |
| 115 | public boolean isRotateSupported() { |
| 116 | return binarizer.getLuminanceSource().isRotateSupported(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns a new object with rotated image data by 90 degrees counterclockwise. |
| 121 | * Only callable if {@link #isRotateSupported()} is true. |
| 122 | * |
| 123 | * @return A rotated version of this object. |
| 124 | */ |
| 125 | public BinaryBitmap rotateCounterClockwise() { |
| 126 | LuminanceSource newSource = binarizer.getLuminanceSource().rotateCounterClockwise(); |
| 127 | return new BinaryBitmap(binarizer.createBinarizer(newSource)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns a new object with rotated image data by 45 degrees counterclockwise. |
| 132 | * Only callable if {@link #isRotateSupported()} is true. |
| 133 | * |
| 134 | * @return A rotated version of this object. |
| 135 | */ |
| 136 | public BinaryBitmap rotateCounterClockwise45() { |
| 137 | LuminanceSource newSource = binarizer.getLuminanceSource().rotateCounterClockwise45(); |
| 138 | return new BinaryBitmap(binarizer.createBinarizer(newSource)); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public String toString() { |
| 143 | try { |
| 144 | return getBlackMatrix().toString(); |
| 145 | } catch (NotFoundException e) { |
| 146 | return ""; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |