// HALCON/Qt pattern matching and measure example
//
// ?2004-2007 MVTec Software GmbH
// QHalconWindow.cpp : Implementation of the class QHalconWindow
//
#include "HalconCpp.h"
#include "iptsmt_dataType.h"
#include "qhalconwindow.h"
#include <QMouseEvent>
#include <QCursor>
#include <QMessageBox>
#include <qmath.h>
void MyHalconExceptionHandle(const Halcon::HException &except)
{
throw except;
}
QHalconWindow::QHalconWindow(QWidget *parent, long Width, long Height)
: QWidget(parent)
{
using namespace Halcon;
resize(Width,Height);
show();
OpenWindow();
// RectHeight = 80;
// RectWidth = 80;
setMouseTracking(TRUE);
dragBegin = FALSE;
rotateBegin = FALSE;
rotateIsAdmit = TRUE;
dragMod = 0;
isInUpBand = FALSE;
isInDnBand = FALSE;
isInLtBand = FALSE;
isInRtBand = FALSE;
isDispCompCam = 0;
FindStep = 4;
isFindMark = 0;
DispRes = 0;
/*将在初始化后重新加载*/
PCBCamrExposure = 666;
PCBCamrGain = 5;
CompCamrExposure = 10000;
CompCamrGain = 10;
minGray_set = 100;
HException::InstallHHandler(&MyHalconExceptionHandle);
}
QHalconWindow::~QHalconWindow(void)
{
using namespace Halcon;
try
{
close_window(WindowID());
close_window(WindowIDBuf);
close_window(WindowIDBuf1);
// close_framegrabber(AcqHandle_CompCam);
close_framegrabber(AcqHandle_PCBCam);
close_all_framegrabbers();
}
catch( HException &except)
{
//TRACE(except.message);
QMessageBox message(QMessageBox::NoIcon,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("摄像头打开错误"), QMessageBox::Yes | QMessageBox::No, NULL);
close_all_framegrabbers();
}
}
// Open a HALCON window inside the QHalconWindow widget
void QHalconWindow::OpenWindow(void)
{
using namespace Halcon;
set_window_attr("border_width",0);
set_check("~father");
// Open a HALCON window with dummy width and height
// it will be resized soon
open_window(0,0,100,100,(Hlong)winId(),"visible","",&WinID);
set_check("father");
}
// Resize the HALCON window whenever the QHalconWindow widget is resized
void QHalconWindow::resizeEvent(QResizeEvent*)
{
using namespace Halcon;
// Set the HALCON window to its new size.
set_window_extents(WindowID(),0,0,width(),height());
}
void QHalconWindow::mousePressEvent ( QMouseEvent * event )
{
if (event->button()==Qt::LeftButton)
{
MouseLeftPressXpos = event->x();
MouseLeftPressYpos = event->y();
dragBegin = TRUE;
}
if (event->button()==Qt::MiddleButton)
{
MouseMidPressXpos = event->x();
MouseMidPressYpos = event->y();
rotateBegin = TRUE;
}
QWidget::mousePressEvent(event);
}
void QHalconWindow::mouseReleaseEvent ( QMouseEvent * event )
{
if (event->button()==Qt::LeftButton)
{
dragBegin = FALSE;
emit VisionMove_sgn(0,'s',0);
emit VisionMove_sgn(1,'s',0);
}
if (event->button()==Qt::MidButton)
{
rotateBegin = FALSE;
}
QWidget::mouseReleaseEvent(event);
}
void QHalconWindow::mouseMoveEvent ( QMouseEvent * event )
{
double d1,d2,d3,d4;
//取得当前鼠标坐标
int x = event->x();
int y = event->y();
//计算当前中心绝对坐标
double CentreX = CenterW - OffsetX;
double CentreY = CenterH - OffsetY;
//计算当前鼠标相对中心的的相对坐标,这里存在Y轴反向,为方便计算。
double RefToCentre_x_now = (double)(x - CentreX);
double RefToCentre_y_now = (double)(CentreY - y);
//计算鼠标左键按下的相对中心直角坐标,存在Y轴反向,为方便计算。
double RefToCentre_x_prior = (double)(MouseLeftPressXpos - CentreX);
double RefToCentre_y_prior = (double)(CentreY - MouseLeftPressYpos);
//实际当前鼠标相对中心的极坐标
double polar_R_now = sqrt((RefToCentre_x_now*RefToCentre_x_now) + (RefToCentre_y_now*RefToCentre_y_now));
double polar_A_now = atan(RefToCentre_y_now/RefToCentre_x_now);
//为了计算方便,计算距离采用虚拟将矩形转成0度,那么鼠标位置也需要转过响应的度数
polar_A_now = polar_A_now + CalRect.a/180*QHALCONW_PI;
//polar_A_prior = polar_A_prior + CalRect.a/180*QHALCONW_PI;
//计算到各边的距离从标记角的顺时针方向分别为
d1 = polar_R_now*sin(polar_A_now) - CalRect.d/2;
d2 = polar_R_now*cos(polar_A_now) - CalRect.w/2;
d3 = polar_R_now*sin(polar_A_now) + CalRect.d/2;
d4 = polar_R_now*cos(polar_A_now) + CalRect.w/2;
if (dragBegin == FALSE && rotateBegin == FALSE)
{
if (abs(d1)<REGION_DIS && d2<-REGION_DIS && d4>REGION_DIS)
{
isInUpBand = TRUE;
}else
{
isInUpBand = FALSE;
}
if (abs(d2)<REGION_DIS && d1<-REGION_DIS && d3>REGION_DIS)
{
isInRtBand = TRUE;
}else
{
isInRtBand = FALSE;
}
if (abs(d3)<REGION_DIS && d2<-REGION_DIS && d4>REGION_DIS)
{
isInDnBand = TRUE;
}else
{
isInDnBand = FALSE;
}
if (abs(d4)<REGION_DIS && d1<-REGION_DIS && d3>REGION_DIS)
{
isInLtBand = TRUE;
}else
{
isInLtBand = FALSE;
}
if (isInUpBand)
{
dragMod = 1;
setCursor(Qt::SizeAllCursor) ;
}else if (isInDnBand)
{
dragMod = 3;
setCursor(Qt::SizeAllCursor) ; ;
}else if (isInRtBand)
{
dragMod = 2;
setCursor(Qt::SizeAllCursor) ;
}else if (isInLtBand)
{
dragMod = 4;
setCursor(Qt::SizeAllCursor) ;
}else
{
dragMod = 0;
setCursor(Qt::ArrowCursor);
}
}
int thresholdValue = 20;
if (dragBegin == TRUE)
{
switch(dragMod)
{
case 0:
if (x-MouseMidPressXpos>thresholdValue)
{
emit VisionMove_sgn(0,'r',0);
}else if (MouseMidPressXpos-x>thresholdValue)
{
emit VisionMove_sgn(0,'r',1);
}else;
if (y-MouseLeftPressYpos>thresholdValue)
{
emit VisionMove_sgn(1,'r',0);
}else if (MouseLeftPressYpos-y>thresholdValue)
{
emit VisionMove_sgn(1,'r',1);
}else;
break;
case 1:
if (CalRect.d/2+d1>REGION_DIS)
{
CalRect.d = CalRect.d + d1*2;
}
break;
case 2:
if (CalRect.w/2+d2>REGION_DIS)
{
CalRect.w = CalRect.w + d2*2;
}
break;
case 3:
if (-CalRect.d/2+d3<-REGION_DIS)
{
CalRect.d = CalRect.d - d3*2;
}
break;
case 4:
if (-CalRect.w/2+d4<-REGION_DIS)
{
CalRect.w = CalRect.w - d4*2;
}
break;
default:break;
}
if (CalRect.w > 450)
{
CalRect.w = 450;
}
if (CalRect.d > 450)
{
CalRect.d = 450;
}
}
if (rotateBegin == true)
{
double a;
if (RefToCentre_x_now == 0 && (MouseMidPressXpos-CentreX) == 0)
{
a = 0;
}else if (RefToCentre_x_now != 0 && (MouseMidPressXpos-CentreX) == 0)
{
a = atan((double)(RefToCentre_y_now/RefToCentre_x_now)) - QHALCONW_PI/2;
if (RefToCentre_x_now<0)
{
a = a + QHALCONW_PI;
}
}else if (RefToCentre_x_now == 0 && (MouseMidPressXpos-CentreX) != 0)
{
a = QHALCONW_PI/2 - atan((double)((CentreY -MouseMidPressYpos)/(MouseMidPressXpos-CentreX)));
if ( (MouseMidPressXpos-CentreX)<0 )
{
a = QHALCONW_PI - a;
}
}else if (RefToCentre_x_now != 0 && (MouseMidPressXpos-CentreX) != 0)
{
a = atan((double)(RefToCentre_y_now/RefToCentre_x_now))-atan((double)((CentreY -MouseMidPressYpos)/(MouseMidPressXpos-CentreX)));
if ((MouseMidPressXpos-CentreX )*(RefToCentre_x_now)<=0)
{
a = a+QHALCONW_PI;
}
}else;
CalRect.a = CalRect.a - (a/QHALCONW_PI*180);
if (CalRect.a>180)
{
CalRect.a = CalRect.a -360;
}else if (CalRect.a <=-180)
{
CalRect.a = CalRect.a +360;
}else;
if (a != 0)
{
emit ChangeRectAng(CalRect.a);
}
MouseMidPressXpos = x;
MouseMidPressYpos = y;
}
QWidget::mouseMoveEvent(event);
}
void QHalconWindow::InitVisionDev(void)
{
using namespace Halcon;
HTuple ParaValue,ParaValueSet;
//release_all_compute_devices();
//关闭所有摄像设备
//close_all_framegrabbers();
//close_all_framegrabbers();
//InstallHHandler(&MyHalconExceptionHandle);
try
{
////分别打开PCB相机及元件相机,并设置参数
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
QHalconwindow.zip (2个子文件)
qhalconwindow.h 4KB
qhalconwindow.cpp 31KB
共 2 条
- 1
资源评论
- rome122016-11-26halcon自带的Qt例程,但是原例程里有一堆东西看不明白,这个Halcon窗口类博主调通了吗?
- kevin_20092018-08-27可以,但是没用上,是我把问题考虑复杂了
loulouu111
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功