Android camera2 獲取的yuv420_8888轉(zhuǎn)argb_8888

調(diào)用android的camera2獲取到的原始圖像數(shù)據(jù)是YUV_420_8888,是無法直接存jpg的,所以需要先利用renderscript進行轉(zhuǎn)換

public static void onImageAvailable(Context context,Image image)

? ? {

? ? ? ? try {

? ? ? ? ? ? // Get the YUV data

//? ? ? ? final Image image = reader.acquireLatestImage();

? ? ? ? ? ? final ByteBuffer yuvBytes = imageToByteBuffer(image);

? ? ? ? ? ? // Convert YUV to RGB

? ? ? ? ? ? final RenderScript rs = RenderScript.create(context);

? ? ? ? ? ? final Bitmap? ? ? ? bitmap? ? = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);

? ? ? ? ? ? final Allocation allocationRgb = Allocation.createFromBitmap(rs, bitmap);

? ? ? ? ? ? final Allocation allocationYuv = Allocation.createSized(rs, Element.U8(rs), yuvBytes.array().length);

? ? ? ? ? ? allocationYuv.copyFrom(yuvBytes.array());

? ? ? ? ? ? ScriptIntrinsicYuvToRGB scriptYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));

? ? ? ? ? ? scriptYuvToRgb.setInput(allocationYuv);

? ? ? ? ? ? scriptYuvToRgb.forEach(allocationRgb);

? ? ? ? ? ? allocationRgb.copyTo(bitmap);

? ? ? ? ? ? String path = context.getExternalFilesDir("/pictures").getAbsolutePath();

? ? ? ? ? ? File file = new File(path+"/"+System.currentTimeMillis()+".jpg");

? ? ? ? ? ? OutputStream outputStream = new FileOutputStream(file);

? ? ? ? ? ? bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

? ? ? ? ? ? outputStream.flush();

? ? ? ? ? ? outputStream.close();

? ? ? ? ? ? // Release

? ? ? ? ? ? bitmap.recycle();

? ? ? ? ? ? allocationYuv.destroy();

? ? ? ? ? ? allocationRgb.destroy();

? ? ? ? ? ? rs.destroy();

? ? ? ? ? ? image.close();

? ? ? ? }catch (Exception e){

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? public static ByteBuffer imageToByteBuffer(final Image image)

? ? {

? ? ? ? final Rect crop? = image.getCropRect();

? ? ? ? final int? width? = crop.width();

? ? ? ? final int? height = crop.height();

? ? ? ? final Image.Plane[] planes? ? = image.getPlanes();

? ? ? ? final byte[]? ? ? ? rowData? ? = new byte[planes[0].getRowStride()];

? ? ? ? final int? ? ? ? ? bufferSize = width * height * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8;

? ? ? ? final ByteBuffer? ? output? ? = ByteBuffer.allocateDirect(bufferSize);

? ? ? ? int channelOffset = 0;

? ? ? ? int outputStride = 0;

? ? ? ? for (int planeIndex = 0; planeIndex < 3; planeIndex++)

? ? ? ? {

? ? ? ? ? ? if (planeIndex == 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? channelOffset = 0;

? ? ? ? ? ? ? ? outputStride = 1;

? ? ? ? ? ? }

? ? ? ? ? ? else if (planeIndex == 1)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? channelOffset = width * height + 1;

? ? ? ? ? ? ? ? outputStride = 2;

? ? ? ? ? ? }

? ? ? ? ? ? else if (planeIndex == 2)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? channelOffset = width * height;

? ? ? ? ? ? ? ? outputStride = 2;

? ? ? ? ? ? }

? ? ? ? ? ? final ByteBuffer buffer? ? ? = planes[planeIndex].getBuffer();

? ? ? ? ? ? final int? ? ? ? rowStride? = planes[planeIndex].getRowStride();

? ? ? ? ? ? final int? ? ? ? pixelStride = planes[planeIndex].getPixelStride();

? ? ? ? ? ? final int shift? ? ? ? = (planeIndex == 0) ? 0 : 1;

? ? ? ? ? ? final int widthShifted? = width >> shift;

? ? ? ? ? ? final int heightShifted = height >> shift;

? ? ? ? ? ? buffer.position(rowStride * (crop.top >> shift) + pixelStride * (crop.left >> shift));

? ? ? ? ? ? for (int row = 0; row < heightShifted; row++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? final int length;

? ? ? ? ? ? ? ? if (pixelStride == 1 && outputStride == 1)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? length = widthShifted;

? ? ? ? ? ? ? ? ? ? buffer.get(output.array(), channelOffset, length);

? ? ? ? ? ? ? ? ? ? channelOffset += length;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? length = (widthShifted - 1) * pixelStride + 1;

? ? ? ? ? ? ? ? ? ? buffer.get(rowData, 0, length);

? ? ? ? ? ? ? ? ? ? for (int col = 0; col < widthShifted; col++)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? output.array()[channelOffset] = rowData[col * pixelStride];

? ? ? ? ? ? ? ? ? ? ? ? channelOffset += outputStride;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (row < heightShifted - 1)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? buffer.position(buffer.position() + rowStride - length);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return output;

? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容