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 row[x] = toLuminance(rgbData[x]);
70 public byte[] getMatrix() {
71 int width = getWidth();
72 int height = getHeight();
73 int area = width * height;
74 byte[] matrix = new byte[area];
76 int[] rgb = new int[area];
77 image.getRGB(rgb, 0, width, 0, 0, width, height);
78 for (int y = 0; y < height; y++) {
79 int offset = y * width;
80 for (int x = 0; x < width; x++) {
81 matrix[offset + x] = toLuminance(rgb[offset + x]);
87 public boolean isRotateSupported() {
91 public LuminanceSource rotateCounterClockwise() {
92 return new CCRotatedLCDUIImageLuminanceSource(image);
95 static byte toLuminance(int pixel) {
96 return (byte) ((((pixel & 0x00FF0000) >> 16) +
97 ((pixel & 0x0000FF00) >> 7) +
98 (pixel & 0x000000FF )) >> 2);
102 * A variant on {@link LCDUIImageLuminanceSource} that acts as if the input is rotated 90 degrees
105 private static final class CCRotatedLCDUIImageLuminanceSource extends LuminanceSource {
107 private final Image image;
108 private int[] rgbData;
110 private CCRotatedLCDUIImageLuminanceSource(Image image) {
111 super(image.getHeight(), image.getWidth());
115 public byte[] getRow(int y, byte[] row) {
116 int height = getHeight();
117 if (y < 0 || y >= height) {
118 throw new IllegalArgumentException("Requested row is outside the image: " + y);
120 int width = getWidth();
121 if (row == null || row.length < width) {
122 row = new byte[width];
125 if (rgbData == null || rgbData.length < width) {
126 rgbData = new int[width];
128 image.getRGB(rgbData, 0, height, height - 1 - y, 0, 1, width);
129 for (int x = 0; x < width; x++) {
130 row[x] = toLuminance(rgbData[x]);
135 public byte[] getMatrix() {
136 int width = getWidth();
137 int height = getHeight();
138 int area = width * height;
139 byte[] matrix = new byte[area];
141 int[] rgb = new int[area];
142 image.getRGB(rgb, 0, width, 0, 0, width, height);
143 // This flips x/y in the target to result in a rotated image
144 int offset = height * (width - 1);
145 for (int y = 0; y < height; y++) {
146 for (int x = 0; x < width; x++) {
147 matrix[offset - height * x + y] = toLuminance(rgb[y * width + x]);