ImageReader保存图片转 opencvmat

目录

[ImageReader 直接保存图片,没成功,格式是yuv420,需要转换](#ImageReader 直接保存图片,没成功,格式是yuv420,需要转换)

转opencv

nv21保存图片,测试ok

[rgb888 data保存图片:](#rgb888 data保存图片:)


ImageReader 直接保存图片,没成功,格式是yuv420,需要转换

java 复制代码
// 创建一个ImageReader对象
ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);

reader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image image = null;
        try {
            image = reader.acquireLatestImage();
            if (image != null) {
                ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                byte[] bytes = new byte[buffer.capacity()];
                buffer.get(bytes);
                saveImage(bytes);
            }
        } finally {
            if (image != null) {
                image.close();
            }
        }
    }
}, handler);

// 保存图片
private void saveImage(byte[] bytes) {
    File file = new File(Environment.getExternalStorageDirectory() + "/picture.jpg");
    FileOutputStream output = null;
    try {
        output = new FileOutputStream(file);
        output.write(bytes);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

转opencv

java 复制代码
Image image = imageReader.acquireLatestImage();
Plane[] planes = image.getPlanes();
ByteBuffer bufferY = planes[0].getBuffer();
ByteBuffer bufferU = planes[1].getBuffer();
ByteBuffer bufferV = planes[2].getBuffer();
byte[] bytesY = new byte[bufferY.remaining()];
byte[] bytesU = new byte[bufferU.remaining()];
byte[] bytesV = new byte[bufferV.remaining()];
bufferY.get(bytesY);
bufferU.get(bytesU);
bufferV.get(bytesV);

c++部分:

cpp 复制代码
cv::Mat imgY(height, width, CV_8UC1, bytesY);
cv::Mat imgU(height, width, CV_8UC1, bytesU);
cv::Mat imgV(height, width, CV_8UC1, bytesV);

c++部分:

cpp 复制代码
cv::Mat imgYUV;
cv::merge(std::vector<cv::Mat>{imgY, imgU, imgV}, imgYUV);
cv::Mat imgRGB;
cv::cvtColor(imgYUV, imgRGB, cv::COLOR_YUV2RGB);

nv21保存图片,测试ok

java 复制代码
     Yuv2Rgb.nativeNV21ToARGB8888(nv21, data, width[0], height[0]);
                        long t2 = System.currentTimeMillis();

                        // 假设你已经有了一个ARGB_8888格式的图像,存储在一个名为pixels的int数组中
                        int width = 1920;
                        int height = 1080;

                        String file_name="/storage/emulated/0/Android/data/com.sandstar.jupiter.terminal.algor/files/"+mCameraId+"/"+frameIdx+"_b.jpg";
                        YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null);

                        try {
                        // 创建一个输出流来保存图片
                        FileOutputStream outStream = new FileOutputStream(file_name);

                        // 将YuvImage转换为JPEG,并保存到输出流中
                        yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, outStream);

                        // 关闭输出流
                        outStream.close();
                         } catch (IOException e) {
                            e.printStackTrace();
                        }

rgb888 data保存图片:

java 复制代码
Yuv2Rgb.nativeNV21ToARGB8888(nv21, data, width[0], height[0]); 

// Create a Bitmap from the ARGB8888 data
Bitmap bitmap = Bitmap.createBitmap(data, width[0], height[0], Bitmap.Config.ARGB_8888);

// Save the Bitmap as a JPEG file
FileOutputStream fos = null;
try {
    fos = new FileOutputStream("/path/to/your/file.jpg");
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fos != null) {
            fos.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
相关推荐
子兮曰3 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
小羊没烦恼!3 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
子兮曰3 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
俊昭喜喜里3 天前
java中的继承和多态的区别
java
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝3 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码3 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖3 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商