26、.NET与Halcon混合编程(2)
Halcon程序:
read_image (Image12, 'C:/12.jpg')
get_image_size (Image12, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image12)
- 导出vb.net代码
1、导出参数(全部默认)
2、导出的代码:
'
' File generated by HDevelop for HALCON/DOTNET (Visual Basic .NET) Version 10.0
'
Option Strict Off
Option Explicit
Imports HalconDotNet
Imports System
Imports Microsoft.VisualBasic
' 1 is not a valid module name.
Module HDevelopExportDefaultModuleName
' Main procedure
Private Sub action()
' Local iconic variables
Dim ho_Image12 As HObject = Nothing
' Local control variables
Dim hv_Width As HTuple = Nothing, hv_Height As HTuple = Nothing
Dim hv_WindowHandle As HTuple = Nothing
' Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(ho_Image12)
ho_Image12.Dispose()
HOperatorSet.ReadImage(ho_Image12, new HTuple("C:/12.jpg"))
HOperatorSet.GetImageSize(ho_Image12, hv_Width, hv_Height)
If (HDevWindowStack.IsOpen()) Then
HOperatorSet.CloseWindow(HDevWindowStack.Pop())
End If
HOperatorSet.SetWindowAttr("background_color",new HTuple("black"))
HOperatorSet.OpenWindow(new HTuple(0),new HTuple(0),hv_Width,hv_Height,0,"","",hv_WindowHandle)
HDevWindowStack.Push(hv_WindowHandle)
If (HDevWindowStack.IsOpen()) Then
HOperatorSet.DispObj(ho_Image12, HDevWindowStack.GetActive())
End If
ho_Image12.Dispose()
End Sub
#If Not NO_EXPORT_APP_MAIN Then
Sub InitHalcon()
' Default settings used in HDevelop
Call HOperatorSet.SetSystem(New HTuple("do_low_error"), New HTuple("false"))
End Sub
Sub Main(ByVal args() As String)
Call InitHalcon()
Call action()
End Sub
#End If
End Modul
- 把导出的代码复制到.net工程
1、根据需要把代码复制到vb.net工程中
(在按钮函数下执行图像处理的action()函数并复制导出的命名空间):
Imports HalconDotNet
Imports System
Imports Microsoft.VisualBasic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
action()
End Sub
Private Sub action() '''''''''''主函数
' Local iconic variables
Dim ho_Image12 As HObject = Nothing
' Local control variables
Dim hv_Width As HTuple = Nothing, hv_Height As HTuple = Nothing
Dim hv_WindowHandle As HTuple = Nothing '定义句柄用于显示图片
' Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(ho_Image12)
ho_Image12.Dispose()
HOperatorSet.ReadImage(ho_Image12, New HTuple("C:/12.jpg"))
HOperatorSet.GetImageSize(ho_Image12, hv_Width, hv_Height)
If (HDevWindowStack.IsOpen()) Then
HOperatorSet.CloseWindow(HDevWindowStack.Pop())
End If
HOperatorSet.SetWindowAttr("background_color", New HTuple("black"))
HOperatorSet.OpenWindow(New HTuple(0), New HTuple(0), hv_Width, hv_Height, 0, "", "", hv_WindowHandle)
HDevWindowStack.Push(hv_WindowHandle)
If (HDevWindowStack.IsOpen()) Then
HOperatorSet.DispObj(ho_Image12, HDevWindowStack.GetActive())
End If
ho_Image12.Dispose()
End Sub
End Class
注:(1)、第三行Imports HalconDotNet刚刚复制时这个命名空间时候编译器时报错的,因为没把
HalconDotNet的dll添加到工程,可以通过右键单击工程---添加引用….----选择路径:
Halcon安装目录下(C:\Program Files\MVTec\HALCON-10.0\bin\dotnet35)把
halcondotnet.dll添加到工程中
(2)、添加dll后,vb.net工程引用情况(选中为新增的):
- 把HWindowXCtrl添加到界面上
1、把HWindowXCtrl添加到工具箱:
打开工具箱-----右键单击----选择项·····
这个控件要在第3步中添加了halcondotnet才会显示的,假如没有显示可以通过浏览按钮到上图鼠标所指的路径中
手动添加。
2、把HWindowXCtrl控件拖拽到界面上:
3、拖拽完控件后,vb.net工程引用情况(选中为新增的):
- 运行程序
效果:
- 改进
从上面的效果看出,这并不是我们想要的结果,因为在程序显示图像的代码为:
HOperatorSet.DispObj(ho_Image12, HDevWindowStack.GetActive())
可以看出,它是把ho_Image12图像显示到由
HOperatorSet.OpenWindow(New HTuple(0), New HTuple(0), hv_Width, hv_Height, 0, "", "", hv_WindowHandle)
HDevWindowStack.Push(hv_WindowHandle)
创建的窗口句柄HDevWindowStack.GetActive()的窗口上。
- 改进方法:把action()函数修改为一下形式
原理:获取HWindowXCtrl的句柄,并通过句柄把图像显示到该控件上。
Private Sub action()
' Local iconic variables
Dim ho_Image12 As HObject = Nothing
' Local control variables
Dim hv_Width As HTuple = Nothing, hv_Height As HTuple = Nothing
Dim hv_WindowHandle As HTuple = Nothing '定义句柄用于显示图片
' Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(ho_Image12)
ho_Image12.Dispose()
HOperatorSet.ReadImage(ho_Image12, New HTuple("C:/12.jpg"))
HOperatorSet.GetImageSize(ho_Image12, hv_Width, hv_Height)
Dim jubing As Integer = CType(AxHWindowXCtrl1.HalconWindow.HalconID, Integer)
Dim jubing_htuple As HTuple = CType(jubing, HTuple)
'使图像适应HWindowXCtrl控件大小显示
HOperatorSet.SetPart(jubing_htuple, 0, 0, hv_Height, hv_Width)
'注意:第三个参数为图像高度,第四个参数为图像宽度
'显示图像
HOperatorSet.DispObj(ho_Image12, 3600)
ho_Image12.Dispose()
End Sub
效果:
- 由于上面操作顺序不同引起的错误分析
上面的操作顺序为:
一、导出vb.net代码
二、把导出的代码复制到.net工程
三、把HWindowXCtrl添加到界面上
四、运行程序
五、改进
假如顺序改为:
一、导出vb.net代码
二、把导出的代码复制到.net工程
三、运行程序
四、把HWindowXCtrl添加到界面上
五、改进
则会产生以下错误:
1、在"三、运行程序"时会提示:
无法加载 DLL"halcon": 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
解决办法:把halcon安装目录(C:\Program Files\MVTec\HALCON-10.0\bin\x86sse2-win32)下的halcon.dll复制到vb.net工程的目录下(…..\bin\Debug,即跟可执行文件.exe同一个文件夹)或复制到C:\Windows\System32路径下。
效果:
2.1、若是通过把halcon.dll复制到vb.net工程的目录下(…..\bin\Debug,即跟可执行文件.exe同一个文件夹)的方法来
修改1中的错误,则在 "五、改进"后运行程序则程序会报错:
HALCON error #5106: No window has been opened for desired action in operator set_part
解决办法:删除vb.net工程的目录下(…..\bin\Debug,即跟可执行文件.exe同一个文件夹)的halcon.dll,因为拖拽
进来的HWindowXCtrl控件已经包括了显示图像所需的库,无再需halcon.dll
2.2、若是通过把halcon.dll复制到C:\Windows\System32路径下的方法来修改1中的错误后,则在 "五、改进"后运行程
序则不会报HALCON error #5106: No window has been opened for desired action in operator set_par错:
- 解决了以上错误之后最终结果就跟之前的就一样了: