赞
踩
Baumer工业相机堡盟相机是一种高性能、高质量的工业相机,可用于各种应用场景,如物体检测、计数和识别、运动分析和图像处理。
Baumer的万兆网相机拥有出色的图像处理性能,可以实时传输高分辨率图像。此外,该相机还具有快速数据传输、低功耗、易于集成以及高度可扩展性等特点。
Baumer工业相机由于其性能和质量的优越和稳定,常用于高速同步采集领域,通常使用各种图像算法来提高其捕获的图像的质量。
Baumer工业相机NEOAPI SDK是用于Baumer工业相机的一款最新的软件开发工具包(SDK)。它为开发人员提供了一系列API和工具,用于与Baumer工业相机进行通信和控制,控制方式极为便捷类似Halcon的相机助手类控制方式。
本文只介绍使用OpenHarmony连接Baumer的USB相机,后续会相应测试网口Gige相机.
可以使用RK3568开发板和最新的DevEco Studio版本进行测试,具体如何搭建OpenHarmony开发环境并连接开发板进行调试,这里不再赘述,可以参考其它相关文档。
OpenHarmony是由开放原子开源基金会(OpenAtom Foundation)孵化及运营的开源项目,目标是面向全场景、全连接、全智能时代,基于开源的方式,搭建一个智能终端设备操作系统的框架和平台,促进万物互联产业的繁荣发展
OpenHarmony 框架支持在设备上操作相机和使用相机功能,为用户提供拍照和录像等场景的支持。本篇文章将介绍相机的开发流程和API的用法,详细如何在自己的应用中使用相机。
OpenHarmony 框架相机功能需要调用相机管理接口、图片处理接口、媒体服务接口和媒体库管理接口等API和组件实现完整的相机操作和使用。
下面介绍在基于开发板使用OpenHarmony连接Baumer工业USB相机并进行采图保存的流程
1.根据camera的getCameraManager方法获取CameraManager
2.通过CameraManager获取所有的相机数组,找到可用的相机,并获取相机的cameraid
3.创建图片接收器并进行订阅,获取receiver的surfaceId
4.通过CameraManager的createCameraInput(cameraid)创建相机输入流
5.通过camera的createPreviewOutput(sufaceId)创建相机预览输出流.这里sufaceId为XComponent的id
6.通过camera的createPhotoOutput(sufaceId)创建相机拍照输出流.这里sufaceId为图片接收器的surfaceId
7.会话管理:创建会话,并且配置会话的相机输入流,相机拍照输出流与相机预览流,提交配置,开始会话
在进行相机功能开发前需要先在配置文件中配置需要的权限信息。
Stage 模型配置 module.json5
代码如下(示例):
{ "module":{ ... "requestPermissions": [ { "name": "ohos.permission.CAMERA" }, { "name": "ohos.permission.MICROPHONE" }, { "name": "ohos.permission.MEDIA_LOCATION" }, { "name": "ohos.permission.READ_MEDIA" }, { "name": "ohos.permission.WRITE_MEDIA" } ] } ... }
FA 模型配置 config.json
{ ... "module":{ ... "reqPermissions": [ { "name": "ohos.permission.CAMERA" }, { "name": "ohos.permission.MICROPHONE" }, { "name": "ohos.permission.MEDIA_LOCATION" }, { "name": "ohos.permission.READ_MEDIA" }, { "name": "ohos.permission.WRITE_MEDIA" } ] } ... }
import camera from '@ohos.multimedia.camera' import image from '@ohos.multimedia.image' import fileio from '@ohos.fileio' import mediaLibrary from '@ohos.multimedia.mediaLibrary const Camerasize = {WIDTH: 640,HEIGHT: 4808} //获取相机管理器 import Camera from '@ohos.multimedia.camera' CameraManager = await Camera.getCameraManager(context: Context): Promise<CameraManager> //获取支持的所有相机设备列表 Cameras = await CameraManager.getCameras(): Promise<Array<Camera>> // 定义变量 private mXComponentController = new XComponentController() private cameraManager: camera.CameraManager = undefined private cameras: Array<camera.Camera> = undefined private cameraId: string = undefined private mReceiver: image.ImageReceiver = undefined private cameraInput: camera.CameraInput = undefined private previewOutput: camera.PreviewOutput = undefined private mSurfaceId: string = undefined private photoOutput: camera.PhotoOutput = undefined private captureSession: camera.CaptureSession = undefined private mediaUtil: MediaUtil = undefined @State desStr: string = "" private fileAsset: mediaLibrary.FileAsset private surfaceId: number @State photoUriMedia: string = "" private photoFlag: boolean = true @State imgUrl: string = "" @State isMediaUrl:boolean=true //判断保存路径为是沙箱路径或者媒体路径,默认媒体路径 aboutToAppear(){ this.mediaTest = mediaLibrary.getMediaLibrary(globalThis.context) }
//初始化相机和会话管理 async initCamera(surfaceId: number) { this.cameraManager = await camera.getCameraManager(globalThis.context)//需要在Ability中定义globalThis.context=this.context this.cameras = await this.cameraManager.getCameras() this.cameraId = this.cameras[1].cameraId await this.photoReceiver() //创建图片接收器并进行订阅 this.mSurfaceId = await this.mReceiver.getReceivingSurfaceId() this.cameraInput = await this.cameraManager.createCameraInput(this.cameraId) this.previewOutput = await camera.createPreviewOutput(surfaceId.toString()) this.photoOutput = await camera.createPhotoOutput(this.mSurfaceId) this.captureSession = await camera.createCaptureSession(globalThis.context) await this.captureSession.beginConfig() await this.captureSession.addInput(this.cameraInput) await this.captureSession.addOutput(this.previewOutput) await this.captureSession.addOutput(this.photoOutput) await this.captureSession.commitConfig() await this.captureSession.start().then(() => { console.log('zmw1--Promise returned to indicate the session start success.'); }) } //创建图片接收器并进行订阅 async photoReceiver() { this.mReceiver = image.createImageReceiver(CameraSize.WIDTH, CameraSize.HEIGHT, 4, 8) let buffer = new ArrayBuffer(4096) this.mReceiver.on('imageArrival', () => { console.log("zmw -service-imageArrival") this.mReceiver.readNextImage((err, image) => { if (err || image === undefined) { return } image.getComponent(4, (errMsg, img) => { if (errMsg || img === undefined) { return } if (img.byteBuffer) { buffer = img.byteBuffer } if(this.isMediaUrl){ this.savePictureMedia(buffer, image) }else{ this.savePictureSand(buffer, image) } }) }) return buffer }) }
//拍摄照片 async takePicture() { let photoSettings = { rotation: camera.ImageRotation.ROTATION_0, quality: camera.QualityLevel.QUALITY_LEVEL_LOW, mirror: false } await this.photoOutput.capture(photoSettings) } //保存沙箱路径 async savePictureSand(buffer: ArrayBuffer, img: image.Image) { let info = this.mediaUtil.getInfoFromType(mediaLibrary.MediaType.IMAGE) let dateTimeUtil = new DateTimeUtil() let name = `${dateTimeUtil.getDate()}_${dateTimeUtil.getTime()}` let displayName = `${info.prefix}${name}${info.suffix}` let sandboxDirPath = globalThis.context.filesDir; let path = sandboxDirPath + '/' + displayName this.imgUrl=path let fdSand = await fileio.open(path, 0o2 | 0o100, 0o666); await fileio.write(fdSand, buffer) await fileio.close(fdSand).then(()=>{ this.desStr="" }); await img.release() } //保存媒体路径 async savePictureMedia(buffer: ArrayBuffer, img: image.Image) { this.fileAsset = await this.mediaUtil.createAndGetUri(mediaLibrary.MediaType.IMAGE) this.imgUrl = this.fileAsset.uri let fd = await this.mediaUtil.getFdPath(this.fileAsset) await fileio.write(fd, buffer) await this.fileAsset.close(fd).then(()=>{ this.desStr="" }) await img.release() }
//结束释放相机资源 async releaseCamera() { if (this.captureSession) { await this.captureSession.stop().then(() => { }) } if (this.cameraInput) { await this.cameraInput.release().then(() => { }) } if (this.previewOutput) { await this.previewOutput.release().then(() => { }) } if (this.photoOutput) { await this.photoOutput.release().then(() => { }) } // 释放会话 if (this.captureSession) { await this.captureSession.release((err) => { if (err) { console.error('zmw Failed to release the CaptureSession instance ${err.message}'); return; } }); } }
连接Baumer的工业USB相机并联合OpenHarmony开发具有许多优势,尤其是在工业应用中。下面详细介绍这些优势:
结论
通过联合OpenHarmony开发连接USB相机,可以在工业应用中获得多种优势,包括兼容性、多样化的应用场景、软硬件协同、数据安全和系统稳定性。这种集成提供了更强大、灵活和可靠的工业视觉解决方案,有助于提升生产效率和质量,并支持智能制造和工业自动化的发展。
工业相机联合OpenHarmony开发连接USB相机的行业应用
连接USB相机并联合OpenHarmony开发在多个行业中具有广泛的应用,为各种应用场景提供了更灵活、可靠的视觉解决方案,包括但不限于以下几个行业:
智能农业: USB相机通过OpenHarmony框架实现智能农业领域应用,例如植物生长监测、病虫害识别和农作物采收等。
环境监测: 利用USB相机对环境进行实时视频监控,适用于自然环境变化监测、野生动物追踪和环境污染检测等。
**结论 **
工业相机联合OpenHarmony开发连接USB相机的行业应用极为广泛,覆盖制造业、智能制造、智能安防、医疗行业、农业与环境监测等多个领域,为各个行业提供了先进的视觉解决方案,推动了企业的自动化、智能化发展,提高了生产效率和产品质量,同时也提升了行业安全性和精准度。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。