import { fileIo, fileUri } from '@kit.CoreFileKit';
import { camera, cameraPicker as picker } from '@kit.CameraKit';

export async function takePicture(context: Context) {
  let pathDir = context.filesDir;
  let fileName = `${new Date().getTime()}`
  let filePath = pathDir + `/${fileName}.png`
  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], //(如果需要录像可以添加) picker.PickerMediaType.VIDEO
      pickerProfile);
  if (!result.resultUri) {
    return Promise.reject('用户未拍照')
  }
  return filePath
}