2 * Copyright 2007 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.common.BaseMonochromeBitmapSource;
21 import javax.microedition.lcdui.Image;
24 * <p>An implementation based on Java ME's {@link Image} representation.</p>
26 * @author Sean Owen (srowen@google.com), Daniel Switkin (dswitkin@google.com)
28 public final class LCDUIImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
30 private final Image image;
31 private final int height;
32 private final int width;
33 private final int[] rgbRow;
34 private final int[] pixelHolder;
35 private int cachedRow;
37 public LCDUIImageMonochromeBitmapSource(Image image) {
39 height = image.getHeight();
40 width = image.getWidth();
41 rgbRow = new int[width];
42 pixelHolder = new int[1];
46 public int getHeight() {
50 public int getWidth() {
54 public int getLuminance(int x, int y) {
59 image.getRGB(pixelHolder, 0, width, x, y, 1, 1);
60 pixel = pixelHolder[0];
63 // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
64 // the multiplies can be implemented as shifts.
68 // return ((((pixel >> 16) & 0xFF) << 8) +
69 // (((pixel >> 8) & 0xFF) << 9) +
70 // (( pixel & 0xFF) << 8)) >> 10;
72 // That is, we're replacing the coefficients in the original with powers of two,
73 // which can be implemented as shifts, even though changing the coefficients slightly
74 // corrupts the conversion. Not significant for our purposes.
75 return (((pixel & 0x00FF0000) >> 16) +
76 ((pixel & 0x0000FF00) >> 7) +
77 (pixel & 0x000000FF )) >> 2;
80 public void cacheRowForLuminance(int y) {
82 image.getRGB(rgbRow, 0, width, 0, y, width, 1);