Android camera2 获取的yuv420_8888转argb_8888

调用android的camera2获取到的原始图像数据是YUV_420_8888,是无法直接存jpg的,所以需要先利用renderscript进行转换

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;

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容