当前位置: 首页 > news >正文

ImageReader保存图片转 opencvmat

目录

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

转opencv

nv21保存图片,测试ok

rgb888 data保存图片:


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

// 创建一个ImageReader对象
ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);reader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {@Overridepublic 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

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++部分:

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++部分:

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

     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保存图片:

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();}
}

http://www.lryc.cn/news/138682.html

相关文章:

  • 【vue3+ts项目】配置husky+配置commitlint
  • html实现iframe全屏
  • 【es6】中的Generator
  • 桥梁安全监测方法和内容是什么?
  • prometheus部署及钉钉告警集成Grafana
  • Java百度提前批面试题
  • Go语言中的Oop面向对象
  • Duplicate keys detected: ‘1‘. This may cause an update error.
  • C++(8.21)c++初步
  • 【【Verilog典型电路设计之log函数的Verilog HDL设计】】
  • 数字放大(C++)
  • FOC控制框架图
  • Spring工具类(获取bean,发布事件)
  • 腾讯云和阿里云服务器折扣对比_看看哪家划算?
  • GO语言中的Defer与Error异常报错详细教程
  • AP6315 DC单节锂电池充电IC 同步2A锂电芯片
  • PDF校对工具正式上线,为用户提供卓越的文档校对解决方案
  • WSL 配置 Oracle 19c 客户端
  • ChatGPT⼊门到精通(1):ChatGPT 是什么
  • idea启动正常,打成jar包时,启动报错
  • 软考高级系统架构设计师系列论文八十九:论软件需求分析方法和工具的选用
  • java八股文面试[JVM]——类加载器
  • CSS中如何实现元素之间的间距(Margin)合并效果?
  • 【实操干货】如何开始用Qt Widgets编程?(三)
  • 基于深度学习的图像风格迁移发展总结
  • 小程序页面间有哪些传递数据的方法?
  • bh002- Blazor hybrid / Maui 保存设置快速教程
  • 同源政策与CORS
  • 科技资讯|三星再申请智能戒指商标,智能穿戴进入更小型化发展
  • HarmonyOS开发第一步,熟知开发工具DevEco Studio