这段代码是一个用于识别和显示PDF417条形码的示例程序,它使用了HALCON软件的API。PDF417是一种高密度的条形码,可以存储大量信息。
dev_update_off ()
dev_close_window ()
ImageFiles := ‘datacode/pdf417/pdf417_misc_’
ImageNum := 10
read_image (Image, ImageFiles + ‘01’)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
dev_set_line_width (3)
dev_set_color (‘green’)
*
- Display short description
Message := ‘This simple program demonstrates how to read’
Message[1] := ‘PDF417 data codes in a sequence of images’
Message[2] := ‘with the standard default parameter setting.’
Message[3] := ’ ’
Message[4] := ‘Note that there are symbols that cannot be found’
Message[5] := ‘with the standard default setting. The reason’
Message[6] := 'for that could be: ’
Message[7] := ‘- the contrast is too low’
Message[8] := ‘- the symbol is printed light on dark’
Message[9] := ‘- the symbol is too big (> 48x48 modules)’
Message[10] := ‘- the modules are printed as small unconnected dots’
Message[11] := ’ ’
Message[12] := ‘To find these symbols the model parameters have to’
Message[13] := ‘be adjusted.’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () - Step 1: Create data code model
-
- Create a 2d data code model of the 2d data code class
- ‘PDF417’. The operator returns a handle to the
- 2d data code model which can be used for all further
- operations on the data code.
create_data_code_2d_model (‘PDF417’, [], [], DataCodeHandle) - Step 2: Read the data codes
-
- Search and read the data codes in each image and
- display the decoded string for each found data code
for Index := 1 to ImageNum by 1
read_image (Image, ImageFiles + Index$‘.2d’)
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
*- Settings for visualization of the results
get_image_size (Image, Width, Height)
get_string_extents (WindowHandle, DecodedDataStrings + ’ ', Ascent, Descent, MessageWidth, MessageHeight)
if (MessageWidth > Width)
DecodedDataStrings := DecodedDataStrings{0:50} + ‘…’
endif - Display the results
dev_display (Image)
dev_display (SymbolXLDs)
disp_message (WindowHandle, 'Image ’ + Index + ’ of ’ + ImageNum, ‘window’, 12, 12, ‘black’, ‘true’)
disp_message (WindowHandle, DecodedDataStrings, ‘window’, 40, 12, ‘black’, ‘true’)
Message := ‘No data code found.’
Message[1] := ‘The symbol could not be found with the standard’
Message[2] := ‘default setting. Please adjust the model parameters’
Message[3] := ‘to read this symbol.’ - If no data code could be found
if (|DecodedDataStrings| == 0)
disp_message (WindowHandle, Message, ‘window’, 40, 12, ‘red’, ‘true’)
endif - Deactivate the following lines to run the program without breaks
if (Index < ImageNum)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
- Settings for visualization of the results
- Clear the 2d data code model
clear_data_code_2d_model (DataCodeHandle)
以下是程序运行结果
以下是代码的详细解释:
初始化和设置:
dev_update_off():关闭图像更新,以避免在处理过程中刷新屏幕。
dev_close_window():关闭当前打开的窗口。
ImageFiles 和 ImageNum:定义了图像文件的前缀和要处理的图像数量。
读取第一张图像并打开窗口:
read_image():读取第一张图像。
dev_open_window_fit_image():根据图像大小打开一个窗口。
set_display_font():设置窗口中文本的字体。
dev_set_line_width() 和 dev_set_color():设置线条宽度和颜色。
显示简短描述:
使用 disp_message() 函数在窗口中显示一系列消息,解释程序的功能和可能遇到的问题。
创建数据码模型:
create_data_code_2d_model():创建PDF417类型的2D数据码模型,并获取模型的句柄。
读取数据码:
循环读取每张图像,并使用 find_data_code_2d() 函数搜索并读取数据码。
如果找到数据码,使用 dev_display() 显示图像和数据码的可视化结果。
如果没有找到数据码,显示一条消息提示调整模型参数。
结果可视化:
根据解码字符串的长度调整显示的文本,如果太长则截断。
使用 disp_message() 显示图像编号和解码的数据字符串。
处理未找到数据码的情况:
如果没有找到数据码,显示一条消息提示可能的原因,并建议调整模型参数。
循环控制:
如果还有更多的图像要处理,使用 disp_continue_message() 和 stop() 暂停程序,等待用户操作。
清除数据码模型:
clear_data_code_2d_model():在处理完所有图像后,清除数据码模型。
这段代码是一个典型的HALCON脚本,用于演示如何使用HALCON软件进行图像处理和条形码识别。代码中包含了注释,解释了每一步的作用,使得其他开发者或用户能够更容易地理解和修改这段代码。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/4620.html