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.j2me;
19 import com.google.zxing.LuminanceSource;
21 import javax.microedition.lcdui.Image;
24 * A LuminanceSource based on Java ME's Image class. It does not support cropping or rotation.
26 * @author dswitkin@google.com (Daniel Switkin)
29 public final class LCDUIImageLuminanceSource extends LuminanceSource {
31 private final Image image;
32 private int[] rgbData;
34 public LCDUIImageLuminanceSource(Image image) {
35 super(image.getWidth(), image.getHeight());
39 // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
40 // the multiplies can be implemented as shifts.
44 // return ((((pixel >> 16) & 0xFF) << 8) +
45 // (((pixel >> 8) & 0xFF) << 9) +
46 // (( pixel & 0xFF) << 8)) >> 10;
48 // That is, we're replacing the coefficients in the original with powers of two,
49 // which can be implemented as shifts, even though changing the coefficients slightly
50 // alters the conversion. The difference is not significant for our purposes.
51 public byte[] getRow(int y, byte[] row) {
52 if (y < 0 || y >= getHeight()) {
53 throw new IllegalArgumentException("Requested row is outside the image: " + y);
55 int width = getWidth();
56 if (row == null || row.length < width) {
57 row = new byte[width];
60 if (rgbData == null || rgbData.length < width) {
61 rgbData = new int[width];
63 image.getRGB(rgbData, 0, width, 0, y, width, 1);
64 for (int x = 0; x < width; x++) {
65 int pixel = rgbData[x];
66 int luminance = (((pixel & 0x00FF0000) >> 16) +
67 ((pixel & 0x0000FF00) >> 7) +
68 (pixel & 0x000000FF )) >> 2;
69 row[x] = (byte) luminance;
74 public byte[] getMatrix() {
75 int width = getWidth();
76 int height = getHeight();
77 int area = width * height;
78 byte[] matrix = new byte[area];
80 int[] rgb = new int[area];
81 image.getRGB(rgb, 0, width, 0, 0, width, height);
82 for (int y = 0; y < height; y++) {
83 int offset = y * width;
84 for (int x = 0; x < width; x++) {
85 int pixel = rgb[offset + x];
86 int luminance = (((pixel & 0x00FF0000) >> 16) +
87 ((pixel & 0x0000FF00) >> 7) +
88 (pixel & 0x000000FF )) >> 2;
89 matrix[offset + x] = (byte) luminance;