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.common;
19 import com.google.zxing.Binarizer;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.ReaderException;
24 * This Binarizer implementation uses the old ZXing global histogram approach. It is suitable
25 * for low-end mobile devices which don't have enough CPU or memory to use a local thresholding
26 * algorithm. However, because it picks a global black point, it cannot handle difficult shadows
29 * @author dswitkin@google.com (Daniel Switkin)
32 public final class GlobalHistogramBinarizer extends Binarizer {
34 private static final int LUMINANCE_BITS = 5;
35 private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
36 private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
38 private byte[] luminances = null;
39 private int[] buckets = null;
41 public GlobalHistogramBinarizer(LuminanceSource source) {
45 // Applies simple sharpening to the row data to improve performance of the 1D Readers.
46 public BitArray getBlackRow(int y, BitArray row) throws ReaderException {
47 LuminanceSource source = getLuminanceSource();
48 int width = source.getWidth();
49 if (row == null || row.getSize() < width) {
50 row = new BitArray(width);
56 byte[] localLuminances = source.getRow(y, luminances);
57 int[] localBuckets = buckets;
58 for (int x = 0; x < width; x++) {
59 int pixel = localLuminances[x] & 0xff;
60 localBuckets[pixel >> LUMINANCE_SHIFT]++;
62 int blackPoint = estimateBlackPoint(localBuckets);
64 int left = localLuminances[0] & 0xff;
65 int center = localLuminances[1] & 0xff;
66 for (int x = 1; x < width - 1; x++) {
67 int right = localLuminances[x + 1] & 0xff;
68 // A simple -1 4 -1 box filter with a weight of 2.
69 int luminance = ((center << 2) - left - right) >> 1;
70 if (luminance < blackPoint) {
79 // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
80 public BitMatrix getBlackMatrix() throws ReaderException {
81 LuminanceSource source = getLuminanceSource();
82 int width = source.getWidth();
83 int height = source.getHeight();
84 BitMatrix matrix = new BitMatrix(width, height);
86 // Quickly calculates the histogram by sampling four rows from the image. This proved to be
87 // more robust on the blackbox tests than sampling a diagonal as we used to do.
89 int[] localBuckets = buckets;
90 for (int y = 1; y < 5; y++) {
91 int row = height * y / 5;
92 byte[] localLuminances = source.getRow(row, luminances);
93 int right = width * 4 / 5;
94 for (int x = width / 5; x < right; x++) {
95 int pixel = localLuminances[x] & 0xff;
96 localBuckets[pixel >> LUMINANCE_SHIFT]++;
99 int blackPoint = estimateBlackPoint(localBuckets);
101 // We delay reading the entire image luminance until the black point estimation succeeds.
102 // Although we end up reading four rows twice, it is consistent with our motto of
103 // "fail quickly" which is necessary for continuous scanning.
104 byte[] localLuminances = source.getMatrix();
105 for (int y = 0; y < height; y++) {
106 int offset = y * width;
107 for (int x = 0; x< width; x++) {
108 int pixel = localLuminances[offset + x] & 0xff;
109 if (pixel < blackPoint) {
118 public Binarizer createBinarizer(LuminanceSource source) {
119 return new GlobalHistogramBinarizer(source);
122 private void initArrays(int luminanceSize) {
123 if (luminances == null || luminances.length < luminanceSize) {
124 luminances = new byte[luminanceSize];
126 if (buckets == null) {
127 buckets = new int[LUMINANCE_BUCKETS];
129 for (int x = 0; x < LUMINANCE_BUCKETS; x++) {
135 private static int estimateBlackPoint(int[] buckets) throws ReaderException {
136 // Find the tallest peak in the histogram.
137 int numBuckets = buckets.length;
138 int maxBucketCount = 0;
140 int firstPeakSize = 0;
141 for (int i = 0; i < numBuckets; i++) {
142 if (buckets[i] > firstPeakSize) {
144 firstPeakSize = buckets[i];
146 if (buckets[i] > maxBucketCount) {
147 maxBucketCount = buckets[i];
151 // Find the second-tallest peak which is somewhat far from the tallest peak.
153 int secondPeakScore = 0;
154 for (int i = 0; i < numBuckets; i++) {
155 int distanceToBiggest = i - firstPeak;
156 // Encourage more distant second peaks by multiplying by square of distance.
157 int score = buckets[i] * distanceToBiggest * distanceToBiggest;
158 if (score > secondPeakScore) {
160 secondPeakScore = score;
164 // Make sure firstPeak corresponds to the black peak.
165 if (firstPeak > secondPeak) {
166 int temp = firstPeak;
167 firstPeak = secondPeak;
171 // If there is too little contrast in the image to pick a meaningful black point, throw rather
172 // than waste time trying to decode the image, and risk false positives.
173 // TODO: It might be worth comparing the brightest and darkest pixels seen, rather than the
174 // two peaks, to determine the contrast.
175 if (secondPeak - firstPeak <= numBuckets >> 4) {
176 throw ReaderException.getInstance();
179 // Find a valley between them that is low and closer to the white peak.
180 int bestValley = secondPeak - 1;
181 int bestValleyScore = -1;
182 for (int i = secondPeak - 1; i > firstPeak; i--) {
183 int fromFirst = i - firstPeak;
184 int score = fromFirst * fromFirst * (secondPeak - i) * (maxBucketCount - buckets[i]);
185 if (score > bestValleyScore) {
187 bestValleyScore = score;
191 return bestValley << LUMINANCE_SHIFT;