2 * Copyright 2007 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.j2se;
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 java.awt.image.BufferedImage;
27 * <p>An implementation based upon {@link BufferedImage}. This provides access to the
28 * underlying image as if it were a monochrome image. Behind the scenes, it is evaluating
29 * the luminance of the underlying image by retrieving its pixels' RGB values.</p>
31 * @author srowen@google.com (Sean Owen), Daniel Switkin (dswitkin@google.com)
33 public final class BufferedImageMonochromeBitmapSource implements MonochromeBitmapSource {
35 private final BufferedImage image;
36 private int blackPoint;
37 private BlackPointEstimationMethod lastMethod;
39 public BufferedImageMonochromeBitmapSource(BufferedImage image) {
44 public boolean isBlack(int x, int y) {
45 return computeRGBLuminance(image.getRGB(x, y)) < blackPoint;
48 public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
50 row = new BitArray(getWidth);
54 int[] pixelRow = image.getRGB(startX, y, getWidth, 1, null, 0, getWidth);
55 for (int i = 0; i < getWidth; i++) {
56 if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
63 public int getHeight() {
64 return image.getHeight();
67 public int getWidth() {
68 return image.getWidth();
71 public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
72 if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
73 if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(lastMethod)) {
74 int width = image.getWidth();
75 int height = image.getHeight();
76 int[] luminanceBuckets = new int[32];
77 int minDimension = width < height ? width : height;
78 int startI = height == minDimension ? 0 : (height - width) >> 1;
79 int startJ = width == minDimension ? 0 : (width - height) >> 1;
80 for (int n = 0; n < minDimension; n++) {
81 int pixel = image.getRGB(startJ + n, startI + n);
82 luminanceBuckets[computeRGBLuminance(pixel) >> 3]++;
84 blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3;
86 } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
89 throw new IllegalArgumentException("Unknown method: " + method);
94 public BlackPointEstimationMethod getLastEstimationMethod() {
99 * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB,
100 * so this implementation computes luminance is a function of a red, green and blue components as
103 * <code>Y = 0.299R + 0.587G + 0.114B</code>
105 * where R, G, and B are values in [0,1].
107 private static int computeRGBLuminance(int pixel) {
108 // Coefficients add up to 1024 to make the divide into a fast shift
109 return (306 * ((pixel >> 16) & 0xFF) +
110 601 * ((pixel >> 8) & 0xFF) +
111 117 * (pixel & 0xFF)) >> 10;