HarmonyOS 鸿蒙Next 4.1 Release Camera Kit 会话管理commitConfig()返回错误码7400201该怎么解决
HarmonyOS 鸿蒙Next 4.1 Release Camera Kit 会话管理commitConfig()返回错误码7400201该怎么解决
执行getSupportedOutputCapability()
函数,返回的USB摄像头的输出流的分辨率很离谱,是131074:1,而且在提交会话配置的时候报错7400201,用厂家自配的MIPI摄像头就可以预览画面,请问这种情况该怎么解决。已经获取了相关权限。
开发板:purple pi oh
附上源码:
import camera from '@ohos.multimedia.camera';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import usb from '@ohos.usbManager';
@Entry
@Component
struct Index {
private mXComponentController: XComponentController = new XComponentController();
@State message: string = 'Hello World';
context: Context = getContext(this);
cameraManager: camera.CameraManager = camera.getCameraManager(this.context);
private surfaceId: string = ''
private xComponentContext: Record<string, () => void> = {}
isInput: boolean = false;
build() {
Row() {
Column() {
Row(){
XComponent({
id: 'test',
type: XComponentType.SURFACE,
controller: this.mXComponentController
})
.onLoad(()=>{
this.mXComponentController.setXComponentSurfaceSize({ surfaceWidth: 640, surfaceHeight: 480 })
this.surfaceId = this.mXComponentController.getXComponentSurfaceId()
this.xComponentContext = this.mXComponentController.getXComponentContext() as Record<string, () => void>
})
.width('640px')
.height('480px')
}.size({width: '80%', height: '30%'})
Row(){
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(async ()=>{
console.info(`-----------------------------------------------------------------------`)
let cameraArray: Array<camera.CameraDevice> = this.cameraManager.getSupportedCameras();
if (cameraArray != undefined && cameraArray.length > 0) {
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); // 获取相机连接类型 0:内置 1:USB连接
console.info(`-----------------------------------------------------------------------`)
}
}
let cameraDevice: camera.CameraDevice = cameraArray[1];
let sceneModes: Array<camera.SceneMode> = this.cameraManager.getSupportedSceneModes(cameraDevice);
console.info(JSON.stringify(sceneModes));
// 创建相机输入流
let cameraInput: camera.CameraInput = this.cameraManager.createCameraInput(cameraDevice);
// 监听cameraInput错误信息
cameraInput.on('error', cameraDevice, (error: BusinessError) =>{
console.error(`Camera input error code: ${error.code}`);
});
this.cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) =>{
console.info(`camera: ${cameraStatusInfo.camera.cameraId}`);
console.info(`status: ${cameraStatusInfo.status}`);
});
// 打开相机
await cameraInput.open();
// 获取相机设备支持的输出流能力
let cameraOutputCapability: camera.CameraOutputCapability = this.cameraManager.getSupportedOutputCapability(cameraDevice, camera.SceneMode.NORMAL_VIDEO);
console.info("previewProfilesArray" + JSON.stringify(cameraOutputCapability.previewProfiles));
console.info("photoProfilesArray" + JSON.stringify(cameraOutputCapability.photoProfiles));
console.info("videoProfilesArray" + JSON.stringify(cameraOutputCapability.videoProfiles));
let previewProfiles = cameraOutputCapability.previewProfiles[0];
previewProfiles.size.width = 640;
previewProfiles.size.height = 480;
let previewOutput = this.cameraManager.createPreviewOutput(cameraOutputCapability.previewProfiles[0], this.surfaceId);
// let photoOutput = this.cameraManager.createPhotoOutput(cameraOutputCapability.photoProfiles[0], this.surfaceId);
let videoOutput = this.cameraManager.createVideoOutput(cameraOutputCapability.videoProfiles[0], this.surfaceId);
let session: camera.VideoSession = this.cameraManager.createSession(camera.SceneMode.NORMAL_VIDEO) as camera.VideoSession;
session.beginConfig();
session.addInput(cameraInput);
try {
session.addOutput(previewOutput);
console.info("session add previewOutput success");
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to add previewOutput. error: ${JSON.stringify(err)}`);
}
try {
await session.commitConfig();
console.info("session commit config success");
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to commitConfig. error: ${JSON.stringify(err)}`);
}
try {
await session.start();
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to start. error: ${JSON.stringify(err)}`);
}
previewOutput.on('frameStart', ()=>{
console.info('Preview frame started');
});
previewOutput.on('error', (previewOutputError: BusinessError)=>{
console.error(`Preview output error code: ${previewOutputError.code}`);
});
})
}.size({width: '100%', height: '50%'})
Button('ON')
.onClick(()=>{
let deviceList : Array<usb.USBDevice> = usb.getDevices();
console.info(JSON.stringify(deviceList))
let deviceName : string = deviceList[1].name;
usb.requestRight(deviceName).then((hasRight : boolean)=>{
console.info("usb device request right result: " + hasRight);
}).catch((error : BusinessError)=>{
console.info("usb device request right failed : " + error);
});
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.SpaceAround)
}
.height('100%')
.width('100%')
}
}
日志打印信息如下:
更多关于HarmonyOS 鸿蒙Next 4.1 Release Camera Kit 会话管理commitConfig()返回错误码7400201该怎么解决的实战教程也可以访问 https://www.itying.com/category-93-b0.html
7400201及7400103错误码有对应的解释及方案,可以参考如下文档链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/errorcode-camera-V5#section7400103-%E4%BC%9A%E8%AF%9D%E6%9C%AA%E9%85%8D%E7%BD%AE
更多关于HarmonyOS 鸿蒙Next 4.1 Release Camera Kit 会话管理commitConfig()返回错误码7400201该怎么解决的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
错误码7400201通常表示在调用HarmonyOS Camera Kit的commitConfig()
方法时,会话配置提交失败。可能的原因包括:会话未正确初始化、配置参数不合法、或设备资源不足。建议您检查以下步骤:
- 确保
CameraSession
已正确初始化并处于可用状态。 - 验证传入
commitConfig()
的配置参数是否符合API要求。 - 检查设备资源(如内存、存储)是否充足。
- 查看日志以获取更多错误详情,或参考官方文档排查问题。