您的位置:首页 > 其它

Halcon异常处理 get_mposition算子异常

2014-07-10 14:34 3923 查看
Halcon10.0 以后,当鼠标离开窗口时,get_mposition算子将异常,需加异常捕获方能正常运行。

Halcon的异常处理方式转自:http://k594081130.blog.163.com/blog/static/21835901320141217371386/

第一种、面向对象的方法( Error handling using HException and try/catch)

在C:\Users\Public\Documents\MVTec\HALCON-10.0\examples\cpp\source可以找到example_errorhandling.cpp

首先声明

// exception handler

void MyHalconExceptionHandler(const Halcon::HException& except)

{

throw except;

}

然后定义一个函数

Herror TryReadImage(Halcon::HImage &image, const char *filename)

{

using namespace Halcon;

Herror error_num;

try

{

cout << endl << "Trying to read '" << filename << "' ... ";

image = HImage::ReadImage(filename);

}

catch (HException &except)

{

error_num = except.err;

cout << endl << except.message << endl;

return error_num;

}

// success

cout << "succeeded!" << endl;

return H_MSG_TRUE;

}

注意在调用的过程中首先要exception“句柄”:

// install the exception handler

HException::InstallHHandler(&MyHalconExceptionHandler);

最后调用TryReadImage

第二种、程序法

在例程(Ctrl+E)——应用范围——一般——搜索“error”有一个dev_set_check的例子如下:

// Local iconic variables

Hobject Image;

// Local control variables

Herror Error;

HTuple WindowHandle, FileName, ReadError;

if (HDevWindowStack::IsOpen())

close_window(HDevWindowStack::Pop());

set_window_attr("background_color","black");

open_window(0,0,512,512,0,"","",&WindowHandle);

HDevWindowStack::Push(WindowHandle);

// Error variable 'Error' activated

set_check("~give_error");

FileName = "wrong_name";

Error = read_image(&Image, FileName);

ReadError = (int)Error;

set_check("give_error");

if (0 != (ReadError!=2))

{

Error = write_string(WindowHandle, "wrong file name: "+FileName);

}

//Now the program will stop with an exception(注意 Error = read_image(&Image, FileName);要在set_check前面才有效。)

Error = read_image(&Image, FileName);

//Please note that some tuple operations have no return value. Then, the described approach leads to a

memory leak. Please use the object-oriented approach instead.

//请注意,某些元组操作没有返回值。然后,所描述的方法导致一个内存泄漏。请用面向对象的方法来代替。

举一个实时显示图像窗口中鼠标所在位置的像素亮度的例子

read_image (Image, 'fabrik')

while (1)

dev_set_check ('~give_error')

try

dev_display (Image)

get_mposition (3600, Row, Column, Button)

*在halcon10.0后当鼠标离开图像窗口后,get_mposition算子出现异常。必须要有异常处理才能运行正常!

get_grayval (Image, Row, Column, Grayval)

set_tposition (3600, 24, 24)

write_string (3600, 'Row='+Row)

set_tposition (3600, 64, 24)

write_string (3600, 'Column =' +Column)

set_tposition (3600, 104, 24)

write_string (3600, 'Grayval =' +Grayval)

catch (Exception)

set_tposition (3600, 24, 24)

write_string (3600, Exception)

endtry

dev_set_check ('give_error')

wait_seconds (0.1)

endwhile
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: