2 * Copyright 2008 ZXing authors
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.bug;
19 import com.google.zxing.ReaderException;
20 import com.google.zxing.common.BaseMonochromeBitmapSource;
22 import java.awt.Image;
23 import java.awt.image.PixelGrabber;
26 * <p>An implementation based on AWT's {@link Image} representation.
27 * This can be used on CDC devices or other devices that do not have access to the
28 * Mobile Information Device Profile and thus do not have access to
29 * javax.microedition.lcdui.Image.</p>
31 * @author David Albert
34 public final class AWTImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
36 private final int[] pixels;
38 public AWTImageMonochromeBitmapSource(Image image) throws ReaderException {
39 super(image.getWidth(null), image.getHeight(null));
40 int height = getHeight();
41 int width = getWidth();
42 pixels = new int[height * width];
43 // Seems best in this situation to grab all pixels upfront. Grabbing any individual pixel
44 // entails creating a relatively expensive object and calling through several methods.
45 PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
48 } catch (InterruptedException ie) {
49 throw ReaderException.getInstance();
54 * See <code>com.google.zxing.client.j2me.LCDUIImageMonochromeBitmapSource</code> for more explanation
55 * of the computation used in this method.
57 public int getLuminance(int x, int y) {
58 int pixel = pixels[y * getWidth() + x];
59 return (((pixel & 0x00FF0000) >> 16) +
60 ((pixel & 0x0000FF00) >> 7) +
61 (pixel & 0x000000FF )) >> 2;
64 public int[] getLuminanceRow(int y, int[] row) {
65 int width = getWidth();
66 if (row == null || row.length < width) {
69 int offset = y * width;
70 for (int x = 0; x < width; x++) {
71 int pixel = pixels[offset + x];
72 row[x] = (((pixel & 0x00FF0000) >> 16) +
73 ((pixel & 0x0000FF00) >> 7) +
74 (pixel & 0x000000FF )) >> 2;
79 public int[] getLuminanceColumn(int x, int[] column) {
80 int height = getHeight();
81 int width = getWidth();
82 if (column == null || column.length < height) {
83 column = new int[height];
86 for (int y = 0; y < height; y++) {
87 int pixel = pixels[offset];
88 column[y] = (((pixel & 0x00FF0000) >> 16) +
89 ((pixel & 0x0000FF00) >> 7) +
90 (pixel & 0x000000FF )) >> 2;