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.android;
19 import com.google.zxing.LuminanceSource;
21 import android.graphics.Bitmap;
24 * This object extends LuminanceSource around an array of YUV data returned from the camera driver,
25 * with the option to crop to a rectangle within the full data. This can be used to exclude
26 * superfluous pixels around the perimeter and speed up decoding.
28 * It works for any pixel format where the Y channel is planar and appears first, including
29 * YCbCr_420_SP and YCbCr_422_SP.
31 * @author dswitkin@google.com (Daniel Switkin)
33 public final class PlanarYUVLuminanceSource extends LuminanceSource {
34 private final byte[] yuvData;
35 private final int dataWidth;
36 private final int dataHeight;
37 private final int left;
38 private final int top;
40 public PlanarYUVLuminanceSource(byte[] yuvData, int dataWidth, int dataHeight, int left, int top,
41 int width, int height) {
44 if (left + width > dataWidth || top + height > dataHeight) {
45 throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
48 this.yuvData = yuvData;
49 this.dataWidth = dataWidth;
50 this.dataHeight = dataHeight;
56 public byte[] getRow(int y, byte[] row) {
57 if (y < 0 || y >= getHeight()) {
58 throw new IllegalArgumentException("Requested row is outside the image: " + y);
60 int width = getWidth();
61 if (row == null || row.length < width) {
62 row = new byte[width];
64 int offset = (y + top) * dataWidth + left;
65 System.arraycopy(yuvData, offset, row, 0, width);
70 public byte[] getMatrix() {
71 int width = getWidth();
72 int height = getHeight();
74 // If the caller asks for the entire underlying image, save the copy and give them the
75 // original data. The docs specifically warn that result.length must be ignored.
76 if (width == dataWidth && height == dataHeight) {
80 int area = width * height;
81 byte[] matrix = new byte[area];
82 int inputOffset = top * dataWidth + left;
84 // If the width matches the full width of the underlying data, perform a single copy.
85 if (width == dataWidth) {
86 System.arraycopy(yuvData, inputOffset, matrix, 0, area);
90 // Otherwise copy one cropped row at a time.
92 for (int y = 0; y < height; y++) {
93 int outputOffset = y * width;
94 System.arraycopy(yuv, inputOffset, matrix, outputOffset, width);
95 inputOffset += dataWidth;
101 public boolean isCropSupported() {
105 public int getDataWidth() {
109 public int getDataHeight() {
113 public Bitmap renderCroppedGreyscaleBitmap() {
114 int width = getWidth();
115 int height = getHeight();
116 int[] pixels = new int[width * height];
117 byte[] yuv = yuvData;
118 int inputOffset = top * dataWidth + left;
120 for (int y = 0; y < height; y++) {
121 int outputOffset = y * width;
122 for (int x = 0; x < width; x++) {
123 int grey = yuv[inputOffset + x] & 0xff;
124 pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
126 inputOffset += dataWidth;
129 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
130 bitmap.setPixels(pixels, 0, width, 0, 0, width, height);