基于Halcon学习的一维码识别【一】25industrial.hdev

读取2/5型工业条码


总代码:

*创建一个条形码阅读器的模型。
*第一个参数为通用参数名字,第二个参数为通用参数的值,第三个为模型的句柄
create_bar_code_model ([], [], BarCodeHandle)

*我们希望对每张图像解码一个条形码
*设置条形码模型的选定参数,stop_after_result_num--设置要解码的条码条数
set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 1)


*有些代码的最小代码长度为1位。因此,我们需要减少此应用程序的默认设置。
*为选定的条形码类型设置条形码模型的选定参数
set_bar_code_param_specific (BarCodeHandle, '2/5 Industrial', 'min_code_length', 1)
*请注意,在实际应用中不建议这样做,因为可能会出现更多错误读取。


*关闭窗口
dev_close_window ()
dev_open_window (0, 0, 120, 300, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)

*循环读入每一幅图片进行识别
for I := 1 to 4 by 1
    *从路径中得到图像
    read_image (Image, 'barcode/25industrial/25industrial0' + I)
    get_image_size (Image, Width, Height)

    *将显示窗口设置与图像等大小
    dev_set_window_extents (0, 0, Width - 1, Height - 1)
    dev_display (Image)
    dev_set_color ('green')

    *读取条形码时,结果字符串包含检查字符
    *设置条形码模型的选定参数。
    *使用 'check_char'且设置为'absent',识别完条形码之后不再检测其正确性即校验位
    set_bar_code_param (BarCodeHandle, 'check_char', 'absent')
    
    
    *寻找条形码并且进行识别,第四个参数可以选择auto,自动确定码制
    *DecodedDataStrings为解码后得到的字符串结果,这个可能是多个字符串,可以看成一个字符串数组
    find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Industrial', DecodedDataStrings)
    
    disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'false')
   *字符个数-1赋给LastChar
    LastChar := strlen(DecodedDataStrings) - 1
    
    *sum(gen_tuple_const(LastChar,' '))意思是形成LastChar个空字符,
    *DecodedDataStrings{LastChar}就是取最后一个字符,1236 索引号0123,取LastChar即是取3

    disp_message (WindowHandle, sum(gen_tuple_const(LastChar,' ')) + DecodedDataStrings{LastChar}, 'window', 12, 12, 'forest green', 'false')
    stop ()

     *使用检查字符读取条形码以检查结果,即检查字符不再属于返回的字符串。
     *如果检查字符不正确,则条形码读取失败
    dev_set_color ('green')
    
    *设置条形码模型,'present'为检定条形码检测的正确性
    set_bar_code_param (BarCodeHandle, 'check_char', 'present')
    *寻找条形码并且进行识别,第四个参数可以选择auto,自动确定码制
    find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Industrial', DecodedDataStrings)
    disp_message (WindowHandle, DecodedDataStrings, 'window', 36, 12, 'black', 'false')
    dev_set_color ('magenta')
    if (I < 4)
        stop ()
    endif
endfor
*清除所建立的条形码模型
clear_bar_code_model (BarCodeHandle)

逐段分析:

*创建一个条形码阅读器的模型。
*第一个参数为通用参数名字,第二个参数为通用参数的值,第三个为模型的句柄
create_bar_code_model ([], [], BarCodeHandle)

*我们希望对每张图像解码一个条形码
*设置条形码模型的选定参数,stop_after_result_num--设置要解码的条码条数
set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 1)


*有些代码的最小代码长度为1位。因此,我们需要减少此应用程序的默认设置。
*为选定的条形码类型设置条形码模型的选定参数
set_bar_code_param_specific (BarCodeHandle, '2/5 Industrial', 'min_code_length', 1)
*请注意,在实际应用中不建议这样做,因为可能会出现更多错误读取。


*关闭窗口
dev_close_window ()
dev_open_window (0, 0, 120, 300, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
*循环读入每一幅图片进行识别
for I := 1 to 4 by 1
    *从路径中得到图像
    read_image (Image, 'barcode/25industrial/25industrial0' + I)
    get_image_size (Image, Width, Height)

    *将显示窗口设置与图像等大小
    dev_set_window_extents (0, 0, Width - 1, Height - 1)
    dev_display (Image)
    dev_set_color ('green')

    *读取条形码时,结果字符串包含检查字符
    *设置条形码模型的选定参数。
    *使用 'check_char'且设置为'absent',识别完条形码之后不再检测其正确性即校验位
    set_bar_code_param (BarCodeHandle, 'check_char', 'absent')

    *寻找条形码并且进行识别,第四个参数可以选择auto,自动确定码制
    *DecodedDataStrings为解码后得到的字符串结果,这个可能是多个字符串,可以看成一个字符串数组
    find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Industrial', DecodedDataStrings)
    
    *显示结果
    disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'false')
 

    *字符个数-1赋给LastChar
    LastChar := strlen(DecodedDataStrings) - 1
    
    *sum(gen_tuple_const(LastChar,' '))意思是形成LastChar个空字符,
    *DecodedDataStrings{LastChar}就是取最后一个字符,1236 索引号0123,取LastChar即是取3

    *显示信息,最后一个字符显示绿色
    disp_message (WindowHandle, sum(gen_tuple_const(LastChar,' ')) + DecodedDataStrings{LastChar}, 'window', 12, 12, 'forest green', 'false')
    stop ()

    *使用检查字符读取条形码以检查结果,即检查字符不再属于返回的字符串。
    *如果检查字符不正确,则条形码读取失败
    dev_set_color ('green')
    
    *设置条形码模型,'present'为检定条形码检测的正确性
    set_bar_code_param (BarCodeHandle, 'check_char', 'present')
    *寻找条形码并且进行识别,第四个参数可以选择auto,自动确定码制
    find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Industrial', DecodedDataStrings)
   
    *只显示前三个字符
    disp_message (WindowHandle, DecodedDataStrings, 'window', 36, 12, 'black', 'false')
    dev_set_color ('magenta')
    if (I < 4)
        stop ()
    endif
endfor
*清除所建立的条形码模型
clear_bar_code_model (BarCodeHandle)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值