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

鸿蒙HarmonyOS-获取系统照片并解码渲染展示2(附更完整的demo)_0

时间:2023-03-22 01:13:48 科技观察

更多内容请访问:与华为官方共建的Harmonyos技术社区https://harmonyos.51cto.com/#zzDeclaration哦,本文是我上一篇文章的续篇——#2020选文-手机#获取系统照片和解码渲染显示(附完整demo)写的原创。如果需要,可以先看前面的文档,这个是在前面代码的基础上进一步修改的。再说说功能升级(相比上个版本):(ps:我也想搞分布式,但是现在现实不允许,等远程模拟器的多机分布式联调能力出来吧打开)1.没有图片有提示2.可以显示相册中的所有照片,并显示计数3.打开应用会刷新。话不多说,先运行demo的效果,如下两张图:第一张图是在手机远程模拟器中其中一张图没有显示界面,第二张图是自行开启远程模拟器的摄像头功能。拍完N张后,完整的显示界面demo可以在附件中下载。老规矩先说升级。总体思路:1.使用TableLayout布局实现所有照片的显示2.添加两个Text分别显示没有照片的提示信息和照片的统计信息3.在onActive生命周期函数中添加一个方法,实现真正的-timerefresh1.使用TableLayout布局实现所有照片的显示1.1在布局文件中添加TableLayout布局代码。需要注意的是:这里我在外面加了一层ScrollView,这是为了让TableLayout在图片多的时候滑动1.2在java代码中获取这个布局TableLayoutimg_layout;img_layout=(TableLayout)findComponentById(ResourceTable.Id_layout_id);img_layout.setColumnCount(3);1.3将新生成的图片放到布局中Imageimg=newImage(MainAbilitySlice.this);img.setId(mediaId);img.setHeight(300);img.setWidth(300);img.setMarginTop(20);img.setMarginLeft(20);img.setPixelMap(pixelMap);img.setScaleMode(Image.ScaleMode.ZOOM_CENTER);img_layout.addComponent(img);2.添加两条Text,显示无照片信息和照片计数信息的提示2.1首先添加两条Text2.2在java中获取这两个文本组件Textpre_text,text;pre_text=(Text)findComponentById(ResourceTable.Id_text_pre_id);text=(Text)findComponentById(ResourceTable.Id_text_id);2.3使用能否获取到图片判断两个文本组件的显示逻辑if(img_ids.size()>0){pre_text.setVisibility(Component.HIDE);text.setVisibility(Component.VISIBLE);text.setText("图片数量:"+img_ids.size());}else{pre_text.setVisibility(Component.VISIBLE);pre_text.setText("Nopicture.");text.setVisibility(Component.HIDE);}3.在onActive生命周期函数中增加方法实现实时刷新3.1onActive简介lifecyclefunctionPagemeeting进入INACTIVE状态后到前台,然后系统调用这个回调Page进入ACTIVE状态后,也就是应用与用户交互的状态。所以当你把app放在后台,打开相机拍照,那么在app打开的时候就会调用生命周期函数3.2在onActive函数中添加需要的调用@OverridepublicvoidonActive(){super.onActive();displayPic();}3.3displayPic函数封装了显示图片的全部代码;尝试{ResultSetresult=helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,null,null);if(result==null){pre_text.setVisibility(Component.VISIBLE);}else{pre_text.setVisibility(Component.HIDE);}while(result!=null&&result.goToNextRow()){intmediaId=result.getInt(result.getColumnIndexForName(AVStorage.Images.Media.ID));Uriuri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,""+mediaId);FileDescriptorfiledesc=helper.openFile(uri,"r");ImageSource.DecodingOptionsdecodingOpts=newImageSource.DecodingOptions();decodingOpts.desiredSize=newSize(300,300);ImageSourceimageSource=ImageSource.create(filedesc,null);PixelMappixelMap=imageSource.createThumbnailPixelmap(decodingOpts,true);Imageimg=newImage(MainAbilitySlice.this);img.setId(mediaId);img.setHeight(300);img.setWidth(300));img.setMarginTop(20);img.setMarginLeft(20);img.setPixelMap(pixelMap);img.setScaleMode(Image.ScaleMode.ZOOM_CENTER);img_layout.addComponent(img);System.out.println("xxx"+uri);img_ids.add(mediaId);}}catch(DataAbilityRemoteException|FileNotFoundExceptione){e.printStackTrace();}if(img_ids.size()>0){pre_text.setVisibility(Component.HIDE);text.setVisibility(Component.VISIBLE);text.setText("照片数量:"+img_ids.size());}else{pre_text.setVisibility(Component.VISIBLE);pre_text.setText("Nopicture.");text.setVisibility(Component.HIDE);}}目前,这个演示基本上是可读的。.有时间我会继续尝试修改和完善。感兴趣的朋友可以关注完整demo的源码,见附件?版权归作者及HarmonyOS技术社区所有。如需转载请注明出处,否则将追究法律责任。更多信息请访问:与华为官方共建Harmonyos技术社区https://harmonyos.51cto.com/#zz

猜你喜欢