Halcon - Qt联合Halcon编程之显示图片

01. 概述

QT与Halcon联合编程。将Halcon中代码集成到Qt程序中。

开发环境

Qt:Qt5.14.2

Halcon: Halcon 24.11

 

02. 编写Halcon程序

image

程序示例代码:

* 从本地磁盘读取一张图片
read_image (Carb, 'C:/Users/T480S/Desktop/test_Image/nene.png')

* 获取图片大小
get_image_size (Carb, Width, Height)

* 关闭当前窗口
dev_close_window ()

* 打开新窗口
dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)

* 显示图片
dev_display (Carb)

 

03. Halcon程序导出C++文件

3.1选择文件中导出菜单

image

3.2 设置导出文件和格式,然后点击导出

image

3.3 导出源文件程序如下

test01.cpp源码:

查看代码
///////////////////////////////////////////////////////////////////////////////
// File generated by HDevelop for HALCON/C++ Version 24.11.1.0
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
// Ensure that the interface encoding is set to locale encoding by calling
// SetHcppInterfaceStringEncodingIsUtf8(false) at the beginning of the program.
// 
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
// 
// Source files with different encoding should not be mixed in one project.
///////////////////////////////////////////////////////////////////////////////
#include "HalconCpp.h"
#include "HDevThread.h"



using namespace HalconCpp;


#ifndef NO_EXPORT_MAIN
// Main procedure 
void action()
{

  // Local iconic variables
  HObject  ho_Carb;

  // Local control variables
  HTuple  hv_Width, hv_Height, hv_WindowHandle;

  //从本地磁盘读取一张图片
  ReadImage(&ho_Carb, "C:/Users/T480S/Desktop/test_Image/nene.png");

  //获取图片大小
  GetImageSize(ho_Carb, &hv_Width, &hv_Height);

  //关闭当前窗口
  if (HDevWindowStack::IsOpen())
    CloseWindow(HDevWindowStack::Pop());

  //打开新窗口
  SetWindowAttr("background_color","black");
  OpenWindow(0,0,hv_Width/2,hv_Height/2,0,"visible","",&hv_WindowHandle);
  HDevWindowStack::Push(hv_WindowHandle);

  //显示图片
  if (HDevWindowStack::IsOpen())
    DispObj(ho_Carb, HDevWindowStack::GetActive());


}


#ifndef NO_EXPORT_APP_MAIN


int main(int argc, char *argv[])
{
  int ret = 0;

  try
  {
#if defined(_WIN32)
    SetSystem("use_window_thread", "true");
#endif

    // file was stored with local-8-bit encoding
    //   -> set the interface encoding accordingly
    SetHcppInterfaceStringEncodingIsUtf8(false);

    // Default settings used in HDevelop (can be omitted)
    SetSystem("width", 512);
    SetSystem("height", 512);

    action();
  }
  catch (HException &exception)
  {
    fprintf(stderr,"  Error #%u in %s: %s\n", exception.ErrorCode(),
            exception.ProcName().TextA(),
            exception.ErrorMessage().TextA());
    ret = 1;
  }

#if defined(_WIN32)
  // On Windows socket communication is no longer possible after returning
  // from main, so HALCON cannot return floating licenses automatically.
  try
  {
    SetSystem("return_license", "true");
  }
  catch (...)
  {
    // Ignore any errors that might occur when returning the license.
  }
#endif

  return ret;
}

#endif


#endif

 

04. 创建Qt图形界面项目

 4.1 简单的GUI设计如下

image

 4.2 选择MSVC2017编译器或者其它版本的编译器

image

集成halcon编译需要用msvc,如果用minGW,会出错,等有时间在去研究minGW编译情形。

 

05. Qt集成Halcon程序

5.1 在pro项目配置文件中添加一下Halcon的头文件以及库目录

QT       += core gui

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

macx {
  QMAKE_CXXFLAGS += -F/Library/Frameworks
  QMAKE_LFLAGS   += -F/Library/Frameworks
  LIBS           += -framework HALCONCpp
}
else
{
  #defines
  win32:DEFINES += WIN32

  #includes
  INCLUDEPATH   += "$$(HALCONROOT)/include"
  INCLUDEPATH   += "$$(HALCONROOT)/include/halconcpp"

  #libs
  QMAKE_LIBDIR  += "$$(HALCONROOT)/lib/$$(HALCONARCH)"
  unix:LIBS     += -lhalconcpp -lhalcon -lXext -lX11 -ldl -lpthread
  win32:LIBS    += "$$(HALCONROOT)/lib/$$(HALCONARCH)/halconcpp.lib" \
                   "$$(HALCONROOT)/lib/$$(HALCONARCH)/halcon.lib"
}

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 

5.2 在mainwindow.h文件中添加Halcon相关头文件

mainwindow.h代码:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include "HalconCpp.h"
#include "HDevThread.h"

using namespace HalconCpp;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;

    // Local iconic variables
    HObject  ho_Carb;

    // Local control variables
    HTuple  hv_Width, hv_Height, hv_WindowHandle;
};
#endif // MAINWINDOW_H

 

5.3 在MainWindow添加halcon相关的变量

mainwindow.h代码:

#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_clicked()
{
    //从本地磁盘读取一张图片
    ReadImage(&ho_Carb, "C:/Users/T480S/Desktop/test_Image/nene.png");

    //获取图片大小
    GetImageSize(ho_Carb, &hv_Width, &hv_Height);

    //关闭当前窗口
    if (HDevWindowStack::IsOpen())
        CloseWindow(HDevWindowStack::Pop());

    //打开新窗口
    SetWindowAttr("background_color","black");
    //Halcon图像窗口与QT窗口是独立的窗口
    //OpenWindow(0,0,hv_Width/2,hv_Height/2,0,"visible","",&hv_WindowHandle);

    //获取Label空间的WinId
    Hlong winId = (Hlong)ui->label->winId();
    //将Label和Halcon窗口进行绑定
    OpenWindow(0,0,hv_Width/2,hv_Height/2,winId,"visible","",&hv_WindowHandle);
    HDevWindowStack::Push(hv_WindowHandle);

    //显示图片
    if (HDevWindowStack::IsOpen())
    {
        DispObj(ho_Carb, HDevWindowStack::GetActive());
    }
}

 

 5.4 编译运行,执行结果

image

 

posted @ 2026-08-15 20:08  [BORUTO]  阅读(8)  评论(0)    收藏  举报