1 /** 2 * Copyright (C) 2022 The Android Open Source Project 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.android.settingslib.qrcode; 18 19 import com.google.zxing.LuminanceSource; 20 21 public class QrYuvLuminanceSource extends LuminanceSource { 22 23 private byte[] mYuvData; 24 private int mWidth; 25 private int mHeight; 26 QrYuvLuminanceSource(byte[] yuvData, int width, int height)27 public QrYuvLuminanceSource(byte[] yuvData, int width, int height) { 28 super(width, height); 29 30 mWidth = width; 31 mHeight = height; 32 mYuvData = yuvData; 33 } 34 35 @Override isCropSupported()36 public boolean isCropSupported() { 37 return true; 38 } 39 40 @Override crop(int left, int top, int crop_width, int crop_height)41 public LuminanceSource crop(int left, int top, int crop_width, int crop_height) { 42 final byte[] newImage = new byte[crop_width * crop_height]; 43 int inputOffset = top * mWidth + left; 44 45 if (left + crop_width > mWidth || top + crop_height > mHeight) { 46 throw new IllegalArgumentException("cropped rectangle does not fit within image data."); 47 } 48 49 for (int y = 0; y < crop_height; y++) { 50 System.arraycopy(mYuvData, inputOffset, newImage, y * crop_width, crop_width); 51 inputOffset += mWidth; 52 } 53 return new QrYuvLuminanceSource(newImage, crop_width, crop_height); 54 } 55 56 @Override getRow(int y, byte[] row)57 public byte[] getRow(int y, byte[] row) { 58 if (y < 0 || y >= mHeight) { 59 throw new IllegalArgumentException("Requested row is outside the image: " + y); 60 } 61 if (row == null || row.length < mWidth) { 62 row = new byte[mWidth]; 63 } 64 System.arraycopy(mYuvData, y * mWidth, row, 0, mWidth); 65 return row; 66 } 67 68 @Override getMatrix()69 public byte[] getMatrix() { 70 return mYuvData; 71 } 72 } 73