halcon 导出C++程序和visualstdio2019配置环境,在控制台显示一张图片
1.halcon代码
-
read_image (DSC_0097, 'C:/Users/Administrator/Desktop/DSC_0097.JPG')
-
dev_open_window_fit_image(DSC_0097,0,0,-1,-1,WindowHandle)
-
dev_clear_window()
-
dev_display(DSC_0097)
2.选择文件,导出
HALCON/C
-
保存到目标程序
-
-
///
-
// File generated by HDevelop for HALCON/C Version 17.12
-
///
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
using namespace HalconCpp;
-
-
// Procedure declarations
-
// Chapter: Develop
-
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
-
void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
-
HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle);
-
-
// Procedures
-
// Chapter: Develop
-
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
-
void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
-
HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle)
-
{
-
-
// Local iconic variables
-
-
// Local control variables
-
HTuple hv_MinWidth, hv_MaxWidth, hv_MinHeight;
-
HTuple hv_MaxHeight, hv_ResizeFactor, hv_ImageWidth, hv_ImageHeight;
-
HTuple hv_TempWidth, hv_TempHeight, hv_WindowWidth, hv_WindowHeight;
-
-
//This procedure opens a new graphics window and adjusts the size
-
//such that it fits into the limits specified by WidthLimit
-
//and HeightLimit, but also maintains the correct image aspect ratio.
-
//
-
//If it is impossible to match the minimum and maximum extent requirements
-
//at the same time (f.e. if the image is very long but narrow),
-
//the maximum value gets a higher priority,
-
//
-
//Parse input tuple WidthLimit
-
if (0 != (HTuple((hv_WidthLimit.TupleLength())==0).TupleOr(hv_WidthLimit<0)))
-
{
-
hv_MinWidth = 500;
-
hv_MaxWidth = 800;
-
}
-
else if (0 != ((hv_WidthLimit.TupleLength())==1))
-
{
-
hv_MinWidth = 0;
-
hv_MaxWidth = hv_WidthLimit;
-
}
-
else
-
{
-
hv_MinWidth = ((const HTuple&)hv_WidthLimit)[0];
-
hv_MaxWidth = ((const HTuple&)hv_WidthLimit)[1];
-
}
-
//Parse input tuple HeightLimit
-
if (0 != (HTuple((hv_HeightLimit.TupleLength())==0).TupleOr(hv_HeightLimit<0)))
-
{
-
hv_MinHeight = 400;
-
hv_MaxHeight = 600;
-
}
-
else if (0 != ((hv_HeightLimit.TupleLength())==1))
-
{
-
hv_MinHeight = 0;
-
hv_MaxHeight = hv_HeightLimit;
-
}
-
else
-
{
-
hv_MinHeight = ((const HTuple&)hv_HeightLimit)[0];
-
hv_MaxHeight = ((const HTuple&)hv_HeightLimit)[1];
-
}
-
//
-
//Test, if window size has to be changed.
-
hv_ResizeFactor = 1;
-
GetImageSize(ho_Image, &hv_ImageWidth, &hv_ImageHeight);
-
//First, expand window to the minimum extents (if necessary).
-
if (0 != (HTuple(hv_MinWidth>hv_ImageWidth).TupleOr(hv_MinHeight>hv_ImageHeight)))
-
{
-
hv_ResizeFactor = (((hv_MinWidth.TupleReal())/hv_ImageWidth).TupleConcat((hv_MinHeight.TupleReal())/hv_ImageHeight)).TupleMax();
-
}
-
hv_TempWidth = hv_ImageWidth*hv_ResizeFactor;
-
hv_TempHeight = hv_ImageHeight*hv_ResizeFactor;
-
//Then, shrink window to maximum extents (if necessary).
-
if (0 != (HTuple(hv_MaxWidth<hv_TempWidth).TupleOr(hv_MaxHeight<hv_TempHeight)))
-
{
-
hv_ResizeFactor = hv_ResizeFactor*((((hv_MaxWidth.TupleReal())/hv_TempWidth).TupleConcat((hv_MaxHeight.TupleReal())/hv_TempHeight)).TupleMin());
-
}
-
hv_WindowWidth = hv_ImageWidth*hv_ResizeFactor;
-
hv_WindowHeight = hv_ImageHeight*hv_ResizeFactor;
-
//Resize window
-
SetWindowAttr("background_color","black");
-
OpenWindow(hv_Row,hv_Column,hv_WindowWidth,hv_WindowHeight,0,"visible","",&(*hv_WindowHandle));
-
HDevWindowStack::Push((*hv_WindowHandle));
-
if (HDevWindowStack::IsOpen())
-
SetPart(HDevWindowStack::GetActive(),0, 0, hv_ImageHeight-1, hv_ImageWidth-1);
-
return;
-
}
-
-
-
// Main procedure
-
void action()
-
{
-
-
// Local iconic variables
-
HObject ho_Img5131;
-
-
// Local control variables
-
HTuple hv_WindowHandle;
-
-
ReadImage(&ho_Img5131, "C:/Users/Administrator/Desktop/DSC_0097.JPG");
-
dev_open_window_fit_image(ho_Img5131, 0, 0, -1, -1, &hv_WindowHandle);
-
if (HDevWindowStack::IsOpen())
-
ClearWindow(HDevWindowStack::GetActive());
-
if (HDevWindowStack::IsOpen())
-
DispObj(ho_Img5131, HDevWindowStack::GetActive());
-
}
-
-
-
-
-
-
// On OS X systems, we must have a CFRunLoop running on the main thread in
-
// order for the HALCON graphics operators to work correctly, and run the
-
// action function in a separate thread. A CFRunLoopTimer is used to make sure
-
// the action function is not called before the CFRunLoop is running.
-
// Note that starting with macOS 10.12, the run loop may be stopped when a
-
// window is closed, so we need to put the call to CFRunLoopRun() into a loop
-
// of its own.
-
HTuple gStartMutex;
-
H_pthread_t gActionThread;
-
HBOOL gTerminate = FALSE;
-
-
static void timer_callback(CFRunLoopTimerRef timer, void *info)
-
{
-
UnlockMutex(gStartMutex);
-
}
-
-
static Herror apple_action(void **parameters)
-
{
-
// Wait until the timer has fired to start processing.
-
LockMutex(gStartMutex);
-
UnlockMutex(gStartMutex);
-
-
try
-
{
-
action();
-
}
-
catch (HException &exception)
-
{
-
fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),
-
(const char *)exception.ProcName(),
-
(const char *)exception.ErrorMessage());
-
}
-
-
// Tell the main thread to terminate itself.
-
LockMutex(gStartMutex);
-
gTerminate = TRUE;
-
UnlockMutex(gStartMutex);
-
CFRunLoopStop(CFRunLoopGetMain());
-
return H_MSG_OK;
-
}
-
-
static int apple_main(int argc, char *argv[])
-
{
-
Herror error;
-
CFRunLoopTimerRef Timer;
-
CFRunLoopTimerContext TimerContext = { 0, 0, 0, 0, 0 };
-
-
CreateMutex("type","sleep",&gStartMutex);
-
LockMutex(gStartMutex);
-
-
error = HpThreadHandleAlloc(&gActionThread);
-
if (H_MSG_OK != error)
-
{
-
fprintf(stderr,"HpThreadHandleAlloc failed: %d\n", error);
-
exit(1);
-
}
-
-
error = HpThreadCreate(gActionThread,0,apple_action);
-
if (H_MSG_OK != error)
-
{
-
fprintf(stderr,"HpThreadCreate failed: %d\n", error);
-
exit(1);
-
}
-
-
Timer = CFRunLoopTimerCreate(kCFAllocatorDefault,
-
CFAbsoluteTimeGetCurrent(),0,0,0,
-
timer_callback,&TimerContext);
-
if (!Timer)
-
{
-
fprintf(stderr,"CFRunLoopTimerCreate failed\n");
-
exit(1);
-
}
-
CFRunLoopAddTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);
-
-
for (;;)
-
{
-
HBOOL terminate;
-
-
CFRunLoopRun();
-
-
LockMutex(gStartMutex);
-
terminate = gTerminate;
-
UnlockMutex(gStartMutex);
-
-
if (terminate)
-
break;
-
}
-
-
CFRunLoopRemoveTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);
-
CFRelease(Timer);
-
-
error = HpThreadHandleFree(gActionThread);
-
if (H_MSG_OK != error)
-
{
-
fprintf(stderr,"HpThreadHandleFree failed: %d\n", error);
-
exit(1);
-
}
-
-
ClearMutex(gStartMutex);
-
return 0;
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
int ret = 0;
-
-
try
-
{
-
-
SetSystem("use_window_thread", "true");
-
-
XInitThreads();
-
-
-
// Default settings used in HDevelop (can be omitted)
-
SetSystem("width", 512);
-
SetSystem("height", 512);
-
-
-
action();
-
-
ret = apple_main(argc,argv);
-
-
}
-
catch (HException &exception)
-
{
-
fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),
-
(const char *)exception.ProcName(),
-
(const char *)exception.ErrorMessage());
-
ret = 1;
-
}
-
return ret;
-
}
-
-
-
-
-
其中action和void dev_open_window_fit_image 中的内容是我们想要的
新建一个visualstdio2019控制台工程
将dev_open_window_fit_image函数复制到工程中action()函数中的内容复制到main函数中
3 配置vs2019环境
项目 属性
VC 目录
包含目录增加
C:\Program Files\MVTec\HALCON-17.12-Progress\include
C:\Program Files\MVTec\HALCON-17.12-Progress\include\halconcpp
库目录中选择C:\Program Files\MVTec\HALCON-17.12-Progress\lib\x64-win64
c/c 常规附加包含目录中添加
$(HALCONROOT)/include
$(HALCONROOT)/include/halconcpp
链接器,附加库目录添加C:\Program Files\MVTec\HALCON-17.12-Progress\lib\x64-win64
输入附加依赖项 添加 halconcpp.lib
点击应用,环境就配置好了
4运行和解决黑屏的问题
-
using namespace HalconCpp;
-
-
// Procedure declarations
-
// Chapter: Develop
-
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
-
void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
-
HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle);
-
-
// Procedures
-
// Chapter: Develop
-
// Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
-
void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
-
HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle)
-
{
-
-
// Local iconic variables
-
-
// Local control variables
-
HTuple hv_MinWidth, hv_MaxWidth, hv_MinHeight;
-
HTuple hv_MaxHeight, hv_ResizeFactor, hv_ImageWidth, hv_ImageHeight;
-
HTuple hv_TempWidth, hv_TempHeight, hv_WindowWidth, hv_WindowHeight;
-
-
//This procedure opens a new graphics window and adjusts the size
-
//such that it fits into the limits specified by WidthLimit
-
//and HeightLimit, but also maintains the correct image aspect ratio.
-
//
-
//If it is impossible to match the minimum and maximum extent requirements
-
//at the same time (f.e. if the image is very long but narrow),
-
//the maximum value gets a higher priority,
-
//
-
//Parse input tuple WidthLimit
-
if (0 != (HTuple((hv_WidthLimit.TupleLength())==0).TupleOr(hv_WidthLimit<0)))
-
{
-
hv_MinWidth = 500;
-
hv_MaxWidth = 800;
-
}
-
else if (0 != ((hv_WidthLimit.TupleLength())==1))
-
{
-
hv_MinWidth = 0;
-
hv_MaxWidth = hv_WidthLimit;
-
}
-
else
-
{
-
hv_MinWidth = ((const HTuple&)hv_WidthLimit)[0];
-
hv_MaxWidth = ((const HTuple&)hv_WidthLimit)[1];
-
}
-
//Parse input tuple HeightLimit
-
if (0 != (HTuple((hv_HeightLimit.TupleLength())==0).TupleOr(hv_HeightLimit<0)))
-
{
-
hv_MinHeight = 400;
-
hv_MaxHeight = 600;
-
}
-
else if (0 != ((hv_HeightLimit.TupleLength())==1))
-
{
-
hv_MinHeight = 0;
-
hv_MaxHeight = hv_HeightLimit;
-
}
-
else
-
{
-
hv_MinHeight = ((const HTuple&)hv_HeightLimit)[0];
-
hv_MaxHeight = ((const HTuple&)hv_HeightLimit)[1];
-
}
-
//
-
//Test, if window size has to be changed.
-
hv_ResizeFactor = 1;
-
GetImageSize(ho_Image, &hv_ImageWidth, &hv_ImageHeight);
-
//First, expand window to the minimum extents (if necessary).
-
if (0 != (HTuple(hv_MinWidth>hv_ImageWidth).TupleOr(hv_MinHeight>hv_ImageHeight)))
-
{
-
hv_ResizeFactor = (((hv_MinWidth.TupleReal())/hv_ImageWidth).TupleConcat((hv_MinHeight.TupleReal())/hv_ImageHeight)).TupleMax();
-
}
-
hv_TempWidth = hv_ImageWidth*hv_ResizeFactor;
-
hv_TempHeight = hv_ImageHeight*hv_ResizeFactor;
-
//Then, shrink window to maximum extents (if necessary).
-
if (0 != (HTuple(hv_MaxWidth<hv_TempWidth).TupleOr(hv_MaxHeight<hv_TempHeight)))
-
{
-
hv_ResizeFactor = hv_ResizeFactor*((((hv_MaxWidth.TupleReal())/hv_TempWidth).TupleConcat((hv_MaxHeight.TupleReal())/hv_TempHeight)).TupleMin());
-
}
-
hv_WindowWidth = hv_ImageWidth*hv_ResizeFactor;
-
hv_WindowHeight = hv_ImageHeight*hv_ResizeFactor;
-
//Resize window
-
SetWindowAttr("background_color","black");
-
OpenWindow(hv_Row,hv_Column,hv_WindowWidth,hv_WindowHeight,0,"visible","",&(*hv_WindowHandle));
-
HDevWindowStack::Push((*hv_WindowHandle));
-
if (HDevWindowStack::IsOpen())
-
SetPart(HDevWindowStack::GetActive(),0, 0, hv_ImageHeight-1, hv_ImageWidth-1);
-
return;
-
}
-
-
-
int main()
-
{
-
-
// Local iconic variables
-
HObject ho_Img5131;
-
-
// Local control variables
-
HTuple hv_WindowHandle;
-
-
ReadImage(&ho_Img5131, "C:/Users/Administrator/Desktop/DSC_0097.JPG");
-
dev_open_window_fit_image(ho_Img5131, 0, 0, -1, -1, &hv_WindowHandle);
-
if (HDevWindowStack::IsOpen())
-
ClearWindow(HDevWindowStack::GetActive());
-
if (HDevWindowStack::IsOpen())
-
DispObj(ho_Img5131, HDevWindowStack::GetActive());
-
system("pause");
-
-
return 0;
-
}
这时候我们的代码就成功导入vs2019了,运行一下,有一个问题
界面是黑屏的,找了好久好久,加了一行代码
SetSystem("use_window_thread", "true");
-
int main()
-
{
-
SetSystem("use_window_thread", "true");
-
// Local iconic variables
-
HObject ho_Img5131;
-
-
// Local control variables
-
HTuple hv_WindowHandle;
-
-
ReadImage(&ho_Img5131, "C:/Users/Administrator/Desktop/DSC_0097.JPG");
-
dev_open_window_fit_image(ho_Img5131, 0, 0, -1, -1, &hv_WindowHandle);
-
if (HDevWindowStack::IsOpen())
-
ClearWindow(HDevWindowStack::GetActive());
-
if (HDevWindowStack::IsOpen())
-
DispObj(ho_Img5131, HDevWindowStack::GetActive());
-
system("pause");
-
-
return 0;
-
}
最后运行
成功显示图片
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhiahakf
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22