Package com.google.zxing.common
Class HybridBinarizer
java.lang.Object
com.google.zxing.Binarizer
com.google.zxing.common.GlobalHistogramBinarizer
com.google.zxing.common.HybridBinarizer
This class implements a local thresholding algorithm, which while slower than the
GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for
high frequency images of barcodes with black data on white backgrounds. For this application,
it does a much better job than a global blackpoint with severe shadows and gradients.
However it tends to produce artifacts on lower frequency images and is therefore not
a good general purpose binarizer for uses outside ZXing.
This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,
and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already
inherently local, and only fails for horizontal gradients. We can revisit that problem later,
but for now it was not a win to use local blocks for 1D.
This Binarizer is the default for the unit tests and the recommended class for library users.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final int
private static final int
private BitMatrix
private static final int
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static int[][]
calculateBlackPoints
(byte[] luminances, int subWidth, int subHeight, int width, int height) Calculates a single black point for each block of pixels and saves it away.private static void
calculateThresholdForBlock
(byte[] luminances, int subWidth, int subHeight, int width, int height, int[][] blackPoints, BitMatrix matrix) For each block in the image, calculate the average black point using a 5x5 grid of the blocks around it.private static int
cap
(int value, int max) createBinarizer
(LuminanceSource source) Creates a new object with the same type as this Binarizer implementation, but with pristine state.Calculates the final BitMatrix once for all requests.private static void
thresholdBlock
(byte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix) Applies a single threshold to a block of pixels.Methods inherited from class com.google.zxing.common.GlobalHistogramBinarizer
getBlackRow
Methods inherited from class com.google.zxing.Binarizer
getHeight, getLuminanceSource, getWidth
-
Field Details
-
BLOCK_SIZE_POWER
private static final int BLOCK_SIZE_POWER- See Also:
-
BLOCK_SIZE
private static final int BLOCK_SIZE- See Also:
-
BLOCK_SIZE_MASK
private static final int BLOCK_SIZE_MASK- See Also:
-
MINIMUM_DIMENSION
private static final int MINIMUM_DIMENSION- See Also:
-
MIN_DYNAMIC_RANGE
private static final int MIN_DYNAMIC_RANGE- See Also:
-
matrix
-
-
Constructor Details
-
HybridBinarizer
-
-
Method Details
-
getBlackMatrix
Calculates the final BitMatrix once for all requests. This could be called once from the constructor instead, but there are some advantages to doing it lazily, such as making profiling easier, and not doing heavy lifting when callers don't expect it.- Overrides:
getBlackMatrix
in classGlobalHistogramBinarizer
- Returns:
- The 2D array of bits for the image (true means black).
- Throws:
NotFoundException
- if image can't be binarized to make a matrix
-
createBinarizer
Description copied from class:Binarizer
Creates a new object with the same type as this Binarizer implementation, but with pristine state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache of 1 bit data. See Effective Java for why we can't use Java's clone() method.- Overrides:
createBinarizer
in classGlobalHistogramBinarizer
- Parameters:
source
- The LuminanceSource this Binarizer will operate on.- Returns:
- A new concrete Binarizer implementation object.
-
calculateThresholdForBlock
private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight, int width, int height, int[][] blackPoints, BitMatrix matrix) For each block in the image, calculate the average black point using a 5x5 grid of the blocks around it. Also handles the corner cases (fractional blocks are computed based on the last pixels in the row/column which are also used in the previous block). -
cap
private static int cap(int value, int max) -
thresholdBlock
private static void thresholdBlock(byte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix) Applies a single threshold to a block of pixels. -
calculateBlackPoints
private static int[][] calculateBlackPoints(byte[] luminances, int subWidth, int subHeight, int width, int height) Calculates a single black point for each block of pixels and saves it away. See the following thread for a discussion of this algorithm: http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
-