当前位置: 首页 > 科技观察

鸿蒙HarmonyOS三方软件开发指南(七)-压缩组件_0

时间:2023-03-21 23:38:54 科技观察

更多内容请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com/#zz1。组件压缩功能介绍1.1.组件介绍:compress是一个轻量级的图片压缩库。压缩允许将大照片压缩成小尺寸照片,图像质量损失很小或可以忽略不计。1.2.在手机模拟器上运行效果:2.如何使用componentcompress2.1.添加依赖将compress-debug.har复制到应用.har的entry\libs目录下,所以不需要修改)。2.2.设置布局2.3.图形压缩核心类:Compressor核心方法:(1)自定义压缩:publicstaticFilecustomCompress(Contextcontext,Filefile,intwidth,inheight,intquality)throwsIOException参数:context-应用上下文文件-待压缩图片的抽象路径名width-压缩后的宽度height-压缩后的高度quality-图片压缩质量,取值范围0~100结果:返回压缩后图片的抽象路径名异常:发生I/O异常(2)默认压缩:publicstaticFiledefaultCompress(Contextcontext,Filefile)throwsIOException参数:上下文-应用上下文文件-图片待压缩抽象路径名结果:返回图像压缩后的抽象路径名。Exception:I/Oexception简单示例:运行示例前,需要在模拟器中保存截图或者使用相机功能拍照publicvoidonStart(Intentintent){super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//请求文件的读取权限String[]permissions={"ohos.permission.READ_USER_STORAGE"};requestPermissionsFromUser(permissions,0);//获取压缩按钮并绑定事件Buttonbutton=(Button)findComponentById(ResourceTable.id_button);if(button!=null){//设置按钮的点击回调io.tmpdir")+File.separator+tmpName);HiLog.error(LOG_LABEL,"oldsize..."+file.length()+"...b");//默认压缩//FilenewFile=Compressor.defaultCompress(file);//自定义压缩FilenewFile=Compressor.customCompress(getContext(),file,500,1000,60);Texttext=(Text)findComponentById(ResourceTable.Id_text);text.setText("size:"+newFile.length()+"b");HiLog.error(LOG_LABEL,"newsize..."+newFile.length()+"...b");PixelMapnewPixelMap=Compressor.decode(newFile);Imageimage=(Image)findComponentById(ResourceTable.Id_image1);image.setPixelMap(newPixelMap);}catch(IOExceptione){e.printStackTrace();}}});}//获取选择图片按钮并绑定事件(newComponent.ClickedListener(){@OverridepublicvoidonClick(Componentcomponent){DataAbilityHelperhelper=DataAbilityHelper.creator(getContext());try{ResultSetresultSet=helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,null,null);while(resultSet!=null&&resultSet.goToNextRow()){//Intid=resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID));HiLog.error(LOG_LABEL,"id:..."+id+"...");Uriuri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,""+id);//根据图片的uri打开文件,保存到t临时目录FileDescriptorfileDescriptor=helper.openFile(uri,"r");ImageSource.DecodingOptionsdecodingOpts=newImageSource.DecodingOptions();decodingOpts.sampleSize=ImageSource.DecodingOptions.DEFAULT_SAMPLE_SIZE;ImageSourceimageSource=ImageSource.create(fileDescriptor,null);PixelMappixelMap=imageSource.createThumbnailPixelmap(decodingOpts,true);ImagePackerimagePacker=ImagePacker.create();tmpName=UUID.randomUUID().toString();Filefile=newFile(System.getProperty("java.io.tmpdir")+File.separator+tmpName);FileOutputStreamoutputStream=newFileOutputStream(文件);ImagePacker.PackingOptionspackingOptions=newImagePacker.PackingOptions();packingOptions.quality=100;booleanresult=imagePacker.initializePacking(outputStream,packingOptions);result=imagePacker.addImage(pixelMap);longdataSize=imagePacker.finalizePacking();//显示图片和图片大小Texttext=(Text)findComponentById(ResourceTable.Id_text);text.setText("size:"+file.length()+"b");Imageimage=(Image)findComponentById(ResourceTable.Id_image1);image.setPixelMap(pixelMap);}}catch(DataAbilityRemoteException|FileNotFoundExceptione){e.printStackTrace();}}});}}3.组件压??缩的开发和实现3.1。复制临时文件到应用临时目录的图片路径privatestaticFilecopyToCache(Contextcontext,FileimageFile)throwsIOException{PixelMappixelMap=decode(imageFile);StringcachePath=context.getCacheDir()+File.separator+imageFile.getName();FilecacheFile=newFile(cachePath);intquality=100;//压缩质量refreshTmpFile(像素图、缓存文件、质量);返回缓存文件;}3.2。图片解码对临时目录下的图片进行解码returnimageSource.createPixelmap(decodingOpts);}3.3。图片编码根据开发者设置的规则进行编码,生成新的图片;imagePacker.initializePacking(newFileOutputStream(文件),选项);imagePacker.addImage(pixelMap);imagePacker.finalizePacking();}项目源码地址:https://github.com/isoftstone-dev/Compressor_Harmony欢迎交流:HOS@isoftstone.com?版权归作者及HarmonyOS技术社区所有。如需转载请注明出处,否则将追究法律责任。更多信息请访问:与华为官方共建的鸿蒙科技社区https://harmonyos.51cto.com/#zz