HarmonyOS Sample之PixelMap图像功能开发

开发 OpenHarmony
HarmonyOS图像模块支持图像业务的开发,常见功能如图像解码、图像编码、基本的位图操作、图像编辑等。当然,也支持通过接口组合来实现更复杂的图像处理逻辑。

[[432512]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

1.介绍

HarmonyOS图像模块支持图像业务的开发,常见功能如图像解码、图像编码、基本的位图操作、图像编辑等。当然,也支持通过接口组合来实现更复杂的图像处理逻辑。

那么什么叫图像解码,什么叫图像编码,什么叫位图?

PixelMap是图像解码后无压缩的位图格式

图像解码就是不同的存档格式图片(如JPEG、PNG等)解码为无压缩的位图格式

图像编码就是将无压缩的位图格式,编码成不同格式的存档格式图片(JPEG、PNG等)

不管是编码还是解码的目的是方便在应用或者系统中进行相应的处理。

本文正在参与优质创作者激励

2.搭建环境

安装DevEco Studio,详情请参考DevEco Studio下载。

设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:

  • 如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。
  • 如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境。

下载源码后,使用DevEco Studio 打开项目,模拟器运行即可。

真机运行需要将config.json中的buddleName修改为自己的,如果没有请到AGC上进行配置,参见 使用模拟器进行调试 。

3.代码结构

HarmonyOS Sample 之 PixelMap 图像功能开发-鸿蒙HarmonyOS技术社区

4.实例讲解

4.1.界面布局

布局只有几个操作按钮,见后面的效果图,不是本文的重点。

4.2.后台代码

4.2.1 图像解码功能

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

主要用到 ImageSource等

ImageSource.SourceOptions 指定数据源的格式信息,非必填

ImageSource.DecodingOptions 用来支持在解码过程中的图像处理操作,例如缩放、裁剪、旋转,非必填。

接下来看个最简单的图像解码的例子:

  1. /** 
  2.  * 图形解码 
  3.  * png -->ImageSource -->PixelMap 
  4.  * 
  5.  * @param component 
  6.  */ 
  7. private void commonDecode(Component component) { 
  8.     cleanComponents(); 
  9.     //1.获取应用程序在设备内部存储器上存放文件的目录。 
  10.     String pathName = new File(getFilesDir(), "test.png").getPath(); 
  11.     /* 
  12.     InputStream inputStream=null
  13.     try { 
  14.         //2.读取 media目录的图片 
  15.         inputStream = getContext().getResourceManager().getResource(ResourceTable.Media_icon); 
  16.         ImageSource imageSource = ImageSource.create(inputStream, null); 
  17.         inputStream.close(); 
  18.     } catch (IOException e) { 
  19.         e.printStackTrace(); 
  20.     } catch (NotExistException e) { 
  21.         e.printStackTrace(); 
  22.     }*/ 
  23.  
  24.     //--------------------最简单的方式----------------------- 
  25.     ImageSource imageSource = ImageSource.create(pathName, null); 
  26.     //解码为位图格式 
  27.     PixelMap pixelMap = imageSource.createPixelmap(null); 
  28.     //设置图片显示 
  29.     showFirstImage.setPixelMap(pixelMap); 
  30.  
  31.  
  32.     //--------------------使用SourceOptions、DecodingOptions 选项------------- 
  33.     //指定数据源的格式信息,提高解码效率 
  34.     ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions(); 
  35.     sourceOptions.formatHint = "image/png"
  36.     //image 源 
  37.     imageSource = ImageSource.create(pathName, sourceOptions); 
  38.  
  39.  
  40.     //提供图像解码选项 
  41.     ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); 
  42.  
  43.     //裁剪,如果设置为全0,则不进行裁剪。358/227-448/279 
  44.     decodingOptions.desiredRegion = new Rect(358, 227, 90, 52); 
  45.  
  46.     //缩放,如果选择的尺寸与原始图像尺寸不同,则将图像放大或缩小后输出。如果设置为全0,则不进行缩放 
  47.     decodingOptions.desiredSize = new Size(90 * 3, 52 * 3); 
  48.     //旋转角度,取值范围为0~360。以原图为旋转中心,顺时针旋转图像。 
  49.     decodingOptions.rotateDegrees = 180; 
  50.  
  51.     //解码为位图格式 
  52.     pixelMap = imageSource.createPixelmap(decodingOptions); 
  53.  
  54.     //设置图片先死 
  55.     showSecondImage.setPixelMap(pixelMap); 
  56.  
  57.     //记得释放资源 
  58.     imageSource.release(); 
  59.     pixelMap.release(); 

效果:

HarmonyOS Sample 之 PixelMap 图像功能开发-鸿蒙HarmonyOS技术社区
  1. 10-26 19:02:01.594 13148-13148/? D 00000/=>MainAbilitySlice:  pngCachePath:/data/user/0/com.buty.samples/files/test.png 
  2. 10-26 19:02:01.594 13148-13148/? D 00000/=>MainAbilitySlice:  jpgCachePath:/data/user/0/com.buty.samples/files/test.jpg 

还有一个渐进式解码,

官方文档的描述:在未获取到全部图像时,支持先更新部分数据来尝试解码,调用updateData更新数据,将参数isFinal设置为false;当获取到全部数据后,最后一次更新数据时设置isFinal为true,表示数据更新完毕。

又去网上搜索了一下关于渐进式解码的内容,是这么说的。

“渐进式解码提供在整个图像完成下载之前以增量方式解码和呈现图像部分的能力。 此功能极大地改进了从 Internet 查看图像时用户的体验,因为用户无需等待整个图像下载,就可以开始解码。 在下载整个映像之前,用户可以查看包含可用数据的映像预览。 此功能对于用于从 Internet 或带宽有限的数据源查看图像的任何应用程序都至关重要。”

“渐进式解码是一种从不完整的图像文件中以增量方式解码图像部分的能力。 传统解码需要完整的图像文件才能开始解码。 渐进式解码在图像的渐进式级别完成下载后开始。 解码器对图像的当前渐进式级别执行解码传递。 然后,它会在下载每个渐进式级别时对图像执行多个解码传递。 每次解码传递都显示更多图像,直到完全下载并解码图像。 解码完整图像所需的传递数取决于图像文件格式和用于创建图像的编码过程。

使用渐进式解码的要求:

1.图像文件必须支持渐进式解码。 大多数图像格式不支持渐进式解码,尽管常用图像格式为 JPEG、PNG 和 GIF。

2.图像文件必须编码为渐进式图像。 未使用渐进式图像编码创建的图像文件无法实现渐进式解码,即使文件格式会支持渐进式解码。

3.支持渐进式解码的编解码器必须可用。 如果编解码器不支持渐进式解码,则编码为渐进式图像的图像将被解码为传统图像。”

一起看个渐进式解码的例子,

点击 “渐进式解码” 按钮,执行一次imageSource.updateData

  1. /** 
  2.  * 渐进式解码 
  3.  * 
  4.  * @param component 
  5.  */ 
  6. private void regionDecode(Component component) { 
  7.  
  8.     if (buttonClickNum < 10) { 
  9.         // 获取到一定的数据时尝试解码, 
  10.         imageSource.updateData(fileByte, fileByte.length * buttonClickNum / 10, fileByte.length / 10, 
  11.                 buttonClickNum==9?true:false); 
  12.         pixelMap = imageSource.createPixelmap(null); 
  13.         showResultText.setText( (buttonClickNum + 1) + "/10"); 
  14.         showSecondImage.setPixelMap(pixelMap); 
  15.         buttonClickNum++; 
  16.     }else
  17.         pixelMap.release(); 
  18.         imageSource.release(); 
  19.         readFileForInitFileByte(); 
  20.         buttonClickNum=0; 
  21.         cleanComponents(); 
  22.     } 
  23.  
  24. /** 
  25.  * 读取文件初始化 fileByte变量 
  26.  * 初始化 imageSource 
  27.  */ 
  28. private void readFileForInitFileByte() { 
  29.     File file = new File(jpgCachePath); 
  30.     try { 
  31.         FileInputStream fileInputStream = new FileInputStream(file); 
  32.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
  33.         byte[] data = new byte[1024]; 
  34.         int len = -1; 
  35.         while ((len = fileInputStream.read(data)) != -1) { 
  36.             byteArrayOutputStream.write(data, 0, len); 
  37.         } 
  38.         fileByte = byteArrayOutputStream.toByteArray(); 
  39.  
  40.         ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); 
  41.         srcOpts.formatHint = "image/jpeg"
  42.         //增量源选项 IncrementalSourceOptions 
  43.         ImageSource.IncrementalSourceOptions incrementalSourceOptions = new ImageSource.IncrementalSourceOptions(); 
  44.         incrementalSourceOptions.opts = srcOpts; 
  45.         //表示只输入增量数据来更新源。 
  46.         incrementalSourceOptions.mode = ImageSource.UpdateMode.INCREMENTAL_DATA; 
  47.         //创建增量数据源 
  48.         imageSource = ImageSource.createIncrementalSource(incrementalSourceOptions); 
  49.     } catch (IOException e) { 
  50.         e.printStackTrace(); 
  51.     } 

效果:

4.2.2 图像编码功能

图像编码主要用到ImagePacker

ImagePacker.PackingOptions 设置编码选项,目前没有太多可选。

  1. /** 
  2.  * 使用 ImagePacker  来打包图片 
  3.  * 表示将压缩图像打包成文件或其他对象的图像打包器。 
  4.  * 可以调用create创建图片打包器,调用initializePacking设置打包选项,调用addImage添加要打包的图片数据,调用finalizePacking完成打包输出目标对象。 
  5.  * 
  6.  * @param component 
  7.  */ 
  8. private void encode(Component component) { 
  9.     cleanComponents(); 
  10.     //创建ImagePacker对象 
  11.     ImagePacker imagePacker = ImagePacker.create(); 
  12.     //设置编码选项 
  13.     ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions(); 
  14.     //图像质量,范围从0-100,100为最佳质量。 
  15.     packingOptions.quality = 90; 
  16.  
  17.  
  18.     //文件输出流, test_encode.jpg 
  19.     try (FileOutputStream outputStream = new FileOutputStream(encodeOutPath)) { 
  20.  
  21.         //初始化将结果输出到 OutputStream 对象的打包任务。 
  22.         imagePacker.initializePacking(outputStream, packingOptions); 
  23.  
  24.         //图像数据源,test.png 
  25.         ImageSource imageSource = ImageSource.create(pngCachePath, null); 
  26.  
  27.         //转为位图格式 
  28.         PixelMap pixelMap = imageSource.createPixelmap(null); 
  29.  
  30.         //将pixelMap添加到编码器,进行编码 
  31.         boolean result = imagePacker.addImage(pixelMap); 
  32.  
  33.         showResultText.setText( 
  34.                 "Encode result : " + result + System.lineSeparator() + "OutputFilePath:" + encodeOutPath); 
  35.  
  36.         //释放图像数据源 
  37.         imageSource.release(); 
  38.         pixelMap.release(); 
  39.     } catch (IOException e) { 
  40.         HiLog.info(LABEL_LOG, "%{public}s""encode IOException "); 
  41.     } 
  42.     //释放imagePacker 
  43.     imagePacker.release(); 

4.2.3 编辑位图功能

a.通过imageSource创建缩略图,createThumbnailPixelmap

  1. //解码 ImageSource 实例中包含的缩略图数据以生成缩略图并创建缩略图像素图。 
  2. //allowFromImage - 如果 ImageSource 不包含缩略图数据,则指定是否允许基于原始图像创建。 
  3. PixelMap thumbnailPixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true); 

 b.读写位图像素数据,画个像素小人

  1. /** 
  2.  * 编辑位图 
  3.  * @param component 
  4.  */ 
  5. private void edit(Component component) { 
  6.     cleanComponents(); 
  7.  
  8.     int colorsWidth = 552; 
  9.     int colorsHeight = 310; 
  10.  
  11.     //PixelMap 选项 
  12.     PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions(); 
  13.     //指示要创建的像素图的预期大小。 
  14.     initializationOptions.size = new Size(colorsWidth, colorsHeight); 
  15.     initializationOptions.pixelFormat = PixelFormat.ARGB_8888; 
  16.  
  17.     //PixelMap是否允许修改 
  18.     initializationOptions.editable = true
  19.  
  20.     //表示像素颜色的 int 数组。 数组中的每个元素都是 PixelFormat#ARGB_8888 格式。 
  21.     int[] colors = new int[colorsWidth * colorsHeight]; 
  22.     Arrays.fill(colors, Color.RED.getValue()); 
  23.  
  24.     //创建PixelMap 
  25.     PixelMap pixelMap = PixelMap.create(colors, initializationOptions); 
  26.     //显示图片 
  27.     showFirstImage.setPixelMap(pixelMap); 
  28.  
  29.  
  30.     // 以另外一个PixelMap作为数据源创建 
  31.     PixelMap pixelMap2 = PixelMap.create(pixelMap, initializationOptions); 
  32.  
  33.     //读取指定位置的颜色值。 
  34.     int color = pixelMap2.readPixel(new Position(1, 1)); 
  35.     HiLog.info(LABEL_LOG, "%{public}s""pixelMapEdit readPixel color :" + color); 
  36.  
  37.  
  38.     //背上一把宝剑 
  39.     for(int i=130;i<180 ;i++){ 
  40.         //在指定位置写入像素 
  41.         pixelMap2.writePixel(new Position(i, i), 0xFF112233); 
  42.     } 
  43.  
  44.     //在指定区域写入像素 
  45.     int[] pixelArray = new int[1024*1024]; 
  46.     Arrays.fill(pixelArray, Color.BLACK.getValue()); 
  47.     //头 
  48.     Rect region = new Rect(160, 110, 50, 50); 
  49.     //stride - 表示数组每行中的像素数。 该值必须大于或等于此 PixelMap 中目标区域的宽度。 
  50.     pixelMap2.writePixels(pixelArray, 0, 100, region); 
  51.  
  52.     Arrays.fill(pixelArray, Color.GREEN.getValue()); 
  53.     //身体 
  54.     Rect region2= new Rect(150, 160, 70, 60); 
  55.     //stride - 表示数组每行中的像素数。 该值必须大于或等于此 PixelMap 中目标区域的宽度。 
  56.     pixelMap2.writePixels(pixelArray, 0, 100, region2); 
  57.  
  58.     Arrays.fill(pixelArray, Color.YELLOW.getValue()); 
  59.     //胳膊 
  60.     Rect region3= new Rect(130, 160, 20, 70); 
  61.     //stride - 表示数组每行中的像素数。 该值必须大于或等于此 PixelMap 中目标区域的宽度。 
  62.     pixelMap2.writePixels(pixelArray, 0, 100, region3); 
  63.     //胳膊 
  64.     Rect region4= new Rect(220, 160, 20, 70); 
  65.     //stride - 表示数组每行中的像素数。 该值必须大于或等于此 PixelMap 中目标区域的宽度。 
  66.     pixelMap2.writePixels(pixelArray, 0, 100, region4); 
  67.  
  68.     Arrays.fill(pixelArray, Color.GRAY.getValue()); 
  69.     //腿 
  70.     Rect region5= new Rect(160, 200, 20, 70); 
  71.     //stride - 表示数组每行中的像素数。 该值必须大于或等于此 PixelMap 中目标区域的宽度。 
  72.     pixelMap2.writePixels(pixelArray, 0, 100, region5); 
  73.  
  74.     //腿 
  75.     Rect region6= new Rect(190, 200, 20, 70); 
  76.     //stride - 表示数组每行中的像素数。 该值必须大于或等于此 PixelMap 中目标区域的宽度。 
  77.     pixelMap2.writePixels(pixelArray, 0, 100, region6); 
  78.  
  79.     showSecondImage.setPixelMap(pixelMap2); 
  80.  
  81.     //从位图对象中获取信息 
  82.     long capacity = pixelMap.getPixelBytesCapacity(); 
  83.     long bytesNumber = pixelMap.getPixelBytesNumber(); 
  84.     int rowBytes = pixelMap.getBytesNumberPerRow(); 
  85.     byte[] ninePatchData = pixelMap.getNinePatchChunk(); 
  86.  
  87.     showResultText.setText( 
  88.             "This pixelMap detail info :" + System.lineSeparator() + "capacity = " + capacity + System.lineSeparator() 
  89.                     + "bytesNumber = " + bytesNumber + System.lineSeparator() + "rowBytes = " + rowBytes 
  90.                     + System.lineSeparator() + "ninePatchData = " + Arrays.toString(ninePatchData) + System.lineSeparator()); 
  91.     pixelMap.release(); 
  92.     pixelMap2.release(); 

效果是这样的:

HarmonyOS Sample 之 PixelMap 图像功能开发-鸿蒙HarmonyOS技术社区

4.2.4 获取图像属性

图像属性解码就是获取图像中包含的属性信息,PropertyKey 存储了常用的属性KEY信息。

看代码:

  1. /** 
  2.  * 获取图片的缩略图和位置信息 
  3.  * 
  4.  * @param component 
  5.  */ 
  6. private void attribute(Component component) { 
  7.     cleanComponents(); 
  8.  
  9.     ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); 
  10.     srcOpts.formatHint = "image/jpeg"
  11.  
  12.     HiLog.debug(LABEL_LOG,"jpgCachePath="+jpgCachePath); 
  13.     ImageSource imageSource = ImageSource.create(jpgCachePath, srcOpts); 
  14.  
  15.     ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions(); 
  16.  
  17.     //解码 ImageSource 实例中包含的缩略图数据以生成缩略图并创建缩略图像素图。 
  18.     //allowFromImage - 如果 ImageSource 不包含缩略图数据,则指定是否允许基于原始图像创建。 
  19.     PixelMap thumbnailPixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true); 
  20.  
  21.  
  22.     //位置信息 
  23.     String location = imageSource.getImagePropertyString(PropertyKey.Exif.SUBJECT_LOCATION); 
  24.  
  25.     HiLog.info(LABEL_LOG, "%{public}s""imageExif location : " + location); 
  26.  
  27.     showResultText.setText("ImageSource attribute : createThumbnailPixelMap"); 
  28.     showSecondImage.setPixelMap(thumbnailPixelMap); 
  29.  
  30.     // 
  31.     imageSource.release(); 
  32.     thumbnailPixelMap.release(); 

效果:

HarmonyOS Sample 之 PixelMap 图像功能开发-鸿蒙HarmonyOS技术社区

5.思考总结

通过这次实践,可以学到的内容:

  • 位图的概念,如何进行图像的编码、解码、编辑。
  • 渐进式解码的概念,如何生成缩略图,获取其他图像属性等。

文章相关附件可以点击下面的原文链接前往下载

https://harmonyos.51cto.com/resource/1419

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-09-24 09:25:01

鸿蒙HarmonyOS应用

2021-12-10 15:06:56

鸿蒙HarmonyOS应用

2021-08-17 10:20:14

鸿蒙HarmonyOS应用

2021-09-15 14:55:49

鸿蒙HarmonyOS应用

2021-08-19 09:56:20

Windows 11操作系统微软

2022-08-09 16:01:24

应用开发鸿蒙

2021-09-17 14:43:54

鸿蒙HarmonyOS应用

2021-11-23 09:58:35

鸿蒙HarmonyOS应用

2021-07-08 09:42:04

鸿蒙HarmonyOS应用

2021-09-22 09:42:41

鸿蒙HarmonyOS应用

2012-03-07 14:37:03

JavaJavaMail

2021-07-29 14:03:35

鸿蒙HarmonyOS应用

2021-11-30 14:51:11

鸿蒙HarmonyOS应用

2021-12-02 10:11:44

鸿蒙HarmonyOS应用

2012-11-05 10:36:40

IBMdw

2021-08-24 15:13:06

鸿蒙HarmonyOS应用

2023-04-26 09:37:25

智驾开发

2023-02-28 15:49:09

鸿蒙应用开发

2017-12-11 14:50:34

前端Javascript文本朗读

2021-12-03 09:49:59

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号