|
| 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 org.reactnative.camera.utils; |
| 18 | + |
| 19 | +import com.google.zxing.Binarizer; |
| 20 | +import com.google.zxing.LuminanceSource; |
| 21 | +import com.google.zxing.NotFoundException; |
| 22 | +import com.google.zxing.common.*; |
| 23 | +import android.util.Log; |
| 24 | + |
| 25 | +/** |
| 26 | + * This Binarizer implementation uses the old ZXing global histogram approach. It is suitable |
| 27 | + * for low-end mobile devices which don't have enough CPU or memory to use a local thresholding |
| 28 | + * algorithm. However, because it picks a global black point, it cannot handle difficult shadows |
| 29 | + * and gradients. |
| 30 | + * |
| 31 | + * Faster mobile devices and all desktop applications should probably use HybridBinarizer instead. |
| 32 | + * |
| 33 | + * @author dswitkin@google.com (Daniel Switkin) |
| 34 | + * @author Sean Owen |
| 35 | + */ |
| 36 | +public class GlobalHistogramBinarizer extends Binarizer { |
| 37 | + |
| 38 | + private static final int LUMINANCE_BITS = 5; |
| 39 | + private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; |
| 40 | + private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; |
| 41 | + private static final byte[] EMPTY = new byte[0]; |
| 42 | + |
| 43 | + private byte[] luminances; |
| 44 | + private final int[] buckets; |
| 45 | + |
| 46 | + public GlobalHistogramBinarizer(LuminanceSource source) { |
| 47 | + super(source); |
| 48 | + luminances = EMPTY; |
| 49 | + buckets = new int[LUMINANCE_BUCKETS]; |
| 50 | + } |
| 51 | + |
| 52 | + // Applies simple sharpening to the row data to improve performance of the 1D Readers. |
| 53 | + @Override |
| 54 | + public BitArray getBlackRow(int y, BitArray row) throws NotFoundException { |
| 55 | + LuminanceSource source = getLuminanceSource(); |
| 56 | + int width = source.getWidth(); |
| 57 | + if (row == null || row.getSize() < width) { |
| 58 | + row = new BitArray(width); |
| 59 | + } else { |
| 60 | + row.clear(); |
| 61 | + } |
| 62 | + |
| 63 | + initArrays(width); |
| 64 | + byte[] localLuminances = source.getRow(y, luminances); |
| 65 | + int[] localBuckets = buckets; |
| 66 | + for (int x = 0; x < width; x++) { |
| 67 | + localBuckets[(localLuminances[x] & 0xff) >> LUMINANCE_SHIFT]++; |
| 68 | + } |
| 69 | + int blackPoint = estimateBlackPoint(localBuckets); |
| 70 | + |
| 71 | + if (width < 3) { |
| 72 | + // Special case for very small images |
| 73 | + for (int x = 0; x < width; x++) { |
| 74 | + if ((localLuminances[x] & 0xff) < blackPoint) { |
| 75 | + row.set(x); |
| 76 | + } |
| 77 | + } |
| 78 | + } else { |
| 79 | + int left = localLuminances[0] & 0xff; |
| 80 | + int center = localLuminances[1] & 0xff; |
| 81 | + for (int x = 1; x < width - 1; x++) { |
| 82 | + int right = localLuminances[x + 1] & 0xff; |
| 83 | + // A simple -1 4 -1 box filter with a weight of 2. |
| 84 | + if (((center * 4) - left - right) / 2 < blackPoint) { |
| 85 | + row.set(x); |
| 86 | + } |
| 87 | + left = center; |
| 88 | + center = right; |
| 89 | + } |
| 90 | + } |
| 91 | + return row; |
| 92 | + } |
| 93 | + |
| 94 | + // Does not sharpen the data, as this call is intended to only be used by 2D Readers. |
| 95 | + @Override |
| 96 | + public BitMatrix getBlackMatrix() throws NotFoundException { |
| 97 | + LuminanceSource source = getLuminanceSource(); |
| 98 | + int width = source.getWidth(); |
| 99 | + int height = source.getHeight(); |
| 100 | + BitMatrix matrix = new BitMatrix(width, height); |
| 101 | + |
| 102 | + // Quickly calculates the histogram by sampling four rows from the image. This proved to be |
| 103 | + // more robust on the blackbox tests than sampling a diagonal as we used to do. |
| 104 | + initArrays(width); |
| 105 | + int[] localBuckets = buckets; |
| 106 | + for (int y = 1; y < 5; y++) { |
| 107 | + int row = height * y / 5; |
| 108 | + byte[] localLuminances = source.getRow(row, luminances); |
| 109 | + int right = (width * 4) / 5; |
| 110 | + for (int x = width / 5; x < right; x++) { |
| 111 | + int pixel = localLuminances[x] & 0xff; |
| 112 | + localBuckets[pixel >> LUMINANCE_SHIFT]++; |
| 113 | + } |
| 114 | + } |
| 115 | + int blackPoint = estimateBlackPoint(localBuckets); |
| 116 | + |
| 117 | + // We delay reading the entire image luminance until the black point estimation succeeds. |
| 118 | + // Although we end up reading four rows twice, it is consistent with our motto of |
| 119 | + // "fail quickly" which is necessary for continuous scanning. |
| 120 | + byte[] localLuminances = source.getMatrix(); |
| 121 | + for (int y = 0; y < height; y++) { |
| 122 | + int offset = y * width; |
| 123 | + for (int x = 0; x < width; x++) { |
| 124 | + int pixel = localLuminances[offset + x] & 0xff; |
| 125 | + if (pixel < blackPoint) { |
| 126 | + matrix.set(x, y); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return matrix; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Binarizer createBinarizer(LuminanceSource source) { |
| 136 | + return new GlobalHistogramBinarizer(source); |
| 137 | + } |
| 138 | + |
| 139 | + private void initArrays(int luminanceSize) { |
| 140 | + if (luminances.length < luminanceSize) { |
| 141 | + luminances = new byte[luminanceSize]; |
| 142 | + } |
| 143 | + for (int x = 0; x < LUMINANCE_BUCKETS; x++) { |
| 144 | + buckets[x] = 0; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static int estimateBlackPoint(int[] buckets) throws NotFoundException { |
| 149 | + // Find the tallest peak in the histogram. |
| 150 | + int numBuckets = buckets.length; |
| 151 | + int maxBucketCount = 0; |
| 152 | + int firstPeak = 0; |
| 153 | + int firstPeakSize = 0; |
| 154 | + for (int x = 0; x < numBuckets; x++) { |
| 155 | + if (buckets[x] > firstPeakSize) { |
| 156 | + firstPeak = x; |
| 157 | + firstPeakSize = buckets[x]; |
| 158 | + } |
| 159 | + if (buckets[x] > maxBucketCount) { |
| 160 | + maxBucketCount = buckets[x]; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // Find the second-tallest peak which is somewhat far from the tallest peak. |
| 165 | + int secondPeak = 0; |
| 166 | + int secondPeakScore = 0; |
| 167 | + for (int x = 0; x < numBuckets; x++) { |
| 168 | + int distanceToBiggest = x - firstPeak; |
| 169 | + // Encourage more distant second peaks by multiplying by square of distance. |
| 170 | + int score = buckets[x] * distanceToBiggest * distanceToBiggest; |
| 171 | + if (score > secondPeakScore) { |
| 172 | + secondPeak = x; |
| 173 | + secondPeakScore = score; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Make sure firstPeak corresponds to the black peak. |
| 178 | + if (firstPeak > secondPeak) { |
| 179 | + int temp = firstPeak; |
| 180 | + firstPeak = secondPeak; |
| 181 | + secondPeak = temp; |
| 182 | + } |
| 183 | + |
| 184 | + // If there is too little contrast in the image to pick a meaningful black point, throw rather |
| 185 | + // than waste time trying to decode the image, and risk false positives. |
| 186 | + if (secondPeak - firstPeak <= numBuckets / 16) { |
| 187 | + //throw NotFoundException.getNotFoundInstance(); |
| 188 | + } |
| 189 | + |
| 190 | + // Find a valley between them that is low and closer to the white peak. |
| 191 | + int bestValley = secondPeak - 1; |
| 192 | + int bestValleyScore = -1; |
| 193 | + for (int x = secondPeak - 1; x > firstPeak; x--) { |
| 194 | + int fromFirst = x - firstPeak; |
| 195 | + int score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]); |
| 196 | + if (score > bestValleyScore) { |
| 197 | + bestValley = x; |
| 198 | + bestValleyScore = score; |
| 199 | + } |
| 200 | + } |
| 201 | + Log.d("mmmmm", "maxBucketCount:" + maxBucketCount + ",maxColor:" + firstPeak + "second:" + secondPeak + ",bestValley:" + bestValley + ",numBuckets:" + numBuckets); |
| 202 | + |
| 203 | + return bestValley << LUMINANCE_SHIFT; |
| 204 | + } |
| 205 | + |
| 206 | +} |
0 commit comments