2 * Copyright 2009 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.LuminanceSource;
20 import com.google.zxing.ReaderException;
22 import java.awt.Image;
23 import java.awt.image.PixelGrabber;
26 * An implementation based on AWT's Image representation. This can be used on CDC devices
27 * or other devices that do not have access to the Mobile Information Device Profile
28 * and thus do not have access to javax.microedition.lcdui.Image.
30 * @author dswitkin@google.com (Daniel Switkin)
31 * @author David Albert
34 public final class AWTImageLuminanceSource extends LuminanceSource {
36 private final int[] pixels;
38 public AWTImageLuminanceSource(Image image) throws ReaderException {
39 super(image.getWidth(null), image.getHeight(null));
41 int width = getWidth();
42 int height = getHeight();
43 pixels = new int[width * height];
45 // Seems best in this situation to grab all pixels upfront. Grabbing any individual pixel
46 // entails creating a relatively expensive object and calling through several methods.
47 PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
50 } catch (InterruptedException ie) {
51 throw ReaderException.getInstance();
55 public byte[] getRow(int y, byte[] row) {
56 if (y < 0 || y >= getHeight()) {
57 throw new IllegalArgumentException("Requested row is outside the image: " + y);
59 int width = getWidth();
60 if (row == null || row.length < width) {
61 row = new byte[width];
64 int offset = y * width;
65 for (int x = 0; x < width; x++) {
66 int pixel = pixels[offset + x];
67 int luminance = (((pixel & 0x00FF0000) >> 16) +
68 ((pixel & 0x0000FF00) >> 7) +
69 (pixel & 0x000000FF )) >> 2;
70 row[x] = (byte) luminance;
75 public byte[] getMatrix() {
76 int width = getWidth();
77 int height = getHeight();
78 int area = width * height;
79 byte[] matrix = new byte[area];
81 for (int y = 0; y < height; y++) {
82 int offset = y * width;
83 for (int x = 0; x < width; x++) {
84 int pixel = pixels[offset + x];
85 int luminance = (((pixel & 0x00FF0000) >> 16) +
86 ((pixel & 0x0000FF00) >> 7) +
87 (pixel & 0x000000FF )) >> 2;
88 matrix[x] = (byte) luminance;