调用拍照获取图片逻辑

async cameraPickerControl() {
  let pathDir = getContext().filesDir
  let fileName = `${new Date().getTime()}`
  let filePath = pathDir + `/${fileName}.tmp`
  fileIo.createRandomAccessFileSync(filePath, fileIo.OpenMode.CREATE);
  let uri = fileUri.getUriFromPath(filePath);
  let pickerProfile: picker.PickerProfile = {
    cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK, saveUri: uri
  };
  let result: picker.PickerResult = await picker.pick(getContext(), [picker.PickerMediaType.PHOTO], pickerProfile);
  emitter.emit('unlockEvent') //调用拍照会触发前台事件,需要在这里解锁
  if (result.resultUri == '') {
    return
  }
  if (this.ref === 1) {
    this.imgSrc1 = uri
    this.imgLink1 = await pict_upload(fileName, 1)
    this.imgSrc1 = HttpConstant.TestApi + 'admin/file/' + this.imgLink1
  } else if (this.ref === 2) {
    this.imgSrc2 = uri
    this.imgLink2 = await pict_upload(fileName, 1)
    this.imgSrc2 = HttpConstant.TestApi + 'admin/file/' + this.imgLink2
  } else if (this.ref === 3) {
    this.imgSrc3 = uri
    this.imgLink3 = await pict_upload(fileName, 1)
    this.imgSrc3 = HttpConstant.TestApi + 'admin/file/' + this.imgLink3
  }
}
export function pict_upload(name: string, type: 0 | 1) {
  let pathDir = '';
  type == 0 ? pathDir = context.cacheDir : pathDir = context.filesDir
  let filePath = pathDir + `/${name}${type == 0 ? '' : '.tmp'}`
  console.log(`filePath===>${filePath}`)
  let formData = new FormData()
  try {
    let file2 = fs.openSync(filePath, 0o2);
    let stat = fs.lstatSync(filePath);
    let buf2 = new ArrayBuffer(stat.size);
    fs.readSync(file2.fd, buf2);
    fs.fsyncSync(file2.fd);
    fs.closeSync(file2.fd);
    formData.append('file', buf2);
  } catch (err) {
    console.info('err:' + JSON.stringify(err));
  }
  return new Promise<string>((resolve, reject) => {
    axios.post<string, AxiosResponse<string>, FormData>(HttpConstant.TestApi+FileApi.FILE_UPLOAD, formData, {
      headers: { 'Content-Type': 'multipart/form-data',
      }
    }).then((res: AxiosResponse) => {
      resolve(res.data.data.pictureName)
      console.info("===>result" + JSON.stringify(res));
    }).catch((error: AxiosError) => {
      reject('发生错误')
      console.error("error:" + JSON.stringify(error));
    })
  })

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值