Lots of updates:
[zxing.git] / android / src / com / google / zxing / client / android / YUVLuminanceSource.java
1 /*
2  * Copyright 2009 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.android;
18
19 import com.google.zxing.LuminanceSource;
20
21 import android.graphics.Bitmap;
22
23 /**
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.
27  *
28  * @author dswitkin@google.com (Daniel Switkin)
29  */
30 public final class YUVLuminanceSource extends LuminanceSource {
31
32   private final byte[] yuvData;
33   private final int dataWidth;
34   private final int dataHeight;
35   private final int left;
36   private final int top;
37
38   public YUVLuminanceSource(byte[] yuvData, int dataWidth, int dataHeight, int left, int top,
39       int width, int height) {
40     super(width, height);
41
42     if (left + width > dataWidth || top + height > dataHeight) {
43       throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
44     }
45
46     this.yuvData = yuvData;
47     this.dataWidth = dataWidth;
48     this.dataHeight = dataHeight;
49     this.left = left;
50     this.top = top;
51   }
52
53   public byte[] getRow(int y, byte[] row) {
54     if (y < 0 || y >= getHeight()) {
55       throw new IllegalArgumentException("Requested row is outside the image: " + y);
56     }
57     int width = getWidth();
58     if (row == null || row.length < width) {
59       row = new byte[width];
60     }
61     int offset = (y + top) * dataWidth + left;
62     System.arraycopy(yuvData, offset, row, 0, width);
63     return row;
64   }
65
66   public byte[] getMatrix() {
67     int width = getWidth();
68     int height = getHeight();
69
70     // If the caller asks for the entire underlying image, save the copy and give them the
71     // original data. The docs specifically warn that result.length must be ignored.
72     if (width == dataWidth && height == dataHeight) {
73       return yuvData;
74     }
75
76     int area = width * height;
77     byte[] matrix = new byte[area];
78     int inputOffset = top * dataWidth + left;
79
80     // If the width matches the full width of the underlying data, perform a single copy.
81     if (width == dataWidth) {
82       System.arraycopy(yuvData, inputOffset, matrix, 0, area);
83       return matrix;
84     }
85
86     // Otherwise copy one cropped row at a time.
87     byte[] yuv = yuvData;
88     for (int y = 0; y < height; y++) {
89       int outputOffset = y * width;
90       System.arraycopy(yuv, inputOffset, matrix, outputOffset, width);
91       inputOffset += dataWidth;
92     }
93     return matrix;
94   }
95
96   public boolean isCropSupported() {
97     return true;
98   }
99
100   public LuminanceSource crop(int left, int top, int width, int height) {
101     return new YUVLuminanceSource(yuvData, dataWidth, dataHeight, left, top, width, height);
102   }
103
104   /**
105    * Creates a greyscale Android Bitmap from the YUV data based on the crop rectangle.
106    *
107    * @return An 8888 bitmap.
108    */
109   public Bitmap renderToBitmap() {
110     int width = getWidth();
111     int height = getHeight();
112     int[] pixels = new int[width * height];
113     byte[] yuv = yuvData;
114     int inputOffset = top * dataWidth + left;
115
116     for (int y = 0; y < height; y++) {
117       int outputOffset = y * width;
118       for (int x = 0; x < width; x++) {
119         int grey = yuv[inputOffset + x] & 0xff;
120         pixels[outputOffset + x] = (0xff << 24) | (grey << 16) | (grey << 8) | grey;
121       }
122       inputOffset += dataWidth;
123     }
124
125     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
126     bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
127     return bitmap;
128   }
129
130 }