调用手机摄像头
首先导入鸿蒙内置模块
import common from '@ohos.app.ability.common' // 应用上下文Context
import wantConstant from '@ohos.ability.wantConstant' // wantConstant能力接口
实现调用摄像头
struct Index {
@State uri :string = ''
// 调用相机
takePhoto() {
const context = getContext(this) as common.UIAbilityContext
//启动应用内的UIAbility并获取返回结果,此处返回的是图片uri
const result = await context.startAbilityForResult({
// 启动页面拍照功能的操作
action: wantConstant.Action.ACTION_IMAGE_CAPTURE
})
// 拍照结果
this.url = result.want.uri
}
build(){
Column(){
Button('拍照')
.onClick(async ()=>{
takePhotos()
})
}.height("100%").width("100%")
}
}
到这里就实现了应用内调用摄像头功能
如需上传操作还需将图片解析转存重组后,上传至服务器
新手上路,欢迎指导