1. 程式人生 > >在MFC中用halcon讀影象,顯示在主視窗和picture control中

在MFC中用halcon讀影象,顯示在主視窗和picture control中

1.建立對話方塊工程(字符集:多位元組字符集)
2.配置屬性:
(1)配置屬性-》c++-》常規-》附加庫目錄,加入:
D:\Program Files\MVTec\HALCON-12.0\include\halconcpp;
D:\Program Files\MVTec\HALCON-12.0\include;
(2)配置屬性-》連結器-》常規-》附加庫目錄,加入:
D:\Program Files\MVTec\HALCON-12.0\lib\x64-win64;
(3)配置屬性-》連結器-》輸入-》附加依賴項,加入:
halconcpp.lib
3.Halcon呼叫,需在Dlg.h或呼叫檔案中加入

include”HalconCpp.h”

using namespace HalconCpp;
4.
(1)在主視窗中顯示
在對話方塊中新增按鈕,並新增響應函式(Dlg.cpp),函式如下:
char* pcsun = “圖片檔案(.bmp .png .jpg)|.bmp;.png;*jpg|All Files (.)|.*||”;
CFileDialog OpenDialog(TRUE, NULL, 0, OFN_OVERWRITEPROMPT, pcsun, NULL);
if (OpenDialog.DoModal() == IDCANCEL) return;
//返回帶檔名的路徑,並傳遞給編輯控制元件變數
CString str = OpenDialog.GetPathName();
char* ch = (char*)LPCTSTR(str);
HObject m_hoImage;
ReadImage(&m_hoImage, ch);
HTuple hv_WindowHandle;
HTuple hv_Width, hv_Height;
GetImageSize(m_hoImage, &hv_Height, &hv_Width);
Hlong MainWndID = (Hlong)m_hWnd;
OpenWindow(0, 0, hv_Height, hv_Width , MainWndID, “”, “”, &hv_WindowHandle);
SetPart(m_htWindow, 0, 0, width, height);//
DispObj(m_hoImage, m_htWindow);    
主視窗顯示


(2)picture control裡面顯示,也是在讀圖按鈕裡面新增響應函式程式碼:
char* pcsun = “圖片檔案(.bmp .png .jpg)|.bmp;.png;*jpg|All Files (.)|.*||”;
CFileDialog OpenDialog(TRUE, NULL, 0, OFN_OVERWRITEPROMPT, pcsun, NULL);
if (OpenDialog.DoModal() == IDCANCEL) return;
//返回帶檔名的路徑,並傳遞給編輯控制元件變數
CString str = OpenDialog.GetPathName();
char* ch = (char*)LPCTSTR(str);
HObject m_hoImage;
ReadImage(&m_hoImage, ch);
HTuple width, height;
GetImageSize(m_hoImage, &height, &width);
CRect rtWindow;
HWND hImgWnd = GetDlgItem(IDC_BMPWINDOW)->m_hWnd;
GetDlgItem(IDC_BMPWINDOW)->GetClientRect(&rtWindow);
HTuple m_htWindow;
OpenWindow(rtWindow.left, rtWindow.top, rtWindow.Width(), rtWindow.Height(), (Hlong)hImgWnd, “visible”, “”, &m_htWindow);
SetPart(m_htWindow, 0, 0, width, height);//
DispObj(m_hoImage, m_htWindow);  

顯示如圖