Use TRY_HARDER hint in javase CommandLineRunner. TRY_HARDER now tries rotating the...
[zxing.git] / android / src / com / google / zxing / client / android / YUVMonochromeBitmapSource.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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 com.google.zxing.client.android;
18
19 import android.graphics.Bitmap;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.common.BitArray;
23 import com.google.zxing.common.BlackPointEstimator;
24
25 /**
26  * This object implements MonochromeBitmapSource around an Android Bitmap. Rather than capturing an
27  * RGB image and calculating the grey value at each pixel, we ask the camera driver for YUV data and
28  * strip out the luminance channel directly. This should be faster but provides fewer bits, i.e.
29  * fewer grey levels.
30  *
31  * @author dswitkin@google.com (Daniel Switkin)
32  * @author srowen@google.com (Sean Owen)
33  */
34 final class YUVMonochromeBitmapSource implements MonochromeBitmapSource {
35
36   private final Bitmap image;
37   private int blackPoint;
38   private BlackPointEstimationMethod lastMethod;
39   private int lastArgument;
40
41   private static final int LUMINANCE_BITS = 5;
42   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
43   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
44
45   YUVMonochromeBitmapSource(Bitmap image) {
46     this.image = image;
47     blackPoint = 0x7F;
48     lastMethod = null;
49     lastArgument = 0;
50   }
51
52   public boolean isBlack(int x, int y) {
53     return ((image.getPixel(x, y) >> 16) & 0xFF) < blackPoint;
54   }
55
56   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
57     if (row == null) {
58       row = new BitArray(getWidth);
59     } else {
60       row.clear();
61     }
62     int[] pixelRow = new int[getWidth];
63      image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
64     for (int i = 0; i < getWidth; i++) {
65       if (((pixelRow[i] >> 16) & 0xFF) < blackPoint) {
66         row.set(i);
67       }
68     }
69     return row;
70   }
71
72   public int getHeight() {
73     return image.height();
74   }
75
76   public int getWidth() {
77     return image.width();
78   }
79
80   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
81     if (!method.equals(lastMethod) || argument != lastArgument) {
82       int width = image.width();
83       int height = image.height();
84       int[] histogram = new int[LUMINANCE_BUCKETS];
85       float biasTowardsWhite = 1.0f;
86       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
87         int minDimension = width < height ? width : height;
88         int startI = height == minDimension ? 0 : (height - width) >> 1;
89         int startJ = width == minDimension ? 0 : (width - height) >> 1;
90         for (int n = 0; n < minDimension; n++) {
91           int pixel = image.getPixel(startJ + n, startI + n);
92           histogram[((pixel >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
93         }
94       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
95         if (argument < 0 || argument >= height) {
96           throw new IllegalArgumentException("Row is not within the image: " + argument);
97         }
98         biasTowardsWhite = 2.0f;
99         int[] pixelRow = new int[width];
100         image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
101         for (int x = 0; x < width; x++) {
102           histogram[((pixelRow[x] >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
103         }
104       } else {
105         throw new IllegalArgumentException("Unknown method: " + method);
106       }
107       blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
108       lastMethod = method;
109       lastArgument = argument;
110     }
111   }
112
113   public BlackPointEstimationMethod getLastEstimationMethod() {
114     return lastMethod;
115   }
116
117   public MonochromeBitmapSource rotateCounterClockwise() {
118     throw new IllegalStateException("Rotate not supported");
119   }
120
121   public boolean isRotatedSupported() {
122     return false;
123   }
124
125 }