鸿蒙OS开发文档 鸿蒙OS 图像解码开发指导

2024-02-25 开发教程 鸿蒙OS开发文档 匿名 4

场景介绍

图像解码就是将所支持格式的存档图片解码成统一的 PixelMap 图像,用于后续图像显示或其他处理,比如旋转、缩放、裁剪等。当前支持格式包括 JPEG、PNG、GIF、HEIF、WebP、BMP。

接口说明

ImageSource 主要用于图像解码。

接口名描述
create(String pathName, SourceOptions opts)从图像文件路径创建图像数据源。
create(InputStream is, SourceOptions opts)从输入流创建图像数据源。
create(byte[] data, SourceOptions opts)从字节数组创建图像源。
create(byte[] data, int offset, int length, SourceOptions opts)从字节数组指定范围创建图像源。
create(File file, SourceOptions opts)从文件对象创建图像数据源。
create(FileDescriptor fd, SourceOptions opts)从文件描述符创建图像数据源。
createIncrementalSource(SourceOptions opts)创建渐进式图像数据源。
createIncrementalSource(IncrementalSourceOptions opts)创建渐进式图像数据源,支持设置渐进式数据更新模式。
createPixelmap(DecodingOptions opts)从图像数据源解码并创建 PixelMap 图像。
createPixelmap(int index, DecodingOptions opts)从图像数据源解码并创建 PixelMap 图像,如果图像数据源支持多张图片的话,支持指定图像索引。
updateData(byte[] data, boolean isFinal)更新渐进式图像源数据。
updateData(byte[] data, int offset, int length, boolean isFinal)更新渐进式图像源数据,支持设置输入数据的有效数据范围。
getImageInfo()获取图像基本信息。
getImageInfo(int index)根据特定的索引获取图像基本信息。
getSourceInfo()获取图像源信息。
release()释放对象关联的本地资源。

普通解码开发步骤

  1. 创建图像数据源 ImageSource 对象,可以通过 SourceOptions 指定数据源的格式信息,此格式信息仅为给解码器的提示,正确提供能帮助提高解码效率,如果不设置或设置不正确,会自动检测正确的图像格式。不使用该选项时,可以将 create 接口传入的 SourceOptions 设置为 null。
ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
srcOpts.formatHint = "image/png";
String pathName = "/path/to/image.png";
ImageSource imageSource = ImageSource.create(pathName, srcOpts);
ImageSource imageSourceNoOptions = ImageSource.create(pathName, null);
  1. 设置解码参数,解码获取 PixelMap 图像对象,解码过程中同时支持图像处理操作。设置 desiredRegion 支持按矩形区域裁剪,如果设置为全 0,则不进行裁剪。设置 desiredSize 支持按尺寸缩放,如果设置为全 0,则不进行缩放。设置 rotateDegrees 支持旋转角度,以图像中心点顺时针旋转。如果只需要解码原始图像,不使用该选项时,可将给 createPixelMap 传入的 DecodingOptions 设置为 null。
// 普通解码叠加旋转、缩放、裁剪
ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
decodingOpts.desiredSize = new Size(100, 2000);
decodingOpts.desiredRegion = new Rect(0, 0, 100, 100);
decodingOpts.rotateDegrees = 90;
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);
// 普通解码
PixelMap pixelMapNoOptions = imageSource.createPixelmap(null);
  1. 解码完成获取到 PixelMap 对象后,可以进行后续处理,比如渲染显示等。

渐进式解码开发步骤

  1. 创建渐进式图像数据源 ImageSource 对象,可以通过 SourceOptions 指定数据源的格式信息,此格式信息仅为提示,如果填写不正确,会自动检测正确的图像格式,使用 IncrementalSourceOptions 指定图像数据的更新方式为渐进式更新。
ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
srcOpts.formatHint = "image/jpeg";
ImageSource.IncrementalSourceOptions incOpts = new ImageSource.IncrementalSourceOptions();
incOpts.opts = srcOpts;
incOpts.mode = ImageSource.UpdateMode.INCREMENTAL_DATA;
imageSource = ImageSource.createIncrementalSource(incOpts);
  1. 渐进式更新数据,在未获取到全部图像时,支持先更新部分数据来尝试解码,更新数据时设置 isFinal 为 false,当获取到全部数据后,最后一次更新数据时设置 isFinal 为 true,表示数据更新完毕。设置解码参数同普通解码。
// 获取到一定的数据时尝试解码
imageSource.updateData(data, 0, bytes, false);
ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);
// 更新数据再次解码,重复调用直到数据全部更新完成
imageSource.updateData(data, 0, bytes, false);
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);
// 数据全部更新完成时需要传入isFinal为true
imageSource.updateData(data, 0, bytes, true);
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);
  1. 解码完成获取到 PixelMap 对象后,可以进行后续处理,比如渲染显示等。