尝试将相机采集图像流程封装成相机采图类

embedded/2025/3/24 8:13:28/

封装相机采集模块,GrabOnce.cpp下面是一个完整的相机采集图像的流程(控制台模式),要求把这个流程封装成一个相机采图类,类接口要求:
1. 三个对外接口,相机初始化,开始采图,停止采图
2. 类的构造,析构函数要完整,成员变量(私有,公有)定义完善,私有函数和接口函数区分开
3. 界面需要有三个按钮,初始化相机按钮,开始采图按钮,停止采图按钮
4. 语言C++, 界面Qt

一、1. 相机采集类封装(C++/Qt)

//--------------------------- CameraCapture.h ---------------------------
#pragma once
#include <QObject>
#include <QImage>
#include <atomic>
#include "IKapBoard.h"
#include "IKapC.h"class CameraCapture : public QObject {Q_OBJECT
public:explicit CameraCapture(QObject *parent = nullptr);~CameraCapture();// 对外接口bool initCamera();void startGrabbing();void stopGrabbing();signals:void imageGrabbed(const QImage &image);  // 图像采集信号void errorOccurred(const QString &msg);  // 错误信号private:// IKapC 和 IKapBoard 资源ITKDEVICE m_cameraHandle = nullptr;HANDLE m_boardHandle = INVALID_HANDLE_VALUE;std::atomic_bool m_isGrabbing{false};bool m_isInitialized = false;// 私有方法bool configureCamera();bool configureFrameGrabber();void registerCallbacks();void releaseResources();// 回调函数包装器static void CALLBACK OnFrameReadyWrapper(void *context);void handleFrameReady();
};
//--------------------------- CameraCapture.cpp ---------------------------
#include "CameraCapture.h"
#include <QDebug>CameraCapture::CameraCapture(QObject *parent) : QObject(parent) {}CameraCapture::~CameraCapture() {stopGrabbing();releaseResources();
}bool CameraCapture::initCamera() {if (m_isInitialized) return true;// 初始化 IKapCITKSTATUS res = ItkManInitialize();if (res != ITKSTATUS_OK) {emit errorOccurred("IKapC initialization failed");return false;}// 配置相机和采集卡if (!configureCamera() || !configureFrameGrabber()) {releaseResources();return false;}m_isInitialized = true;return true;
}void CameraCapture::startGrabbing() {if (!m_isInitialized || m_isGrabbing) return;ITKSTATUS res = ItkDevExecuteCommand(m_cameraHandle, "AcquisitionStart");if (res != ITKSTATUS_OK) {emit errorOccurred("Failed to start acquisition");return;}int ret = IKapStartGrab(m_boardHandle, 1);if (ret != 1) {emit errorOccurred("Failed to start grabbing");return;}m_isGrabbing = true;
}void CameraCapture::stopGrabbing() {if (!m_isGrabbing) return;int ret = IKapStopGrab(m_boardHandle);if (ret != 1) {emit errorOccurred("Failed to stop grabbing");}m_isGrabbing = false;
}//--------------------------- 私有方法实现 ---------------------------
bool CameraCapture::configureCamera() {// ... (参考 GrabOnce.cpp 中的 ConfigureCamera 实现)// 枚举设备、打开相机和采集卡return true;
}bool CameraCapture::configureFrameGrabber() {// ... (参考 GrabOnce.cpp 中的 ConfigureFrameGrabber 实现)// 配置缓冲区、超时、注册回调registerCallbacks();return true;
}void CameraCapture::registerCallbacks() {// 注册回调函数,传递 this 指针作为上下文IKapRegisterCallback(m_boardHandle, IKEvent_FrameReady, &CameraCapture::OnFrameReadyWrapper, this);
}void CameraCapture::releaseResources() {if (m_boardHandle != INVALID_HANDLE_VALUE) {IKapClose(m_boardHandle);m_boardHandle = INVALID_HANDLE_VALUE;}if (m_cameraHandle != nullptr) {ItkDevClose(m_cameraHandle);m_cameraHandle = nullptr;}ItkManTerminate();m_isInitialized = false;
}//--------------------------- 回调函数包装器 ---------------------------
void CALLBACK CameraCapture::OnFrameReadyWrapper(void *context) {CameraCapture *self = static_cast<CameraCapture*>(context);self->handleFrameReady();
}void CameraCapture::handleFrameReady() {unsigned char *buffer = nullptr;int frameSize = 0;IKAPBUFFERSTATUS status;// 获取图像数据IKapGetBufferStatus(m_boardHandle, 0, &status);if (status.uFull == 1) {IKapGetInfo(m_boardHandle, IKP_FRAME_SIZE, &frameSize);IKapGetBufferAddress(m_boardHandle, 0, (void**)&buffer);// 转换为 QImage(假设图像格式为 RGB888)QImage img(buffer, status.uWidth, status.uHeight, QImage::Format_RGB888);emit imageGrabbed(img);  // 发送图像信号}
}

 二、QT界面实现

