首先要导入内置的模块,然后我们可以去调用手机的摄像头以及本地图库(必须是真机才可以)
接下来就尽情的复制粘贴吧
struct PicTure {
@State message :string = 'hello'
@State uri :string = ''
private context = getContext(this) as common.UIAbilityContext
build(){
Row(){
Column(){
Button('拍照')
.onClick(async ()=>{
let wantObj = {
deviceId:"",
action:wantConstant.Action.ACTION_IMAGE_CAPTURE
}
let data = await this.context.startAbilityForResult(wantObj)
console.log(data.want.uri)
this.uri = data.want.uri
})
Button('选择图片')
.onClick(async ()=>{
let phoToSelectOptions = new picker.PhotoSelectOptions()
phoToSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE
phoToSelectOptions.maxSelectNumber = 1
let photoPicker = new picker.PhotoViewPicker()
let res = await photoPicker.select(phoToSelectOptions)
console.log(res.photoUris[0])
this.uri = res.photoUris[0]
})
Image(this.uri).size({width:200})
}
.width("100%")
}
.height("100%")
}
}