| 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 hierarchy provides a set of methods to convert luminance data to 1 bit data. |
| 24 | * It allows the algorithm to vary polymorphically, for example allowing a very expensive |
| 25 | * thresholding technique for servers and a fast one for mobile. It also permits the implementation |
| 26 | * to vary, e.g. a JNI version for Android and a Java fallback version for other platforms. |
| 27 | * |
| 28 | * @author dswitkin@google.com (Daniel Switkin) |
| 29 | */ |
| 30 | public abstract class Binarizer { |
| 31 | |
| 32 | private final LuminanceSource source; |
| 33 | |
| 34 | protected Binarizer(LuminanceSource source) { |
| 35 | this.source = source; |
| 36 | } |
| 37 | |
| 38 | public final LuminanceSource getLuminanceSource() { |
| 39 | return source; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return |
| 44 | * cached data. Callers should assume this method is expensive and call it as seldom as possible. |
| 45 | * This method is intended for decoding 1D barcodes and may choose to apply sharpening. |
| 46 | * For callers which only examine one row of pixels at a time, the same BitArray should be reused |
| 47 | * and passed in with each call for performance. However it is legal to keep more than one row |
| 48 | * at a time if needed. |
| 49 | * |
| 50 | * @param y The row to fetch, which must be in [0, bitmap height) |
| 51 | * @param row An optional preallocated array. If null or too small, it will be ignored. |
| 52 | * If used, the Binarizer will call BitArray.clear(). Always use the returned object. |
| 53 | * @return The array of bits for this row (true means black). |
| 54 | * @throws NotFoundException if row can't be binarized |
| 55 | */ |
| 56 | public abstract BitArray getBlackRow(int y, BitArray row) throws NotFoundException; |
| 57 | |
| 58 | /** |
| 59 | * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive |
| 60 | * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or |
| 61 | * may not apply sharpening. Therefore, a row from this matrix may not be identical to one |
| 62 | * fetched using getBlackRow(), so don't mix and match between them. |
| 63 | * |
| 64 | * @return The 2D array of bits for the image (true means black). |
| 65 | * @throws NotFoundException if image can't be binarized to make a matrix |
| 66 | */ |
| 67 | public abstract BitMatrix getBlackMatrix() throws NotFoundException; |
| 68 | |
| 69 | /** |
| 70 | * Creates a new object with the same type as this Binarizer implementation, but with pristine |
| 71 | * state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache |
| 72 | * of 1 bit data. See Effective Java for why we can't use Java's clone() method. |
| 73 | * |
| 74 | * @param source The LuminanceSource this Binarizer will operate on. |
| 75 | * @return A new concrete Binarizer implementation object. |
| 76 | */ |
| 77 | public abstract Binarizer createBinarizer(LuminanceSource source); |
| 78 | |
| 79 | public final int getWidth() { |
| 80 | return source.getWidth(); |
| 81 | } |
| 82 | |
| 83 | public final int getHeight() { |
| 84 | return source.getHeight(); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |