CameraKit 相机管理 HarmonyOS 鸿蒙Next
CameraKit 相机管理 HarmonyOS 鸿蒙Next 执行了await this.cameraInput.open(),页面却没有打开相机这是怎么回事(api10的版本)
let context = this.getContext();
@Entry
@Component
struct CamearKit {
@State message: string = 'Hello World'
@State cameraInput: camera.CameraInput | undefined = undefined;
onPageShow() {
this.cameraShootingCase({stageMode:true})
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}.onClick(() => {
if(this.cameraInput!=undefined){
// 释放相机输入流
this.cameraInput.close();
}
router.replaceUrl({ url: 'pages/LoginPage'});
})
.width('100%')
}
.height('100%')
}
setPhotoOutputCb(photoOutput: camera.PhotoOutput): void {
// 设置回调之后,调用photoOutput的capture方法,就会将拍照的buffer回传到回调中
photoOutput.on('photoAvailable', (errCode: BusinessError, photo: camera.Photo): void => {
console.info('getPhoto start::::');
console.info(`err::::: ${JSON.stringify(errCode)}`);
if (errCode || photo === undefined) {
console.error('getPhoto failed::::');
return;
}
let imageObj = photo.main;
imageObj.getComponent(image.ComponentType.JPEG, (errCode: BusinessError, component: image.Component): void => {
console.info('getComponent start::::');
if (errCode || component === undefined) {
console.error('getComponent failed::::');
return;
}
let buffer: ArrayBuffer;
if (component.byteBuffer) {
buffer = component.byteBuffer;
} else {
console.error('byteBuffer is null::::');
return;
}
// buffer处理结束后需要释放该资源,如果未正确释放资源会导致后续拍照获取不到buffer
imageObj.release();
});
});
}
async cameraShootingCase(baseContext: common.BaseContext): Promise<void> {
let atManager = abilityAccessCtrl.createAtManager();
try {
atManager.requestPermissionsFromUser(getContext(this), ['ohos.permission.CAMERA']).then(async (data) => {
console.info("data::::" + JSON.stringify(data));
console.info("data permissions:::::" + data.permissions);
console.info("data authResults:::::" + data.authResults);
// 创建CameraManager对象
let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext);
if (!cameraManager) {
console.error("camera.getCameraManager error::::");
return;
}
// 监听相机状态变化
cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
if (err !== undefined && err.code !== 0) {
console.error('cameraStatus with errorCode :::: ' + err.code);
return;
}
console.info(`camera ::::: ${cameraStatusInfo.camera.cameraId}`);
console.info(`status::::: ${cameraStatusInfo.status}`);
});
// 获取相机列表
let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
if (cameraArray.length <= 0) {
console.error("cameraManager.getSupportedCameras error::::");
return;
}
for (let index = 0; index < cameraArray.length; index++) {
console.info('cameraId ::::: ' + cameraArray[index].cameraId); // 获取相机ID
console.info('cameraPosition ::::: ' + cameraArray[index].cameraPosition); // 获取相机位置
console.info('cameraType ::::: ' + cameraArray[index].cameraType); // 获取相机类型
console.info('connectionType ::::: ' + cameraArray[index].connectionType); // 获取相机连接类型
}
// 创建相机输入流
try {
this.cameraInput = cameraManager.createCameraInput(cameraArray[1]);
} catch (error) {
let err = error as BusinessError;
console.error('Failed to createCameraInput errorCode :::: ' + err.code);
}
if ( this.cameraInput === undefined) {
return;
}
// 监听cameraInput错误信息
let cameraDevice: camera.CameraDevice = cameraArray[1];
this.cameraInput.on('error', cameraDevice, (error: BusinessError) => {
console.error(`Camera input error code::::: ${error.code}`);
})
// 打开相机
await this.cameraInput.open().then(() => {
console.info('Promise returned with camera opened:::::');
}).catch((error: BusinessError) => {
console.error(`Failed to open the camera, error code:::::: ${error.code}.`);
});
}).catch((err: BusinessError) => {
console.error("data:::::" + JSON.stringify(err));
})
} catch (err) {
if(this.cameraInput!=undefined){
// 释放相机输入流
await this.cameraInput.close();
}
console.error(`catch err::::->${JSON.stringify(err)}`);
}
}
getSupportedOutputCapability(camera: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraOutputCapability {
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camera);
return cameraOutputCapability;
}
createPhotoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PhotoOutput | undefined {
let profile: camera.Profile = cameraOutputCapability.photoProfiles[0];
let photoOutput: camera.PhotoOutput | undefined = undefined;
try {
photoOutput = cameraManager.createPhotoOutput(profile, surfaceId);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createPhotoOutput call failed. error code: ${err.code}`);
}
return photoOutput;
}
getCameraManager(context: common.BaseContext): camera.CameraManager | undefined {
let cameraManager: camera.CameraManager | undefined = undefined;
try {
cameraManager = camera.getCameraManager(context);
} catch (error) {
let err = error as BusinessError;
console.error(`The getCameraManager call failed. error code: ${err.code}`);
}
return cameraManager;
}
}
更多关于CameraKit 相机管理 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
楼主您好!看下您当前使用系统相机的场景,如果仅需要拉起系统相机拍摄一张照片可直接使用CameraPicker,无需申请相机权限,直接拉起系统相机完成拍摄,具体可参考Camera Picker。
看您提供的代码是在开发一个相机应用,那么您需要创建Surface并且通过XComponent组件以实现预览流的显示,完整的示例可以参考这个demo:third-party-camera,相关开发文档可以参考生态应用相机实现系统级相机体验。
PS:目前最新的api12的release版本已发布,建议可以升级到最新的IDE版本哈。
更多关于CameraKit 相机管理 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
CameraKit 是鸿蒙Next中用于相机管理的框架,提供了相机设备的访问和控制功能。开发者可以通过CameraKit实现相机预览、拍照、录像等操作。CameraKit支持多相机设备的管理,允许开发者选择特定的相机设备进行操作。CameraKit还提供了丰富的API,用于设置相机参数,如分辨率、帧率、对焦模式等。此外,CameraKit还支持实时图像处理,开发者可以通过回调函数获取相机捕获的图像数据,并进行进一步的处理。CameraKit的设计旨在简化相机应用的开发流程,提升开发效率。
CameraKit 是 HarmonyOS 鸿蒙Next 中用于相机管理的核心组件,提供了一套简洁易用的 API,支持开发者快速集成相机功能。它支持多相机设备管理、实时预览、拍照、录像、图像处理等操作,并优化了性能与功耗。CameraKit 还支持自定义相机参数设置,如分辨率、帧率、对焦模式等,满足不同场景需求。通过 CameraKit,开发者可以高效实现高质量的相机应用,适用于拍照、视频录制、AR 等场景。