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 com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.common.BitArray;
22 import com.google.zxing.common.BlackPointEstimator;
24 import android.graphics.Bitmap;
27 * This object implements MonochromeBitmapSource around an Android Bitmap. Rather than capturing an
28 * RGB image and calculating the grey value at each pixel, we ask the camera driver for YUV data and
29 * strip out the luminance channel directly. This should be faster but provides fewer bits, i.e.
32 * @author dswitkin@google.com (Daniel Switkin)
33 * @author srowen@google.com (Sean Owen)
35 final class YUVMonochromeBitmapSource implements MonochromeBitmapSource {
37 private final Bitmap image;
38 private final BitArray[] blackWhitePixels;
39 private final int width;
40 private final int height;
41 private int blackPoint;
42 private BlackPointEstimationMethod lastMethod;
43 private int lastArgument;
45 private static final int LUMINANCE_BITS = 5;
46 private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
47 private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
49 YUVMonochromeBitmapSource(Bitmap image) {
50 width = image.width();
51 height = image.height();
53 blackWhitePixels = new BitArray[height];
59 public boolean isBlack(int x, int y) {
60 BitArray blackWhite = blackWhitePixels[y];
61 if (blackWhite == null) {
62 blackWhite = parseBlackWhite(y);
64 return blackWhite.get(x);
67 public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
68 BitArray blackWhite = blackWhitePixels[y];
69 if (blackWhite == null) {
70 blackWhite = parseBlackWhite(y);
73 if (startX == 0 && getWidth == width) {
76 row = new BitArray(getWidth);
80 for (int i = 0; i < getWidth; i++) {
81 if (blackWhite.get(startX + i)) {
88 private BitArray parseBlackWhite(int y) {
89 int width = this.width;
90 int[] pixelRow = new int[width];
91 image.getPixels(pixelRow, 0, width, 0, y, width, 1);
92 BitArray luminanceRow = new BitArray(width);
93 int blackPoint = this.blackPoint;
94 // Calculate 32 bits at a time to more efficiently set the bit array
97 for (int j = 0; j < width; j++) {
99 // Computation of luminance is inlined here for speed:
100 if (((pixelRow[j] >> 16) & 0xFF) <= blackPoint) {
103 if (++bitCount == 32) {
104 luminanceRow.setBulk(j, bits);
110 luminanceRow.setBulk(width, bits >>> (32 - bitCount));
112 blackWhitePixels[y] = luminanceRow;
116 public int getHeight() {
120 public int getWidth() {
124 public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
125 if (!method.equals(lastMethod) || argument != lastArgument) {
126 for (int i = 0; i < blackWhitePixels.length; i++) {
127 blackWhitePixels[i] = null;
129 int[] histogram = new int[LUMINANCE_BUCKETS];
130 if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
131 int minDimension = width < height ? width : height;
132 int startI = height == minDimension ? 0 : (height - width) >> 1;
133 int startJ = width == minDimension ? 0 : (width - height) >> 1;
134 for (int n = 0; n < minDimension; n++) {
135 int pixel = image.getPixel(startJ + n, startI + n);
136 // Computation of luminance is inlined here for speed:
137 histogram[((pixel >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
139 } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
140 if (argument < 0 || argument >= height) {
141 throw new IllegalArgumentException("Row is not within the image: " + argument);
143 int[] yuvArray = new int[width];
144 image.getPixels(yuvArray, 0, width, 0, argument, width, 1);
145 for (int x = 0; x < width; x++) {
146 histogram[((yuvArray[x] >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
149 throw new IllegalArgumentException("Unknown method: " + method);
151 blackPoint = BlackPointEstimator.estimate(histogram, 1.0f) << LUMINANCE_SHIFT;
153 lastArgument = argument;
157 public BlackPointEstimationMethod getLastEstimationMethod() {