HarmonyOS 鸿蒙Next Camera-Kit 无法创建previewOutput: camera.PreviewOutput

发布于 1周前 作者 sinazl 最后一次编辑是 5天前 来自 鸿蒙OS

HarmonyOS 鸿蒙Next Camera-Kit 无法创建previewOutput: camera.PreviewOutput

如上传文件,按照文档使用cameraKit的方式写的代码报错了,无法创建previewOutput: camera.PreviewOutput

2 回复

HarmonyOS 鸿蒙Next Camera-Kit 无法创建previewOutput: camera.PreviewOutput 参考

// CameraCapture.ets
import camera from '@ohos.multimedia.camera';

export class CameraCapture {
private cameraManager?: camera.CameraManager
private fontCamera?: camera.CameraDevice //前置
private backCamera?: camera.CameraDevice //后置
private surfaceId: string = ""
private session?: camera.Session
private photoOuput?: camera.PhotoOutput
private cameraInput?: camera.CameraInput
private previewOutput?: camera.PreviewOutput

async init(surfaceId: string): Promise<void> {
this.surfaceId = surfaceId
try {
// 释放会话及其相关参数
await this.releaseCamera();
// 获取相机管理器实例
this.cameraManager = camera.getCameraManager(getContext(this));

let muted = this.cameraManager.isCameraMuted()
if (muted) {
return
}

// 获取支持指定的相机设备对象
let cameras = this.cameraManager.getSupportedCameras()
//查找前置相机和后置相机
cameras.forEach(item => {
if (item.cameraPosition == camera.CameraPosition.CAMERA_POSITION_BACK) {
this.backCamera = item
} else if (item.cameraPosition == camera.CameraPosition.CAMERA_POSITION_FRONT) {
this.fontCamera = item
}
})
let profiles = this.cameraManager.getSupportedOutputCapability(this.backCamera, camera.SceneMode.NORMAL_PHOTO);
let previewProfiles: Array<camera.Profile> = profiles.previewProfiles;
let photoProfiles: Array<camera.Profile> = profiles.photoProfiles;

// 创建previewOutput输出对象
this.previewOutput = this.cameraManager.createPreviewOutput(previewProfiles[0], this.surfaceId);
// 创建photoOutPut输出对象
this.photoOuput = this.cameraManager.createPhotoOutput(photoProfiles[0]);
// 创建cameraInput输出对象
this.cameraInput = this.cameraManager.createCameraInput(this.backCamera);

// 打开相机
await this.cameraInput.open();

// 会话流程
// 创建CaptureSession实例
this.session = this.cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO);
// 开始配置会话
this.session.beginConfig();
// 把CameraInput加入到会话
this.session.addInput(this.cameraInput);
// 把previewOutput加入到会话
this.session.addOutput(this.previewOutput);
// 把photoOutPut加入到会话
this.session.addOutput(this.photoOuput);
// 提交配置信息
await this.session.commitConfig();
// 开始会话工作
await this.session.start();

this.cameraManager.on('cameraStatus', (data) => {})
} catch (e) {
}
}

async releaseCamera(): Promise<void> {
if (this.previewOutput) {
try {
await this.previewOutput.release();
} catch (error) {
} finally {
this.previewOutput = undefined;
}
}
if (this.photoOuput) {
try {
await this.photoOuput.release();
} catch (error) {
} finally {
this.photoOuput = undefined;
}
}
if (this.session) {
try {
await this.session.release();
} catch (error) {
} finally {
this.session = undefined;
}
}
if (this.cameraInput) {
try {
await this.cameraInput.close();
} catch (error) {
} finally {
this.cameraInput = undefined;
}
}
}
}
// 测试页面CameraView.ets

import { CameraCapture } from '../viewmodel/CameraCaputre'
import { abilityAccessCtrl, common } from '@kit.AbilityKit';

@Entry
@Component
struct CameraView {
private camera?: CameraCapture
private controller: XComponentController = new XComponentController()
private surfaceId: string = '';
@State userGrant: boolean = false

async aboutToAppear() {
await this.requestCameraPermission()
}

async reqPermissionsFromUser(): Promise<number[]> {
let context = getContext() as common.UIAbilityContext;
let atManager = abilityAccessCtrl.createAtManager();
let grantStatus = await atManager.requestPermissionsFromUser(context, [
'ohos.permission.CAMERA',
]);
return grantStatus.authResults;
}

// 申请相机权限
async requestCameraPermission() {
let grantStatus = await this.reqPermissionsFromUser()
for (let i = 0; i < grantStatus.length; i++) {
if (grantStatus[i] === 0) {
// 用户授权,可以继续访问目标操作
this.userGrant = true;
}
}
}

build() {
Column() {
Text('相机')
if (this.userGrant) {
Row() {
XComponent({
id: 'componentId',
type: 'surface',
controller: this.controller
})
.onLoad(async () => {
this.surfaceId = this.controller.getXComponentSurfaceId();
this.camera = new CameraCapture()
await this.camera?.init(this.surfaceId)
})
.size({
width: 384,
height: 500
})
.margin({ bottom: 40 })
}
}
}
}
}

针对Camera-Kit升级为HarmonyOS鸿蒙Next Camera-Kit的问题,作为IT专家,我理解这一升级旨在提升摄像头组件的性能与兼容性。以下是一些专业见解:

HarmonyOS鸿蒙Next系统为华为自研的操作系统,其Camera-Kit作为系统的重要组成部分,升级后可能带来以下变化:

  • 性能提升:鸿蒙Next系统可能针对Camera-Kit进行了优化,提高了图像处理能力,使得拍照和录像更加流畅。
  • 兼容性增强:升级后的Camera-Kit可能支持更多型号的摄像头,提高了设备的兼容性。
  • 新功能增加:鸿蒙Next Camera-Kit可能新增了如AI修图、夜景模式等实用功能,提升了用户体验。

若您正在考虑或正在进行这一升级,请确保已备份重要数据,并遵循官方提供的升级指南进行操作。如果在升级过程中遇到问题,建议查阅华为官方文档或联系官网客服以获取帮助。官网地址是:https://www.itying.com/category-93-b0.html

回到顶部