1.在halcon运行目录下,找到include和lib这两个文件夹复制到qt项目文件.pro同一个文件目录
2.在qt的.pro文件中加入
INCLUDEPATH+=\
$$PWD/include\
$$PWD/include/halconcpp\
$$PWD/include/com\
$$PWD/include/halconc\
$$PWD/include/hdevengine\
$$PWD/include/hlib\
$$PWD/Hwin\
LIBS+=\
$$PWD/lib/x64-win64/halcon.lib\
$$PWD/lib/x64-win64/halconcpp.lib\
其中$$PWD代表.pro文件夹当前目录路径。
3.把lib文件夹里面的文件复制到软件运行目录下,到halcon运行目录找到lib文件夹里面.lib文件对应的.dll文件
4.halcon在c++中没有控件,但是可以绑定显示的控件代码如下:
/***创建一个窗口***/
_create_halcon_window=false;
Hlong winid_= this->winId();
HalconCpp::OpenWindow(50,0,this->width(),this->height(),winid_,"visible","",&_hv_window_handle);
_current_hwin_size._start_row_y=50+10;
_current_hwin_size._start_column_x=0+10;
_current_hwin_size._end_row_y=this->height()+50-10;
_current_hwin_size._end_column_x=this->width()-10;
_create_halcon_window=true;
5.一般鼠标移动,halcon绑定的窗口是不会响应的,要选中窗口按下按键才会响应,
6.显示控件代码
头文件
#ifndef QHALCONWIN_H
#define QHALCONWIN_H
#include <QObject>
#include <QWidget>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QLabel>
#include <QVBoxLayout>
#include "qhalconwin.h"
#include <HalconCpp.h>
#include <dealwithimage.h>
using namespace HalconCpp;
/***运行操作***/
enum OperImageEnum
{
/***获取像素坐标***/
_get_pix_position_value,
/***移动图片***/
_move_image
};
/***当前halcon显示窗口的坐标***/
class CurrentHWinSize
{
public:
double _start_column_x;
double _start_row_y;
double _end_column_x;
double _end_row_y;
};
class QHalconWin : public QWidget
{
Q_OBJECT
public:
explicit QHalconWin(QWidget *parent = nullptr);
/***刷新窗口***/
void refreshWin();
/***适应窗口***/
void fitWin();
protected:
/***滚轮事件***/
void wheelEvent(QWheelEvent *event) override;
/***鼠标按下事件***/
void mousePressEvent(QMouseEvent *event) override;
/***鼠标移动事件***/
void mouseMoveEvent(QMouseEvent *event) override;
/***鼠标释放事件***/
void mouseReleaseEvent(QMouseEvent *event) override;
/***绘制事件***/
void paintEvent(QPaintEvent *event) override;
/***显示事件***/
void showEvent(QShowEvent *event) override;
/***重置大小事件***/
void resizeEvent(QResizeEvent *event) override;
signals:
public:
/***创建halcon窗口标志***/
bool _create_halcon_window;
/***第一次显示的标志***/
bool _show_frist;
/***窗口句柄***/
HalconCpp::HTuple _hv_window_handle;
/***鼠标按下的坐标***/
HalconCpp::HTuple _mouse_down_row;
/***鼠标按下的坐标***/
HalconCpp::HTuple _mouse_down_column;
/***当前显示halcon的窗口的大小***/
CurrentHWinSize _current_hwin_size;
/***鼠标在有效范围内***/
bool _mouse_in_effective_range;
/***鼠标按下标志***/
bool _mouse_is_down;
OperImageEnum _oper_image_enum;
/***显示的信息***/
QLabel* _label_mes;
};
#endif // QHALCONWIN_H
内容文件
#include "qhalconwin.h"
#include "QDebug"
QHalconWin::QHalconWin(QWidget *parent)
: QWidget{parent}
{
/****这句话一定要加,不然不相应一些事件****/
this->setMouseTracking(true);
_label_mes=new QLabel(this);
_label_mes->setMaximumWidth(300);
_label_mes->setMinimumWidth(300);
_label_mes->setMaximumHeight(50);
_label_mes->setMinimumHeight(50);
_label_mes->setText("---");
/***创建一个窗口***/
_create_halcon_window=false;
Hlong winid_= this->winId();
HalconCpp::OpenWindow(50,0,this->width(),this->height(),winid_,"visible","",&_hv_window_handle);
_current_hwin_size._start_row_y=50+10;
_current_hwin_size._start_column_x=0+10;
_current_hwin_size._end_row_y=this->height()+50-10;
_current_hwin_size._end_column_x=this->width()-10;
_create_halcon_window=true;
_oper_image_enum=OperImageEnum::_move_image;
_show_frist=true;
_mouse_is_down = false;
_mouse_in_effective_range=false;
}
void QHalconWin::refreshWin()
{
if(DealWithImage::getStatic()->_exit_image==true)
{
this->update();
}
}
void QHalconWin::fitWin()
{
if(DealWithImage::getStatic()->_exit_image==true)
{
double win_width_,win_height_,width_scale_,height_scale_,scale_;
HalconCpp::HTuple img_width_,img_height_;
win_width_=this->width();
win_height_=this->height()-50;
HalconCpp::GetImageSize(DealWithImage::getStatic()->_image,
&img_width_,
&img_height_);
width_scale_=win_width_/img_width_;
height_scale_=win_height_/img_height_;
if(width_scale_<height_scale_)
{
scale_=width_scale_;
}
else
{
scale_=height_scale_;
}
double fit_width_=img_width_*scale_;
double fit_height_=img_height_*scale_;
double start_x_=(win_width_-fit_width_)/2;
/****后+50代表显示_label_mes高******/
double start_y_=(win_height_-fit_height_)/2+50;
/***边界要内缩10,防止在边界获取halcon坐标出错***/
_current_hwin_size._start_row_y=start_y_+10;
_current_hwin_size._start_column_x=start_x_+10;
_current_hwin_size._end_row_y=start_y_+fit_height_-10;
_current_hwin_size._end_column_x=start_x_+fit_width_-10;
HalconCpp::SetWindowExtents(this->_hv_window_handle,start_y_, start_x_, fit_width_, fit_height_);
HalconCpp::SetPart(this->_hv_window_handle, 0, 0, img_height_-1, img_width_-1);
this->refreshWin();
}
}
void QHalconWin::wheelEvent(QWheelEvent *event)
{
//QWidget::wheelEvent(event);
if(DealWithImage::getStatic()->_exit_image==true
&&_mouse_in_effective_range==true)
{
HalconCpp::HTuple mouse_row_, mouse_col_, button_;
HalconCpp::HTuple current_begin_row_,
current_begin_column_,
current_end_row_,
current_end_column_;
HalconCpp::HTuple img_width_,img_height_;
double win_width_,win_height_;
int zoom_begin_row_=0,zoom_begin_column_,zoom_end_row_,zoom_end_column_;
HalconCpp::GetMpositionSubPix(this->_hv_window_handle, &mouse_row_, &mouse_col_, &button_);
HalconCpp::GetPart(this->_hv_window_handle,
¤t_begin_row_,
¤t_begin_column_,
¤t_end_row_,
¤t_end_column_);
if (event->angleDelta().y()>0)
{
/***滚轮前滑,放大***/
zoom_begin_row_ = (int)(current_begin_row_.D() + (mouse_row_.D() - current_begin_row_.D()) * 0.300);
zoom_begin_column_ = (int)(current_begin_column_.D() + (mouse_col_.D() - current_begin_column_.D()) * 0.300);
zoom_end_row_ = (int)(current_end_row_.D() - (current_end_row_.D() - mouse_row_.D()) * 0.300);
zoom_end_column_ = (int)(current_end_column_.D() - (current_end_column_.D() - mouse_col_.D()) * 0.300);
}
else
{
/***否则缩小***/
zoom_begin_row_ = (int)(mouse_row_.D() - (mouse_row_.D() - current_begin_row_.D()) / 0.7);
zoom_begin_column_ = (int)(mouse_col_.D() - (mouse_col_.D() - current_begin_column_.D()) / 0.700);
zoom_end_row_ = (int)(mouse_row_.D() + (current_end_row_.D() - mouse_row_.D()) / 0.700);
zoom_end_column_ = (int)(mouse_col_.D() + (current_end_column_.D() - mouse_col_.D()) / 0.700);
}
win_width_=this->width()-100;
win_height_=this->height()-100;
HalconCpp::GetImageSize(DealWithImage::getStatic()->_image,
&img_width_,
&img_height_);
/***避免像素过大***/
bool is_out_of_area_ = true;
bool is_out_of_size_ = true;
bool is_out_of_pixel_ = true;
is_out_of_area_ = zoom_begin_row_ >= (int)img_height_ || zoom_end_row_ <= 0 || zoom_begin_column_ >= (int)img_width_ || zoom_end_column_ < 0;
is_out_of_size_ = (zoom_end_row_ - zoom_begin_row_) > (int)img_height_ * 20 || (zoom_end_column_ - zoom_begin_column_) > (int)img_width_ * 20;
is_out_of_pixel_ = win_height_ / (zoom_end_row_ - zoom_begin_row_) > 500 || win_width_ / (zoom_end_column_ - zoom_begin_column_) > 500;
if (is_out_of_area_ || is_out_of_size_)
{
HalconCpp::SetPart(this->_hv_window_handle, 0, 0, img_height_ - 1, img_width_ - 1);
}
else if (!is_out_of_pixel_)
{
//HSystem.SetSystem("flush_graphic", "false");//防止图片频闪
//hWindowControl1.HalconWindow.ClearWindow();
//_zoom_endCol = _zoom_beginCol + (_zoom_endRow - _zoom_beginRow) * hw_width / hw_height;
//hWindowControl1.HalconWindow.SetPart(_zoom_beginRow, _zoom_beginCol, _zoom_endRow, _zoom_endCol);
//HSystem.SetSystem("flush_graphic", "true");//防止图片频闪
zoom_end_column_ = zoom_begin_column_ + (zoom_end_row_ - zoom_begin_row_) * win_width_ / win_height_;
HalconCpp::SetPart(this->_hv_window_handle,zoom_begin_row_, zoom_begin_column_, zoom_end_row_, zoom_end_column_);
}
this->refreshWin();
}
}
void QHalconWin::mousePressEvent(QMouseEvent *event)
{
// QWidget::mousePressEvent(event);
if(DealWithImage::getStatic()->_exit_image==true
&&_mouse_in_effective_range==true)
{
HalconCpp::HTuple mouse_row_, mouse_col_, button_;
try
{
HalconCpp::GetMpositionSubPix(this->_hv_window_handle, &mouse_row_, &mouse_col_, &button_);
_mouse_down_row = mouse_row_;
_mouse_down_column = mouse_col_;
_mouse_is_down = true;
}
catch (HException)
{
return;
}
}
}
void QHalconWin::mouseMoveEvent(QMouseEvent *event)
{
/***获取当前坐标***/
QPointF point_=event->pos();
qDebug() << QString("x:%1 y:%2").arg(point_.x()).arg(point_.y());
/***判断是不是不在画板里面****/
if(point_.x()<_current_hwin_size._start_column_x
||point_.x()>_current_hwin_size._end_column_x
||point_.y()<_current_hwin_size._start_row_y
||point_.y()>_current_hwin_size._end_row_y)
{
_mouse_in_effective_range=false;
return;
}
_mouse_in_effective_range=true;
qDebug() << QString("in-halcon");
//QWidget::mouseMoveEvent(event);
if(DealWithImage::getStatic()->_exit_image==true)
{
HalconCpp::HTuple start_row_bf_,
start_col_bf_,
end_row_bf_,
end_col_bf_,
mouse_row_,
mouse_col_,
button_;
try
{
/***获取当前鼠标的位置***/
HalconCpp::GetMpositionSubPix(this->_hv_window_handle, &mouse_row_, &mouse_col_, &button_);
switch(_oper_image_enum)
{
case OperImageEnum::_move_image:
if (_mouse_is_down)
{
/***计算移动值***/
double row_move_ = mouse_row_[0].D() - _mouse_down_row[0].D();
double col_move_ = mouse_col_[0].D() - _mouse_down_column[0].D();
/***得到当前的窗口坐标****/
HalconCpp::GetPart(this->_hv_window_handle, &start_row_bf_, &start_col_bf_, &end_row_bf_, &end_col_bf_);
HalconCpp::SetPart(_hv_window_handle, start_row_bf_ - row_move_,
start_col_bf_ - col_move_,
end_row_bf_ - row_move_,
end_col_bf_ - col_move_);
}
break;
case OperImageEnum::_get_pix_position_value:
if(_mouse_is_down)
{
/***显示鼠标坐标***/
QString str_position_="",str_value_="";
bool is_xout_=true,is_yout_=true;
HalconCpp::HTuple channel_count_,img_width_,img_height_;
HalconCpp::CountChannels(DealWithImage::getStatic()->_image,&channel_count_);
HalconCpp::GetImageSize(DealWithImage::getStatic()->_image,&img_width_,&img_height_);
str_position_="x:"+QString::number(mouse_col_.D())+"-y:"+QString::number(mouse_row_.D());
if(mouse_col_<0||mouse_col_>=img_width_)
{
is_xout_=true;
}
else
{
is_xout_=false;
}
if(mouse_row_<0||mouse_row_>=img_height_)
{
is_yout_=true;
}
else
{
is_yout_=false;
}
if(!is_xout_&&!is_yout_)
{
if(channel_count_.I()==1)
{
HalconCpp::HTuple grayval_;
HalconCpp::GetGrayval(DealWithImage::getStatic()->_image,
mouse_row_,
mouse_col_,
&grayval_);
str_value_=QString::number(grayval_.D());
}
else
{
if(channel_count_.I()==3)
{
HalconCpp::HTuple grayval_r_,grayval_g_,grayval_b_;
HalconCpp::HObject channel_r_,channel_g_,channel_b_;
HalconCpp::GenEmptyObj(&channel_r_);
HalconCpp::GenEmptyObj(&channel_g_);
HalconCpp::GenEmptyObj(&channel_b_);
HalconCpp::AccessChannel(DealWithImage::getStatic()->_image,
&channel_r_,
1);
HalconCpp::AccessChannel(DealWithImage::getStatic()->_image,
&channel_g_,
2);
HalconCpp::AccessChannel(DealWithImage::getStatic()->_image,
&channel_b_,
3);
HalconCpp::GetGrayval(channel_r_,mouse_row_,mouse_col_,&grayval_r_);
HalconCpp::GetGrayval(channel_g_,mouse_row_,mouse_col_,&grayval_g_);
HalconCpp::GetGrayval(channel_b_,mouse_row_,mouse_col_,&grayval_b_);
// HalconCpp::ClearObj(channel_r_);
// HalconCpp::ClearObj(channel_g_);
// HalconCpp::ClearObj(channel_b_);
str_value_="-r:"+QString::number(grayval_r_.D())
+"-g:"+QString::number(grayval_g_.D())
+"-b:"+QString::number(grayval_b_.D());
}
}
}
_label_mes->setText(str_position_+str_value_);
}
break;
}
this->refreshWin();
}
catch (HalconCpp::HException& HDevExpDefaultException)
{
return;
}
}
}
void QHalconWin::mouseReleaseEvent(QMouseEvent *event)
{
//QWidget::mouseReleaseEvent(event);
_mouse_is_down = false;
}
void QHalconWin::paintEvent(QPaintEvent *event)
{
// QWidget::paintEvent(event);
if(DealWithImage::getStatic()->_exit_image==true)
{
if (_create_halcon_window==true)
{
/***如果有图像,则先清空图像***/
DetachBackgroundFromWindow(this->_hv_window_handle);
HalconCpp::AttachBackgroundToWindow(DealWithImage::getStatic()->_image,
_hv_window_handle);
}
}
}
void QHalconWin::showEvent(QShowEvent *event)
{
//QWidget::showEvent(event);
if(_show_frist==true)
{
_show_frist=true;
}
else
{
}
}
void QHalconWin::resizeEvent(QResizeEvent *event)
{
//QWidget::resizeEvent(event);
/***判断有没有创建窗口***/
if(this->_create_halcon_window)
{
fitWin();
}
}
7.视频跟源代码地址
添加链接描述