鸿蒙开发拍照和选择图库封装
鸿蒙开发的拍照和选择图库比Android还要简单些,因为官方已经提供有相应的库给你调用,并且不用申请相机和存储权限,是不是很神奇,居然还不用申请权限。
一、思路
直接调用官方的库,我只是做了一层封装
二、效果图
二、关键代码如下
// 完整代码联系:893151960
@Entry
@Component
struct Index {
@State imagePath:string = ''
build() {
Stack(){
Column({space:20}) {
Image(this.imagePath)
.width(200)
.height(200)
Text('点击选择图片')
.fontSize(36)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
PictureSelectorUtils.openGallery(1, async (result: string[]) => {
this.imagePath = result[0]
})
})
Text('点击拍照')
.fontSize(36)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
// 要真机才能拍成功
PictureSelectorUtils.usePhoto(getContext() as common.UIAbilityContext, (result: string) => {
this.imagePath = result
})
})
}
}
.height('100%')
.width('100%')
}
}
四、项目demo结构图
有问题或者需要完整demo源码可私信或者看昵称联系我