Android camera2 获取的yuv420_8888转argb_8888

  • 时间:2025-10-22 20:19 作者: 来源: 阅读:0
  • 扫一扫,手机访问
摘要:调用android的camera2获取到的原始图像数据是YUV_420_8888,是无法直接存jpg的,所以需要先利用renderscript进行转换public static void onImageAvailable(Context context,Image image)    {       

调用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;

    }

  • 全部评论(0)
上一篇:已是第一篇内容
下一篇:自然语言处理(NLP)进阶:对话系统与聊天机器人
手机二维码手机访问领取大礼包
返回顶部