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;
20 * The purpose of this class hierarchy is to abstract different bitmap implementations across
21 * platforms into a standard interface for requesting greyscale luminance values. The interface
22 * only provides immutable methods; therefore crop and rotation create copies. This is to ensure
23 * that one Reader does not modify the original luminance source and leave it in an unknown state
24 * for other Readers in the chain.
26 * @author dswitkin@google.com (Daniel Switkin)
28 public abstract class LuminanceSource {
30 private final int width;
31 private final int height;
33 protected LuminanceSource(int width, int height) {
39 * Fetches one row of luminance data from the underlying platform's bitmap. Values range from
40 * 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have
41 * to bitwise and with 0xff for each value. It is preferable for implementations of this method
42 * to only fetch this row rather than the whole image, since no 2D Readers may be installed and
43 * getMatrix() may never be called.
45 * @param y The row to fetch, 0 <= y < getHeight().
46 * @param row An optional preallocated array. If null or too small, it will be ignored.
47 * Always use the returned object, and ignore the .length of the array.
48 * @return An array containing the luminance data.
50 public abstract byte[] getRow(int y, byte[] row);
53 * Fetches luminance data for the underlying bitmap. Values should be fetched using:
54 * int luminance = array[y * width + x] & 0xff;
56 * @return A row-major 2D array of luminance values. Do not use result.length as it may be
57 * larger than width * height bytes on some platforms. Do not modify the contents
60 public abstract byte[] getMatrix();
63 * @return The width of the bitmap.
65 public final int getWidth() {
70 * @return The height of the bitmap.
72 public final int getHeight() {
77 * @return Whether this subclass supports cropping.
79 public boolean isCropSupported() {
84 * Returns a new object with cropped image data. Implementations may keep a reference to the
85 * original data rather than a copy. Only callable if isCropSupported() is true.
87 * @param left The left coordinate, 0 <= left < getWidth().
88 * @param top The top coordinate, 0 <= top <= getHeight().
89 * @param width The width of the rectangle to crop.
90 * @param height The height of the rectangle to crop.
91 * @return A cropped version of this object.
93 public LuminanceSource crop(int left, int top, int width, int height) {
94 throw new RuntimeException("This luminance source does not support cropping.");
98 * @return Whether this subclass supports counter-clockwise rotation.
100 public boolean isRotateSupported() {
105 * Returns a new object with rotated image data. Only callable if isRotateSupported() is true.
107 * @return A rotated version of this object.
109 public LuminanceSource rotateCounterClockwise() {
110 throw new RuntimeException("This luminance source does not support rotation.");