了解更多开源内容请访问:开源基础软件社区https://ost.51cto.com随着版本更新OpenHarmony,在3.2上已经提供了非常丰富的API来调用camera。此处说明了本机使用相机的过程。同时,和普通Android应用开发一样,使用一个intent直接调用系统相机应用进行拍照。根据调用摄像头的原生API,您可以定义自己的功能更丰富的摄像头应用。这里之所以特别强调OpenHarmony3.2beta4,是因为我发现虽然同为3.2版本,但beta4上的相机相关api与beta2版本差别很大,所以选择最新版本进行说明。既然用到了摄像头,那么第一步就是想办法点亮摄像头,也就是可以通过摄像头看到预览画面,然后实现摄像头、视频、分布式摄像头等功能。关于sdk的问题目前在OpenHarmony3.2上调用camera需要使用ohos-full-sdk,而不是大家从DevEcoStudio下载的sdk,那个sdk叫做publicsdk。sdk的替换方法可以参考官方文档《full-SDK替换指南》,这里不再赘述。这里最核心需要注意的是,我在3.2beta4上使用的sdk对应的版本号是3.2.9.4,而官方文档上写的sdk最高可以下载的版本只有3.2.5.6。因此,我们需要手动下载系统源码,自己完成sdk的编译。这里是我们自己基于3.2beta4系统源码编译的full-sdk。使摄像头开启预览画面核心流程及代码实现(1)动态权限申请需要获取ohos.permission.CAMERA权限(2)摄像头相关API操作流程以上为摄像头全功能时序图,这里我们只按照时序图的流程只实现预览部分。(3)配合XComponent组件完成相机预览流的输出。在XComponent组件中,可以通过XComponentController的getXComponentSurfaceId方法获取surfaceId,然后通过相机管理对象cameraManager.createPreviewOutput的key方法绑定surface,从而实现预览画面的输出。启用相机打开预览画面代码实现importcamerafrom'@ohos.multimedia.camera'constPERMISSIONS:Array=['ohos.permission.CAMERA']letpreviewWidth;letpreviewHeight;@Entry@ComponentstructIndex{privatemXComponentController:XComponentController=newXComponentController()privatesurfaceId:string='-1'asyncinitCamera(surfaceId:string){//动态获取隐私权限letcontext=getContext(this)asanyawaitcontext.requestPermissionsFromUser(PERMISSIONS)控制台。日志('grantPermission,requestPermissionsFromUser');//创建CameraManager对象}//获取摄像头列表letcameraArray=awaitcameraManager.getSupportedCameras()if(!cameraArray){console.error('获取摄像头失败');}for(letindex=0;index{console.log('opencamerasucc.');}).catch(function(err){console.log("opencamerafailedwitherror:"+err);});//获取相机设备支持的输出流能力letcameraOutputCap=awaitcameraManager.getSupportedOutputCapability(cameraArray[0]);if(!cameraOutputCap){console.error("outputCapabilityoutputCapability==null||undefined")}else{console.log("outputCapability:"+JSON.stringify(cameraOutputCap));}//获取相机支持的输出能力--支持的预览配置信息消息让previewProfilesArray=cameraOutputCap.previewProfiles;if(!previewProfilesArray){console.error("createOutputpreviewProfilesArray==null||undefined")}else{console.log("previewProfiles:"+JSON.stringify(previewProfilesArray[0]))previewWidth=previewProfilesArray[0]。大小.宽度;previewHeight=previewProfilesArray[0].size.height;}//创建预览输出流,其中参数surfaceId指的是下面的XComponent组件,预览流就是XComponent组件提供的surfaceletpreviewOutput=awaitcameraManager.createPreviewOutput(previewProfilesArray[0],surfaceId)if(!previewOutput){console.error("创建PreviewOutput实例失败。")}else{console.log("创建PreviewOutput实例成功。")}//createsessionletcaptureSession=awaitcameraManager.createCaptureSession()if(!captureSession){console.error('创建CaptureSession实例失败。');返回;}console.log('Captur返回的回调eSession实例。+捕获会话);//启动配置会话awaitcaptureSession.beginConfig().then(()=>{console.log('captureSessionbeginConfigsucc');}).catch(function(err){console.log("captureSessionbeginConfigfailedwitherror:"+错误);});//将摄像头输入流添加到会话中awaitcaptureSession.addInput(cameraInput).then(()=>{console.log('captureSessionaddInput实例已添加。');}).catch(function(err){console.log("captureSessionaddInput失败,错误:"+err);});//向会话添加预览输入流awaitcaptureSession.addOutput(previewOutput).then(()=>{console.log('captureSessionaddOutputpreviewOutput实例已添加。');}).catch(function(err){console.log("captureSessionaddOutputpreviewOutput失败,错误:"+err);});//提交会话配置awaitcaptureSession.commitConfig().then(()=>{console.log('captureSessioncommitConfigsuccess.');}).catch(function(err){console.log("captureSessioncommitConfig失败,错误:"+err);});//启动会话awaitcaptureSession.start().then(()=>{console.log('captureSession启动成功。');}).catch(function(err){console.log("captureSession启动失败,错误:"+错误);});}build(){Flex(){XComponent({//创建XComponentid:'',type:'surface',libraryname:'',controller:this.mXComponentController}).onLoad(()=>{//设置onload回调//设置Surface宽高(1920*1080),预览尺寸设置是指当前设备从上一次previewProfilesArray中获取的支持的预览分辨率尺寸来设置this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:previewWidth,surfaceHeight:previewHeight})//获取表面IDthis.surfaceId=this.mXComponentController.getXComponentSurfaceId()this.initCamera(this.surfaceId)}).width('100%')//设置XComponentwidth.height('100%')//设置XComponent高度}}}更多开源信息请访问:开源基础软件社区https://ost.51cto.com