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 * This object extends LuminanceSource around an array of YUV data returned from the camera driver,
21 * with the option to crop to a rectangle within the full data. This can be used to exclude
22 * superfluous pixels around the perimeter and speed up decoding.
24 * It works for any pixel format where the Y channel is planar and appears first, including
25 * YCbCr_420_SP and YCbCr_422_SP.
27 * @author dswitkin@google.com (Daniel Switkin)
29 public final class PlanarYUVLuminanceSource extends LuminanceSource {
31 private static final int THUMBNAIL_SCALE_FACTOR = 2;
33 private final byte[] yuvData;
34 private final int dataWidth;
35 private final int dataHeight;
36 private final int left;
37 private final int top;
39 public PlanarYUVLuminanceSource(byte[] yuvData,
46 boolean reverseHorizontal) {
49 if (left + width > dataWidth || top + height > dataHeight) {
50 throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
53 this.yuvData = yuvData;
54 this.dataWidth = dataWidth;
55 this.dataHeight = dataHeight;
58 if (reverseHorizontal) {
59 reverseHorizontal(width, height);
64 public byte[] getRow(int y, byte[] row) {
65 if (y < 0 || y >= getHeight()) {
66 throw new IllegalArgumentException("Requested row is outside the image: " + y);
68 int width = getWidth();
69 if (row == null || row.length < width) {
70 row = new byte[width];
72 int offset = (y + top) * dataWidth + left;
73 System.arraycopy(yuvData, offset, row, 0, width);
78 public byte[] getMatrix() {
79 int width = getWidth();
80 int height = getHeight();
82 // If the caller asks for the entire underlying image, save the copy and give them the
83 // original data. The docs specifically warn that result.length must be ignored.
84 if (width == dataWidth && height == dataHeight) {
88 int area = width * height;
89 byte[] matrix = new byte[area];
90 int inputOffset = top * dataWidth + left;
92 // If the width matches the full width of the underlying data, perform a single copy.
93 if (width == dataWidth) {
94 System.arraycopy(yuvData, inputOffset, matrix, 0, area);
98 // Otherwise copy one cropped row at a time.
100 for (int y = 0; y < height; y++) {
101 int outputOffset = y * width;
102 System.arraycopy(yuv, inputOffset, matrix, outputOffset, width);
103 inputOffset += dataWidth;
109 public boolean isCropSupported() {
114 public LuminanceSource crop(int left, int top, int width, int height) {
115 return new PlanarYUVLuminanceSource(yuvData,
125 public int[] renderThumbnail() {
126 int width = getWidth() / THUMBNAIL_SCALE_FACTOR;
127 int height = getHeight() / THUMBNAIL_SCALE_FACTOR;
128 int[] pixels = new int[width * height];
129 byte[] yuv = yuvData;
130 int inputOffset = top * dataWidth + left;
132 for (int y = 0; y < height; y++) {
133 int outputOffset = y * width;
134 for (int x = 0; x < width; x++) {
135 int grey = yuv[inputOffset + x * THUMBNAIL_SCALE_FACTOR] & 0xff;
136 pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
138 inputOffset += dataWidth * THUMBNAIL_SCALE_FACTOR;
144 * @return width of image from {@link #renderThumbnail()}
146 public int getThumbnailWidth() {
147 return getWidth() / THUMBNAIL_SCALE_FACTOR;
151 * @return height of image from {@link #renderThumbnail()}
153 public int getThumbnailHeight() {
154 return getHeight() / THUMBNAIL_SCALE_FACTOR;
157 private void reverseHorizontal(int width, int height) {
158 byte[] yuvData = this.yuvData;
159 for (int y = 0, rowStart = top * dataWidth + left; y < height; y++, rowStart += dataWidth) {
160 int middle = rowStart + width / 2;
161 for (int x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) {
162 byte temp = yuvData[x1];
163 yuvData[x1] = yuvData[x2];