读取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')
![](https://img-blog.csdnimg.cn/840308924a764d728b4377ada94c0a45.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_3,color_FFFFFF,t_70,g_se,x_16)
*寻找条形码并且进行识别,第四个参数可以选择auto,自动确定码制
*DecodedDataStrings为解码后得到的字符串结果,这个可能是多个字符串,可以看成一个字符串数组
find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Industrial', DecodedDataStrings)
*显示结果
disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'false')
![](https://img-blog.csdnimg.cn/37f118a5d59146fa859073f2e7c4ccf4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_3,color_FFFFFF,t_70,g_se,x_16)
*字符个数-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 ()
![](https://img-blog.csdnimg.cn/69e88585a44a47ca82894cee4d49db92.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_3,color_FFFFFF,t_70,g_se,x_16)
*使用检查字符读取条形码以检查结果,即检查字符不再属于返回的字符串。
*如果检查字符不正确,则条形码读取失败
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)
![](https://img-blog.csdnimg.cn/8581419921f644eca2c1058cca745f41.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_3,color_FFFFFF,t_70,g_se,x_16)