NV21格式的byte[]数组要如何做才能在保存为图片文件后能查看图片文件的详细信息可以看到图片的宽度高度以及其他参数
1条回答 默认 最新
关注 引自强大而免费的微信小程序:皆我百晓生。
要查看NV21格式的图片文件的详细信息,可以将NV21格式的byte[]数组转换为Bitmap对象后保存为图片文件,并利用Bitmap对象来获取图片的宽度、高度以及其他参数。
这里是一个示例代码:
// 将NV21格式的byte[]数组转换为Bitmap对象 public Bitmap nv21ToBitmap(byte[] nv21, int width, int height) { YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, outputStream); byte[] jpeg = outputStream.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length); return bitmap; } // 在保存图片文件后获取图片的详细信息 public void saveImage(byte[] nv21, int width, int height, String path) { Bitmap bitmap = nv21ToBitmap(nv21, width, height); // 保存Bitmap对象为图片文件 File file = new File(path); try { FileOutputStream outputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.flush(); outputStream.close(); // 获取图片文件的详细信息 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int imageWidth = options.outWidth; int imageHeight = options.outHeight; // 其他参数... // 输出图片的详细信息 Log.i("ImageInfo", "Width: " + imageWidth + ", Height: " + imageHeight); // 其他参数... } catch (IOException e) { e.printStackTrace(); } }
在上面的示例代码中,
nv21ToBitmap()
方法将NV21格式的byte[]数组转换为Bitmap对象,然后使用compress()
方法将Bitmap对象保存为图片文件。接着,利用BitmapFactory.Options
类的inJustDecodeBounds
属性,可以在不实际解析图片的情况下获取图片的宽度、高度以及其他参数。最后,可以通过输出日志或其他方式获取到保存图片文件后的详细信息,如图片的宽度、高度等。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥30 关于用python写支付宝扫码付异步通知收不到的问题
- ¥50 vue组件中无法正确接收并处理axios请求
- ¥15 隐藏系统界面pdf的打印、下载按钮
- ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
- ¥15 基于pso参数优化的LightGBM分类模型
- ¥15 安装Paddleocr时报错无法解决
- ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
- ¥50 分布式追踪trace异常问题
- ¥15 人在外地出差,速帮一点点
- ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改