更多内容请访问:与华为官方共建的HarmonyOS技术社区https://harmonyos.51cto.com1。项目介绍HarmonyOSimage模块支持开发图像服务,图像解码、图像编码、基本位图操作、图像编辑等常用功能,当然也通过接口组合支持更复杂的图像处理逻辑。本教程以图库中常见的旋转、裁剪、缩放、镜像四种操作为例,介绍HarmonyOS图片编解码的相关开发指导。2、图片转PixelMap图片解码是将支持格式的存档图片解码成统一的PixelMap图片,用于后续图片显示或其他处理,如旋转、缩放、裁剪等。目前支持的格式包括JPEG、PNG、GIF、HEIF、WebP,BMP。本例提供了getPixelMapFromResource函数,可以将resources/base/media目录下的图片资源转换为PixelMap图片,入参为图片的资源ID。privatePixelMapgetPixelMapFromResource(intresourceId){InputStreaminputStream=null;try{//创建图像数据源ImageSource对象inputStream=getContext().getResourceManager().getResource(resourceId);ImageSource.SourceOptionssrcOpts=newImageSource.SourceOptions();srcOpts.formatHint="/jpg";ImageSourceimageSource=ImageSource.create(inputStream,srcOpts);//设置图片参数");}catch(NotExistExceptione){HiLog.info(LABEL_LOG,"NotExistException");}最后{if(inputStream!=null){try{inputStream.close();}catch(IOExceptione){HiLog.info(LABEL_LOG,"inputStreamIOException");}}}returnnull;}3。图片参数设置本例中使用的图片像素大小为1024*768。单击旋转按钮一次,将旋转90度,按2:1的比例进行缩放。缩放和裁剪是将高度裁剪为400像素,同时保持宽度不变。相关参数设置如下://设置图片参数ImageSource.DecodingOptionsdecodingOptions=newImageSource.DecodingOptions();//旋转decodingOptions.rotateDegrees=90*whirlCount;//缩放decodingOptions.desiredSize=newSize(isScale?512:0,isScale?384:0);//剪切decodingOptions.desiredRegion=newRect(0,0,isCorp?1024:0,isCorp?400:0);4.Imagemirroringoperation图像镜像操作是以垂直轴为轴做对称图像绘制图像时将调用onDraw方法。本例中使用图像画布的镜像操作来实现图像的镜像显示。示例代码如下:privatevoidmirrorImage(PixelMappixelMap){scaleX=-scaleX;image.addDrawTask(newComponent.DrawTask()(float)pixelMap.getImageInfo().size.height/2);canvas.drawPixelMapHolder(pmh,0,0,newPaint());}}});}5.完整示例以手机为例,初始化页面如图1所示,依次点击按钮,实现图片旋转、裁剪、缩放、镜像。效果如下(需要准备一张像素大小为1024*768的图片,放在ImageDemo\entry\src\main\resources\base\media目录下):示例代码如下:importcom.huawei.codelab.ResourceTable;importohos.aafwk.ability.AbilitySlice;importohos.aafwk.content.Intent;importohos.agp.components.Button;importohos.agp.components.Component;importohos.agp.components.Image;importohos.agp.render.Canvas;importohos.agp.render.Paint;importohos.agp.render.PixelMapHolder;importohos.global.resource.NotExistException;importohos.hiviewdfx.HiLog;importohos.hiviewdfx.HiLogLabel;importohos.media.image.ImageSource;importohos.media.image.PixelMap;importohos.media.image.common.PixelFormat;importohos.media.image.common.Rect;importohos.media。image.common.Size;importjava.io.IOException;importjava.io.InputStream;/***图像主页*/publicclassMainAbilitySliceextendsAbilitySlice{privatestaticfinalHiLogLabelLABEL_LOG=newHiLogLabel(3,0xD001100,"MainAbilitySlice");Imageimage;PixelMapimagePixelMapl;ButtonwhiImage;ButtonwhiImageButtonscaleImageBtn;ButtonmirrorImageBtn;privateintwhirlCount=0;privatebooleanisCorp=false;privatebooleanisScale=false;privatebooleanisMirror=false;privatefloatscaleX=1.0f;@OverridepublicvoidonStart(意图){super.onStart(意图);super.setUIContent(ResourceTable.Layout_ability_main);initView();}privatevoidinitView(){if(findComponentById(ResourceTable.Id_whirl_image)instanceofButton){whirlImageBtn=(Button)findComponentById(ResourceTable.Id_whirl_image);}if(findComponentById(ResourceTable.Id_crop_image)instanceofButton){cropImageBtn=(Button)findComponentById(ResourceTable.Id_crop_image);}if(findComponentById(ResourceTable.Id_scale_image)instanceofButton){scaleImageBtn=(Button)findComponentById(ResourceTable.Id_scale_image);}if(findComponentById(ResourceTable.Id_mirror_image)instanceofButton){mirrorImageBtn=(Button)findComponentById(ResourceTable.Id_mirror_image);}if(findComponentById(ResourceTable.Id_image)instanceofImage){image=(Image)findComponentById(ResourceTable.Id_image);}whirlImageBtn.setClickedListener(newButtonClick());cropImageBtn.setClickedListener(newButtonClick());scaleImageBtn.setClickedListener(newButtonClick());mirrorImageBtn.setClickedListener(newButtonClick());getId();switch(btnId){caseResourceTable.Id_whirl_image://旋转图片whirlCount++;isCorp=false;isScale=false;isMirror=false;imagePixelMap=getPixelMapFromResource(ResourceTable.Media_shanghai);image.setPixelMap(imagePixelMap);break;caseResourceTable.Id_crop_image://剪裁图片whirlCount=0;isCorp=!isCorp;isScale=false;isMirror=false;imagePixelMap=getPixelMapFromResource(ResourceTable.Media_shanghai);image.setPixelMap(imagePixelMap);break;caseResourceTable.Id_scale_image://缩小图片whirlCount=0;isCorp=false;isScale=!isScale;isMirror=false;imagePixelMap=getPixelMapFromResource(ResourceTable.Media_shanghai);image.setPixelMap(imagePixelMap);break;caseResourceTable.Id_mirror_image://镜像图片whirlCount=0;isCorp=false;isScale=false;isMirror=true;imagePixelMap=getPixelMapFromResource(ResourceTable.Media_shanghai);mirrorImage(imagePixelMap);image.setPixelMap(imagePixelMap);break;default:break;}}}privatevoidmirrorImage(PixelMappixelMap){scaleX=-scaleX;image.addDrawTask(newComponent.DrawTask(){@OverridepublicvoidonDraw(Componentcomponent,Canvascanvas){if(isMirror){isMirror=false;PixelMapHolderpmh=newPixelMapHolder(pixelMap);canvas.scale(scaleX,1.0f,(float)pixelMap.getImageInfo().size.width/2,(float)pixelMap.getImageInfo().size.height/2);canvas.drawPixelMapHolder(pmh,0,0,newPaint());}}});}/***通过图片ID返回PixelMap**@paramresourceId图片资源ID*@return图片PixelMap*/privatePixelMapgetPixelMapFromResource(intresourceId){InputStreaminputStream=null;try{//创建图片数据源ImageSource对象.create(inputStream,srcOpts);//设置图片参数ImageSource.DecodingOptionsdecodingOptions=newImageSource.DecodingOptions();//旋转decodingOptions.rotateDegrees=90*whirlCount;//缩放decodingOptions.desiredSize=newSize(isScale?512:0,isScale?384:0);//切decodingOptions.desiredRegion=newRect(0,0,isCorp?1024:0,isCorp?400:0);decodingOptions.desiredPixelFormat=PixelFormat.ARGB_8888;returnimageSource.createPixelmap(decodingOptions);}catch(IOExceptione){HiLog.info(LABEL_LOG,"IOException");}catch(NotExistExceptione){HiLog.info(LABEL_LOG,"NotExistException");}最后{if(inputStream!=null){try{inputStream.close();}catch(IOExceptione){HiLog.info(LABEL_LOG,"inputStreamIOException");}}}returnnull;}@OverridepublicvoidonActive(){super.onActive();}@OverridepublicvoidonForeground(Intentintent){super.onForeground(intent);}}布署代码如下:
