
回复
本文原创发布在华为开发者社区,更多鸿蒙场景化示例请见华为开发者联盟官网“行业实践与常见问题”专题页。
本示例实现了自定义相机拍照功能以及保存图片功能,用到的API如下。
.onClick(() => {
let permissions: Array<Permissions> = [
···
];
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(this.context, permissions).then((data: PermissionRequestResult) => {
···
})
}
Button('图库')
.width(200)
.height(30)
.onClick(() => {
this.imagePicker();
})
.margin({ top: 30 })
imagePicker() {
try {
let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE;
photoSelectOptions.maxSelectNumber = 1;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
this.curFile = photoSelectResult.photoUris[0];
if (this.curFile) {
···
}
})
}
}
isVideoType(fileUri: string): boolean {
try {
let fileExtention = fileUri.substring(fileUri.lastIndexOf('.'));
let typeId = uniformTypeDescriptor.getUniformDataTypeByFilenameExtension(fileExtention);
···
let typeObj: uniformTypeDescriptor.TypeDescriptor = uniformTypeDescriptor.getTypeDescriptor(typeId);
return typeObj.belongingToTypes.some((s) => s == uniformTypeDescriptor.UniformDataType.VIDEO.toString())
}
}
createCameraManager() {
let cameraManager: camera.CameraManager = camera.getCameraManager(this.context);
this.cameraManager = cameraManager;
···
this.cameraManager.on();
}
SaveButton(this.saveButtonOptions)
.onClick(async (event, result: SaveButtonOnClickResult) => {
···
···
···
})
.onClick(async () => {
let photoCaptureSetting: camera.PhotoCaptureSetting = {
quality: camera.QualityLevel.QUALITY_LEVEL_HIGH,
rotation: camera.ImageRotation.ROTATION_0
}
await this.photoOutput?.capture(photoCaptureSetting).catch((error: BusinessError) => {
console.error(`CameraDemo Failed to capture the photo ${error.message}`);
})
this.hasPicture = true;
})
let cameraDevices: Array<camera.CameraDevice> = [];
try {
this.cameraInput = this.cameraManager.createCameraInput(cameraDevices[0]);
}