Support SMTP URLs
[zxing.git] / core / src / com / google / zxing / Binarizer.java
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     if (source == null) {
36       throw new IllegalArgumentException("Source must be non-null.");
37     }
38     this.source = source;
39   }
40
41   public LuminanceSource getLuminanceSource() {
42     return source;
43   }
44
45   /**
46    * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
47    * cached data. Callers should assume this method is expensive and call it as seldom as possible.
48    * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
49    * For callers which only examine one row of pixels at a time, the same BitArray should be reused
50    * and passed in with each call for performance. However it is legal to keep more than one row
51    * at a time if needed.
52    *
53    * @param y The row to fetch, 0 <= y < bitmap height.
54    * @param row An optional preallocated array. If null or too small, it will be ignored.
55    *            If used, the Binarizer will call BitArray.clear(). Always use the returned object.
56    * @return The array of bits for this row (true means black).
57    */
58   public abstract BitArray getBlackRow(int y, BitArray row) throws NotFoundException;
59
60   /**
61    * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive
62    * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
63    * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
64    * fetched using getBlackRow(), so don't mix and match between them.
65    *
66    * @return The 2D array of bits for the image (true means black).
67    */
68   public abstract BitMatrix getBlackMatrix() throws NotFoundException;
69
70   /**
71    * Creates a new object with the same type as this Binarizer implementation, but with pristine
72    * state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache
73    * of 1 bit data. See Effective Java for why we can't use Java's clone() method.
74    *
75    * @param source The LuminanceSource this Binarizer will operate on.
76    * @return A new concrete Binarizer implementation object.
77    */
78   public abstract Binarizer createBinarizer(LuminanceSource source);
79
80 }