//--------------------------- MainWindow.h ---------------------------
#include <QMainWindow>
#include "CameraCapture.h"namespace Ui { class MainWindow; }class MainWindow : public QMainWindow {Q_OBJECT
public:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void onInitClicked();void onStartClicked();void onStopClicked();void updateImage(const QImage &img);void handleError(const QString &msg);private:Ui::MainWindow *ui;CameraCapture *m_camera;
};
//--------------------------- MainWindow.cpp ---------------------------
#include "MainWindow.h"
#include "ui_MainWindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow), m_camera(new CameraCapture(this)) {ui->setupUi(this);// 初始化按钮状态ui->startButton->setEnabled(false);ui->stopButton->setEnabled(false);// 连接信号槽connect(ui->initButton, &QPushButton::clicked, this, &MainWindow::onInitClicked);connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::onStartClicked);connect(ui->stopButton, &QPushButton::clicked, this, &MainWindow::onStopClicked);connect(m_camera, &CameraCapture::imageGrabbed, this, &MainWindow::updateImage);connect(m_camera, &CameraCapture::errorOccurred, this, &MainWindow::handleError);
}MainWindow::~MainWindow() {delete ui;
}void MainWindow::onInitClicked() {if (m_camera->initCamera()) {ui->startButton->setEnabled(true);ui->initButton->setEnabled(false);}
}void MainWindow::onStartClicked() {m_camera->startGrabbing();ui->startButton->setEnabled(false);ui->stopButton->setEnabled(true);
}void MainWindow::onStopClicked() {m_camera->stopGrabbing();ui->startButton->setEnabled(true);ui->stopButton->setEnabled(false);
}void MainWindow::updateImage(const QImage &img) {ui->imageLabel->setPixmap(QPixmap::fromImage(img).scaled(ui->imageLabel->size(), Qt::KeepAspectRatio));
}void MainWindow::handleError(const QString &msg) {QMessageBox::critical(this, "Error", msg);
}

 三、Qt 界面文件(MainWindow.ui)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><widget class="QWidget" name="centralWidget"><layout class="QVBoxLayout"><item><widget class="QLabel" name="imageLabel"><property name="minimumSize"><size><width>640</width><height>480</height></size></property></widget></item><item><layout class="QHBoxLayout"><item><widget class="QPushButton" name="initButton"><property name="text"><string>Initialize</string></property></widget></item><item><widget class="QPushButton" name="startButton"><property name="text"><string>Start</string></property></widget></item><item><widget class="QPushButton" name="stopButton"><property name="text"><string>Stop</string></property></widget></item></layout></item></layout></widget></widget>
</ui>


http://www.ppmy.cn/embedded/175016.html

相关文章

深度学习视觉BEV 3D目标检测算法综述

目录 一、基于深度估计的BEV方法 1.1 LSS算法&#xff08;Lift, Splat, Shoot&#xff0c;2020&#xff09; 1.2 BEVDet算法&#xff08;High-Performance Multi-Camera 3D Object Detection in Bird-Eye-View&#xff0c;2022&#xff09; 1.3 BEVDet4D算法&#xff08;Ex…

HTTPS 加密过程详解

HTTPS 详解及其加密过程流程框架 HTTPS&#xff08;Hypertext Transfer Protocol Secure&#xff09;是一种基于 HTTP 协议的安全通信协议&#xff0c;通过 SSL/TLS 协议对传输数据进行加密和身份验证&#xff0c;解决了 HTTP 明文传输的安全隐患。以下是其核心原理和加密流程的…

python 游戏开发cocos2d库安装与使用

Cocos2d-x 是一个广泛使用的开源游戏开发框架&#xff0c;支持多种编程语言&#xff0c;包括 Python。对于 Python 开发者来说&#xff0c;通常使用的是 Cocos2d-py 或者更现代的 Cocos2d-x 的 Python 绑定版本。这里我将指导你如何安装和开始使用 Cocos2d-py。 安装步骤 安装…

接口自动化测试框架详解

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 接口自动化测试是指通过编写程序来模拟用户的行为&#xff0c;对接口进行自动化测试。Python是一种流行的编程语言&#xff0c;它在接口自动化测试中得到了广泛…

【el-upload】el-upload组件 - list-type=“picture“ 时,文件预览展示优化

目录 问题图el-upload预览组件 PicturePreview效果展示 问题图 el-upload <el-uploadref"upload"multipledragaction"#":auto-upload"false":file-list"fileList"name"files":accept".png,.jpg,.jpeg,.JGP,.JPEG,.…

基于springboot的企业客户管理系统(024)

摘 要 本论文主要论述了如何使用JAVA语言开发一个企业客户管理系统&#xff0c;本系统将严格按照软件开发流程进行各个阶段的工作&#xff0c;采用B/S架构&#xff0c;面向对象编程思想进行项目开发。在引言中&#xff0c;作者将论述企业客户管理系统的当前背景以及系统开发的目…

leetcode106 从中序与后序遍历序列构造二叉树

中序遍历的根节点左侧是左子树&#xff0c;右侧是右子树&#xff0c;后序遍历的最后一个元素为根节点。 在中序遍历中找到根节点&#xff0c;从而找到左右子树&#xff0c;知道左右子树的范围&#xff0c;从而后序遍历中的左右子树也就确定好了。 然后分别对左右子树用同样的…

实战指南:智慧水厂管理平台搭建全流程解析(二)

【上期内容】我们重点解析了智慧水务管理平台的五大核心模块&#xff1a;管理驾驶舱、资产管理、库存管理、水质分析、能耗分析。 【本期内容】我们将继续深入探讨智慧水务管理平台的进阶功能模块&#xff1a;工艺流程图、系统配电、电器设备状态、监控查看、GIS地图。 ⬇️智慧…