halcon+qt开发显示图片界面软件

13 篇文章 0 订阅

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,
                           &current_begin_row_,
                           &current_begin_column_,
                           &current_end_row_,
                           &current_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.视频跟源代码地址
添加链接描述

### 回答1: VS (Visual Studio) 和 QT(HALCON) 是两种不同的软件开发工具。 VS 是微软开发的集成开发环境,主要用于开发 Windows 操作系统和应用程序。它提供了一系列的工具和组件,例如代码编辑器、调试器、图形化界面设计工具等,使开发者能够方便地进行软件开发和调试。VS支持多种编程语言,如C++、C#等,可以满足不同开发需求。 QT(HALCON) 是一种跨平台的开发框架,主要用于开发图形化界面和多媒体应用。它提供了丰富的工具和组件,支持多种操作系统,如Windows、Linux、macOS等,使开发者能够轻松地开发不同平台的应用程序。QT还提供了Qt Creator集成开发环境,方便开发者进行代码编写和调试。 VS 和 QT 在应用开发方面有各自的优势。VS 在 Windows 平台上的开发经验丰富,支持多种编程语言,并提供了强大的调试功能,适用于开发各类 Windows 应用程序。而QT则提供了一整套的跨平台解决方案,开发者可以使用相同的代码在不同平台上进行开发,避免了重复编写代码的繁琐工作。 总而言之,VS 适用于主要在 Windows 平台进行开发开发者,而QT 则适用于需要在多个平台上开发应用程序的开发者。开发者可以根据自己的需求选择适合的开发工具来进行应用开发。 ### 回答2: VS和Qt是两种不同的开发工具,分别用于不同的应用领域。 VS是微软开发的一款集成开发环境(IDE),主要用于Windows平台上的软件开发。它提供了丰富的功能和工具,可以用于开发各种类型的应用程序,包括桌面应用、Web应用、移动应用等。VS具有良好的集成能力,可以与其他微软产品(如.NET、Azure等)无缝连接,同时也支持多种编程语言(如C++、C#等)。 Qt是一种跨平台的应用程序开发框架,主要用于开发图形界面应用。它提供了丰富的UI组件和类库,可以简化用户界面开发的工作。Qt支持多种编程语言,包括C++、Python等,可以运行在多个操作系统上,如Windows、Linux、Mac等。Qt还提供了一系列的工具和功能,用于处理图像、网络、数据库等方面的开发Halcon是一种专业的机器视觉软件库,主要用于开发机器视觉应用。它提供了丰富的图像处理和分析算法,可以用于解决各种视觉相关的问题。Halcon具有良好的性能和稳定性,可以运行在多个平台上,并且支持多种编程语言Halcon在工业自动化、品质控制、医学影像等领域得到广泛应用。 总结来说,VS适用于开发各种类型的应用程序,Qt适用于开发图形界面应用,而Halcon适用于开发机器视觉应用。在具体选择时,需要根据实际需求和开发环境来决定使用哪个工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值