最终效果如下
需要实现的主要功能:1.读取指定的图像 2. 读取连接的相机图像
代码如下
QCameraInfo.pro 主要是 1.配置了halcon环境,使用了绝对路径,建议将halcon文件移到项目下,改成相对路径 2.添加了资源文件,用于配置样式.qss
QT += core gui
QT += multimedia
QT += multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhalcon
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhalconc
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhalconcpp
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhalconcppxl
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhalconcxl
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhalconxl
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhdevenginecpp
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -lhdevenginecppxl
win32: LIBS += -LE:/HALCON-18.11-Steady/lib/x64-win64/ -llibiomp5md
INCLUDEPATH += E:/HALCON-18.11-Steady/include
DEPENDPATH += E:/HALCON-18.11-Steady/include
RESOURCES += \
Resuorces.qrc
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "halconcpp/HalconCpp.h"
#include "Halcon.h"
#include <QLabel>
#include <QPushButton>
#include <QTabWidget>
#include <QGroupBox>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QDebug>
#include <QMouseEvent>
#include <QPainter>
#include <QLineEdit>
#include <QTextEdit>
#include<QFileDialog>
#include<QCoreApplication>
#include <QTimer>
#include <QPalette>
#include <QWidget>
#include <QMessageBox>
#include <QTabWidget>
#include <QGroupBox>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QDebug>
#include <QMouseEvent>
#include <QPainter>
#include <QLineEdit>
#include <QTextEdit>
#include <QComboBox>
using namespace HalconCpp;
#include <QWidget>
#pragma execution_character_set("utf-8") //支持中文
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QLabel *label;
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QPushButton *btn4;
HTuple hv_AcqHandle,hv_WindowHandle;
HObject ho_Image;
HTuple hv_Width, hv_Height;
Hlong windID;
QTimer *timer;
Hlong MainWndID;
QTabWidget *tabwidget;
QWidget *tab1;
QWidget *tab2;
QTextEdit *qle;
QComboBox *cbox;
HTuple hv_Information, hv_ValueList;
//QTextEdit *qle2;
private slots:
void on_clicked1();
void on_clicked2();
void on_clicked3();
void on_clicked4();
void camera();
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QFile qss(":/new/prefix1/bule.qss");
if( qss.open(QFile::ReadOnly))
{
qDebug("open success");
QString style = QLatin1String(qss.readAll());
a.setStyleSheet(style);
qss.close();
}
else
{
qDebug("Open failed");
}
w.show();
return a.exec();
}
Widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(1000,575);
this->setWindowTitle("图像读取");
label = new QLabel(this);
label->setGeometry(350,25,600,500);
MainWndID = (Hlong)this->label->winId();
OpenWindow(0, 0, label->width(), label->height(), MainWndID, "visible", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
qle = new QTextEdit(this);
qle->setGeometry(25,325,300,200);
qle->setReadOnly(true);
tabwidget = new QTabWidget(this);
tab1 = new QWidget;
tab2 = new QWidget;
//tab3 = new QWidget;
tabwidget->addTab(tab1, "读取指定图像");
tabwidget->addTab(tab2, "读取相机图像");
tabwidget->setGeometry(25, 25, 300, 300);
QVBoxLayout *vbox1 = new QVBoxLayout;
vbox1->setAlignment(Qt::AlignTop|Qt::AlignHCenter);
tab1->setLayout(vbox1);
btn1 = new QPushButton();
btn1->setFixedSize(100,50);
btn1->setText("选择图片");
vbox1->addWidget(btn1);
QVBoxLayout *vbox2 = new QVBoxLayout;
vbox2->setAlignment(Qt::AlignTop|Qt::AlignHCenter);
tab2->setLayout(vbox2);
btn2 = new QPushButton();
btn2->setFixedSize(100,50);
btn2->setText("打开相机");
btn2->setEnabled(false);
btn3 = new QPushButton();
btn3->setFixedSize(100,50);
btn3->setText("关闭相机");
btn3->setEnabled(false);
btn4 = new QPushButton();
btn4->setFixedSize(100,50);
btn4->setText("检测相机");
cbox = new QComboBox();
cbox->setFixedSize(200,30);
vbox2->addWidget(btn4);
vbox2->addWidget(cbox);
vbox2->addWidget(btn2);
vbox2->addWidget(btn3);
timer = new QTimer(this);
connect(this->btn1,SIGNAL(clicked()),this,SLOT(on_clicked1()));
connect(this->btn2,SIGNAL(clicked()),this,SLOT(on_clicked2()));
connect(this->btn3,SIGNAL(clicked()),this,SLOT(on_clicked3()));
connect(timer,SIGNAL(timeout()),this,SLOT(camera()));
connect(this->btn4,SIGNAL(clicked()),this,SLOT(on_clicked4()));
}
Widget::~Widget()
{
}
void Widget::on_clicked1(){
qDebug() << "按下了1号按钮,读取图片";
try
{
QString file_name;
file_name = QFileDialog::getOpenFileName(this,tr("open file"),".",tr("Video Files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm)"));
qle->setText("读取图像:"+file_name);
QByteArray ba = file_name.toLatin1();
const char *str = ba.data();
HTuple ImageName(str);
ReadImage(&ho_Image, ImageName);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
DispObj(ho_Image, HDevWindowStack::GetActive());
}
catch (HException &HDevExpDefaultException)
{
HTuple hv_Exception;
HDevExpDefaultException.ToHTuple(&hv_Exception);
}
}
void Widget::on_clicked2(){
qDebug() << "按下了2号按钮,打开相机";
qle->setText("打开相机");
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] USB2.0 PC CAMERA", 0, -1, &hv_AcqHandle);*/
try
{
int curIndex = cbox->currentIndex();
OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false",
"default", hv_ValueList[curIndex], 0, -1, &hv_AcqHandle);
GrabImageStart(hv_AcqHandle, -1);
// 在openwindow前先抓取一帧图片
GrabImage(&ho_Image,hv_AcqHandle);
btn3->setEnabled(true);
btn2->setEnabled(false);
}
catch (HException &HDevExpDefaultException)
{
HTuple hv_Exception;
HDevExpDefaultException.ToHTuple(&hv_Exception);
}
}
void Widget::on_clicked3(){
qDebug() << "按下了3号按钮,关闭相机";
qle->setText("关闭相机");
timer->stop();
CloseFramegrabber(hv_AcqHandle);
btn3->setEnabled(false);
btn2->setEnabled(true);
}
void Widget::camera()
{
GrabImage(&ho_Image, hv_AcqHandle);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
if(HDevWindowStack::IsOpen())
{
DispObj(ho_Image,HDevWindowStack::GetActive());
}
//WaitSeconds(0.1);
}
void Widget::on_clicked4(){
qDebug() << "按下了4号按钮,检测相机";
try
{
InfoFramegrabber("DirectShow", "device", &hv_Information, &hv_ValueList);
HString str1 = hv_Information.S();
int num = hv_ValueList.Length();
QString result = "检测到"+QString::number(num)+"个相机"+"\n"+str1.Text()+"\n";
// HString str2 = "";
cbox->clear();
for (int i = 0; i < num; ++i) {
HString str2 = hv_ValueList[i].S();
cbox->addItem(QString(str2));
result = result + str2.Text()+"\n";
}
qDebug()<<result;
qle->setText(result);
btn2->setEnabled(true);
}
catch (HException &HDevExpDefaultException)
{
HTuple hv_Exception;
HDevExpDefaultException.ToHTuple(&hv_Exception);
qle->setText("没检测到相机");
}
}
//
版本2
调节了控件的布局位置
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QTabWidget>
#include <QGroupBox>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QDebug>
#include <QMouseEvent>
#include <QPainter>
#include <QLineEdit>
#include <QTextEdit>
#include<QFileDialog>
#include<QCoreApplication>
#include <QTimer>
#include <QPalette>
#include <QWidget>
#include <QMessageBox>
#include <QTabWidget>
#include <QGroupBox>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QDebug>
#include <QMouseEvent>
#include <QPainter>
#include <QLineEdit>
#include <QTextEdit>
#include <QComboBox>
#include "halconcpp/HalconCpp.h"
#include "Halcon.h"
using namespace HalconCpp;
#pragma execution_character_set("utf-8") //支持中文
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QLabel *label;
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QPushButton *btn4;
HTuple hv_AcqHandle,hv_WindowHandle;
HObject ho_Image;
HTuple hv_Width, hv_Height;
Hlong windID;
QTimer *timer;
Hlong MainWndID;
QTabWidget *tabwidget;
QWidget *tab1;
QWidget *tab2;
QTextEdit *qle;
QComboBox *cbox;
HTuple hv_Information, hv_ValueList;
void createWidget();
//QTextEdit *qle2;
private slots:
void on_clicked1();
void on_clicked2();
void on_clicked3();
void on_clicked4();
void camera();
};
#endif // WIDGET_H
Widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
createWidget();
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(camera()));
connect(this->btn1,SIGNAL(clicked()),this,SLOT(on_clicked1()));
connect(this->btn2,SIGNAL(clicked()),this,SLOT(on_clicked2()));
connect(this->btn3,SIGNAL(clicked()),this,SLOT(on_clicked3()));
connect(this->btn4,SIGNAL(clicked()),this,SLOT(on_clicked4()));
}
Widget::~Widget()
{
}
void Widget::createWidget(){
this->setFixedSize(1000,575);
this->setWindowTitle("图像读取");
label = new QLabel(this);
label->setGeometry(350,25,600,500);
MainWndID = (Hlong)this->label->winId();
OpenWindow(0, 0, label->width(), label->height(), MainWndID,
"visible", "", &hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
qle = new QTextEdit(this);
//qle->setGeometry(25,325,300,200);
qle->setGeometry(25,330,300,195);
qle->setReadOnly(true);
tabwidget = new QTabWidget(this);
tab1 = new QWidget;
tab2 = new QWidget;
//tab3 = new QWidget;
tabwidget->addTab(tab1, "读取指定图像");
tabwidget->addTab(tab2, "读取相机图像");
tabwidget->setGeometry(25, 25, 300, 300);
tabwidget->setTabShape(QTabWidget::Triangular);
QVBoxLayout *vbox1 = new QVBoxLayout;
//vbox1->setAlignment(Qt::AlignTop|Qt::AlignHCenter);
vbox1->setAlignment(Qt::AlignCenter);
tab1->setLayout(vbox1);
btn1 = new QPushButton();
btn1->setFixedSize(100,50);
btn1->setText("选择图片");
vbox1->addWidget(btn1);
QVBoxLayout *vbox2 = new QVBoxLayout;
//vbox2->setSpacing(20);
//vbox2->addStretch();
//vbox2->setContentsMargins(0,9,0,9);
//vbox2->setAlignment(Qt::AlignTop|Qt::AlignHCenter);
//vbox2->setAlignment(Qt::AlignVCenter);
vbox2->setAlignment(Qt::AlignHCenter);
//vbox2->setAlignment(Qt::AlignJustify);
//vbox2->addSpacing(10);
//vbox2->setSpacing(20);
tab2->setLayout(vbox2);
btn2 = new QPushButton();
btn2->setFixedSize(100,50);
btn2->setText("打开相机");
btn2->setEnabled(false);
btn3 = new QPushButton();
btn3->setFixedSize(100,50);
btn3->setText("关闭相机");
btn3->setEnabled(false);
QVBoxLayout *vbox5 = new QVBoxLayout;
vbox5->addWidget(btn2);
vbox5->addWidget(btn3);
vbox5->setAlignment(Qt::AlignHCenter);
vbox5->setSpacing(20);
btn4 = new QPushButton();
btn4->setFixedSize(100,50);
btn4->setText("检测相机");
QVBoxLayout *vbox4 = new QVBoxLayout;
vbox4->addWidget(btn4);
vbox4->setAlignment(Qt::AlignHCenter);
QVBoxLayout *vbox3 = new QVBoxLayout;
cbox = new QComboBox();
//cbox->setFixedSize(200,30);
cbox->setFixedSize(200,35);
vbox3->addWidget(cbox);
vbox3->setAlignment(Qt::AlignHCenter);
//vbox2->addWidget(btn4);
QWidget *qw0 = new QWidget;
qw0->setLayout(vbox4);
vbox2->addWidget(qw0);
//vbox2->addStretch();
//vbox2->addWidget(cbox);
QWidget *qw1 = new QWidget;
qw1->setLayout(vbox3);
vbox2->addWidget(qw1);
//vbox2->addWidget(vbox3);
//vbox2->addWidget(btn2);
//vbox2->addWidget(btn3);
QWidget *qw2 = new QWidget;
qw2->setLayout(vbox5);
vbox2->addWidget(qw2);
//vbox2->addSpacing(50);
}
void Widget::on_clicked1(){
qDebug() << "按下了1号按钮,读取图片";
try
{
QString file_name;
file_name = QFileDialog::getOpenFileName(this,tr("open file"),".",tr("Video Files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm)"));
qle->setText("读取图像:"+file_name);
QByteArray ba = file_name.toLatin1();
const char *str = ba.data();
HTuple ImageName(str);
ReadImage(&ho_Image, ImageName);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
DispObj(ho_Image, HDevWindowStack::GetActive());
}
catch (HException &HDevExpDefaultException)
{
HTuple hv_Exception;
HDevExpDefaultException.ToHTuple(&hv_Exception);
}
}
void Widget::on_clicked2(){
qDebug() << "按下了2号按钮,打开相机";
qle->setText("打开相机");
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] USB2.0 PC CAMERA", 0, -1, &hv_AcqHandle);*/
try
{
int curIndex = cbox->currentIndex();
OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false",
"default", hv_ValueList[curIndex], 0, -1, &hv_AcqHandle);
GrabImageStart(hv_AcqHandle, -1);
// 在openwindow前先抓取一帧图片
GrabImage(&ho_Image,hv_AcqHandle);
btn3->setEnabled(true);
btn2->setEnabled(false);
}
catch (HException &HDevExpDefaultException)
{
HTuple hv_Exception;
HDevExpDefaultException.ToHTuple(&hv_Exception);
}
}
void Widget::on_clicked3(){
qDebug() << "按下了3号按钮,关闭相机";
qle->setText("关闭相机");
timer->stop();
CloseFramegrabber(hv_AcqHandle);
btn3->setEnabled(false);
btn2->setEnabled(true);
}
void Widget::camera()
{
GrabImage(&ho_Image, hv_AcqHandle);
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
if(HDevWindowStack::IsOpen())
{
DispObj(ho_Image,HDevWindowStack::GetActive());
}
//WaitSeconds(0.1);
}
void Widget::on_clicked4(){
qDebug() << "按下了4号按钮,检测相机";
try
{
InfoFramegrabber("DirectShow", "device", &hv_Information, &hv_ValueList);
HString str1 = hv_Information.S();
int num = hv_ValueList.Length();
QString result = "检测到"+QString::number(num)+"个相机"+"\n"+str1.Text()+"\n";
// HString str2 = "";
cbox->clear();
for (int i = 0; i < num; ++i) {
HString str2 = hv_ValueList[i].S();
cbox->addItem(QString(str2));
result = result + str2.Text()+"\n";
}
qDebug()<<result;
qle->setText(result);
btn2->setEnabled(true);
}
catch (HException &HDevExpDefaultException)
{
HTuple hv_Exception;
HDevExpDefaultException.ToHTuple(&hv_Exception);
qle->setText("没检测到相机");
}
}
编译运行