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.j2se;
19 import com.google.zxing.LuminanceSource;
21 import java.awt.image.BufferedImage;
22 import java.awt.image.BufferedImageOp;
23 import java.awt.image.AffineTransformOp;
24 import java.awt.geom.AffineTransform;
27 * This LuminanceSource implementation is meant for J2SE clients and our blackbox unit tests.
29 * @author dswitkin@google.com (Daniel Switkin)
32 public final class BufferedImageLuminanceSource extends LuminanceSource {
34 private final BufferedImage image;
35 private final int left;
36 private final int top;
37 private int[] rgbData;
39 public BufferedImageLuminanceSource(BufferedImage image) {
40 this(image, 0, 0, image.getWidth(), image.getHeight());
43 public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width,
47 int sourceWidth = image.getWidth();
48 int sourceHeight = image.getHeight();
49 if (left + width > sourceWidth || top + height > sourceHeight) {
50 throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
58 // These methods use an integer calculation for luminance derived from:
59 // <code>Y = 0.299R + 0.587G + 0.114B</code>
61 public byte[] getRow(int y, byte[] row) {
62 if (y < 0 || y >= getHeight()) {
63 throw new IllegalArgumentException("Requested row is outside the image: " + y);
65 int width = getWidth();
66 if (row == null || row.length < width) {
67 row = new byte[width];
70 if (rgbData == null || rgbData.length < width) {
71 rgbData = new int[width];
73 image.getRGB(left, top + y, width, 1, rgbData, 0, width);
74 for (int x = 0; x < width; x++) {
75 int pixel = rgbData[x];
76 int luminance = (306 * ((pixel >> 16) & 0xFF) +
77 601 * ((pixel >> 8) & 0xFF) +
78 117 * (pixel & 0xFF)) >> 10;
79 row[x] = (byte) luminance;
85 public byte[] getMatrix() {
86 int width = getWidth();
87 int height = getHeight();
88 int area = width * height;
89 byte[] matrix = new byte[area];
91 int[] rgb = new int[area];
92 image.getRGB(left, top, width, height, rgb, 0, width);
93 for (int y = 0; y < height; y++) {
94 int offset = y * width;
95 for (int x = 0; x < width; x++) {
96 int pixel = rgb[offset + x];
97 int luminance = (306 * ((pixel >> 16) & 0xFF) +
98 601 * ((pixel >> 8) & 0xFF) +
99 117 * (pixel & 0xFF)) >> 10;
100 matrix[offset + x] = (byte) luminance;
107 public boolean isCropSupported() {
112 public LuminanceSource crop(int left, int top, int width, int height) {
113 return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
116 // Can't run AffineTransforms on images of unknown format.
118 public boolean isRotateSupported() {
119 return image.getType() != BufferedImage.TYPE_CUSTOM;
123 public LuminanceSource rotateCounterClockwise() {
124 if (!isRotateSupported()) {
125 throw new IllegalStateException("Rotate not supported");
127 int sourceWidth = image.getWidth();
128 int sourceHeight = image.getHeight();
130 // Rotate 90 degrees counterclockwise.
131 AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
132 BufferedImageOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
134 // Note width/height are flipped since we are rotating 90 degrees.
135 BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, image.getType());
136 op.filter(image, rotatedImage);
138 // Maintain the cropped region, but rotate it too.
139 int width = getWidth();
140 return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width),