2 * Copyright (C) 2008 Google Inc.
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.client.android;
19 import android.graphics.Bitmap;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.common.BitArray;
24 import com.google.zxing.common.BlackPointEstimator;
27 * This object implements MonochromeBitmapSource around an Android Bitmap.
29 * @author dswitkin@google.com (Daniel Switkin)
30 * @author srowen@google.com (Sean Owen)
32 final class RGBMonochromeBitmapSource implements MonochromeBitmapSource {
34 private final Bitmap image;
35 private int blackPoint;
36 private BlackPointEstimationMethod lastMethod;
37 private int lastArgument;
39 private static final int LUMINANCE_BITS = 5;
40 private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
41 private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
43 RGBMonochromeBitmapSource(Bitmap image) {
50 public boolean isBlack(int x, int y) {
51 return computeRGBLuminance(image.getPixel(x, y)) < blackPoint;
54 public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
55 if (row == null || row.getSize() < getWidth) {
56 row = new BitArray(getWidth);
60 int[] pixelRow = new int[getWidth];
61 image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
63 // If the current decoder calculated the blackPoint based on one row, assume we're trying to
64 // decode a 1D barcode, and apply some sharpening.
65 // TODO: We may want to add a fifth parameter to request the amount of shapening to be done.
66 if (lastMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
67 int left = computeRGBLuminance(pixelRow[0]);
68 int center = computeRGBLuminance(pixelRow[1]);
69 for (int i = 1; i < getWidth - 1; i++) {
70 int right = computeRGBLuminance(pixelRow[i + 1]);
71 // Simple -1 4 -1 box filter with a weight of 2
72 int luminance = ((center << 2) - left - right) >> 1;
73 if (luminance < blackPoint) {
80 for (int i = 0; i < getWidth; i++) {
81 if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
89 public int getHeight() {
90 return image.height();
93 public int getWidth() {
97 public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
98 if (!method.equals(lastMethod) || argument != lastArgument) {
99 int width = image.width();
100 int height = image.height();
101 int[] histogram = new int[LUMINANCE_BUCKETS];
102 if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
103 int minDimension = width < height ? width : height;
104 int startI = height == minDimension ? 0 : (height - width) >> 1;
105 int startJ = width == minDimension ? 0 : (width - height) >> 1;
106 for (int n = 0; n < minDimension; n++) {
107 int pixel = image.getPixel(startJ + n, startI + n);
108 histogram[computeRGBLuminance(pixel) >> LUMINANCE_SHIFT]++;
110 } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
111 if (argument < 0 || argument >= height) {
112 throw new IllegalArgumentException("Row is not within the image: " + argument);
114 int[] pixelRow = new int[width];
115 image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
116 for (int x = 0; x < width; x++) {
117 histogram[computeRGBLuminance(pixelRow[x]) >> LUMINANCE_SHIFT]++;
120 throw new IllegalArgumentException("Unknown method: " + method);
122 blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
124 lastArgument = argument;
128 public BlackPointEstimationMethod getLastEstimationMethod() {
132 public MonochromeBitmapSource rotateCounterClockwise() {
133 throw new IllegalStateException("Rotate not supported");
136 public boolean isRotateSupported() {
141 * An optimized approximation of a more proper conversion from RGB to luminance which
142 * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
144 * @param pixel An ARGB input pixel
145 * @return An eight bit luminance value
147 private static int computeRGBLuminance(int pixel) {
148 // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
149 // the multiplies can be implemented as shifts.
153 // return ((((pixel >> 16) & 0xFF) << 8) +
154 // (((pixel >> 8) & 0xFF) << 9) +
155 // (( pixel & 0xFF) << 8)) >> 10;
157 // That is, we're replacing the coefficients in the original with powers of two,
158 // which can be implemented as shifts, even though changing the coefficients slightly
159 // corrupts the conversion. Not significant for our purposes.
161 // But we can get even cleverer and eliminate a few shifts:
162 return (((pixel & 0x00FF0000) >> 16) +
163 ((pixel & 0x0000FF00) >> 7) +
164 ( pixel & 0x000000FF )) >> 2;