最近想用qt做一个摄像头的界面,于是通过halcon调用摄像头,在导出到c++代码。
以下是导出的cpp文件的action函数
// Local iconic variables
HObject ho_Image;
// Local control variables
HTuple hv_AcqHandle;
//Image Acquisition 01: Code generated by Image Acquisition 01
OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false",
"default", "[0] USB Camera", 0, -1, &hv_AcqHandle);
GrabImageStart(hv_AcqHandle, -1);
while (0 != 1)
{
GrabImageAsync(&ho_Image, hv_AcqHandle, -1);
//Image Acquisition 01: Do something
}
CloseFramegrabber(hv_AcqHandle);
将此代码复制进qt程序中,我们需要先配置qt的halcon环境,参考我的上一篇博客:
链接: https://blog.csdn.net/Philloasd/article/details/106366474
在Qt creator中新建一个label以及两个putton控件
mainwindow.h代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include"Halcon.h"
#include"cpp/HalconCpp.h"
#include"halconcpp\HalconCpp.h"
using namespace HalconCpp;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
// Local iconic variables
HObject ho_Image;
// Local control variables
HTuple hv_AcqHandle,hv_WindowHandle;
Hlong windID;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_start_clicked();
void on_pushButton_stop_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindows.cpp代码如下
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_start_clicked()
{
OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false","default", "[0] USB Camera", 0, -1, &hv_AcqHandle);
windID = (Hlong)this->ui->label_camera->winId();
GrabImageStart(hv_AcqHandle, -1);
// 在openwindow前先抓取一帧图片
GrabImage(&ho_Image,hv_AcqHandle);
OpenWindow(0,0,ui->label_camera->width(),ui->label_camera->height(),windID,"visible","",&hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
while(0!=1)
{
GrabImage(&ho_Image, hv_AcqHandle);
if(HDevWindowStack::IsOpen())
{
DispObj(ho_Image,HDevWindowStack::GetActive());
WaitSeconds(0.1);//参数影响帧率
}
}
QCoreApplication::processEvents(QEventLoop::AllEvents,100);//防止卡死
}
void MainWindow::on_pushButton_stop_clicked()
{
CloseFramegrabber(hv_AcqHandle);//关闭摄像头
}
此时点击开始按钮我们是可以成功的在我们设定的label控件上实时显示摄像的内容,但是我们退出的时候会报错。
图片显示我们的内存发生了泄露,通过在vs中编译发现是退出时没有跳出while的死循环,于是参考这篇文章改用定时器代替while循环。
链接: https://blog.csdn.net/hexuyangguang/article/details/106219104.
修改后的mainwindow.h代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include"Halcon.h"
#include"cpp/HalconCpp.h"
#include"halconcpp\HalconCpp.h"
using namespace HalconCpp;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// Local iconic variables
HalconCpp::HObject ho_Image;
// Local control variables
HalconCpp::HTuple hv_AcqHandle,hv_WindowHandle;
Hlong windID;
private slots:
void on_pushButton_start_clicked();
void on_pushButton_stop_clicked();
void camera();//放置需要定时器循环的代码
private:
Ui::MainWindow *ui;
QTimer * timer;
};
#endif // MAINWINDOW_H
mainwindow.cpp代码如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(camera()));
connect(ui->pushButton_stop,SIGNAL(clicked(bool)),qApp,SLOT(quit()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_start_clicked()
{
timer->start(20);//定时器开始工作,参数影响帧率
//Image Acquisition 01: Code generated by Image Acquisition 01
OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false","default", "[0] USB Camera", 0, -1, &hv_AcqHandle);
windID = (Hlong)this->ui->label_camera->winId();
GrabImageStart(hv_AcqHandle, -1);
// 在openwindow前先抓取一帧图片
GrabImage(&ho_Image,hv_AcqHandle);
OpenWindow(0,0,ui->label_camera->width(),ui->label_camera->height(),windID,"visible","",&hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
}
void MainWindow::on_pushButton_stop_clicked()
{
timer->stop();
CloseFramegrabber(hv_AcqHandle);
}
void MainWindow::camera()
{
GrabImage(&ho_Image, hv_AcqHandle);
if(HDevWindowStack::IsOpen())
{
DispObj(ho_Image,HDevWindowStack::GetActive());
}
}
此时再次运行就没问题了!!!