使用HALCON条形码阅读器自动识别功能的示例程序。
总代码:
*创建一个条形码阅读器的模型。
*第一个参数为通用参数名字,第二个参数为通用参数的值,第三个为模型的句柄
create_bar_code_model ([], [], BarCodeHandle)
*设置条码读取器参数,是否验证空白区域是否干净
set_bar_code_param (BarCodeHandle, 'quiet_zone', 'true')
*
* Initialization
*设置一些参数
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('forest green')
*
* Define the example images to use
* for part I
*定义要使用的示例图像
*第一部分
*定义每一种条码的样本图像
ExampleImagesAny := ['barcode/ean13/ean1301','barcode/code39/code3901','barcode/gs1databar_omnidir/gs1databar_omnidir_01']
*期望条码数量
ExpectedNumCodes := [1,2,1]
*期望数据
ExpectedData := ['4008535118906','92274A','R74-2001-010','(01)00098431358722']
* for part II
*混合图像
ExampleImagesMixed := ['barcode/mixed/barcode_mixed_04','barcode/mixed/barcode_mixed_03','barcode/mixed/barcode_mixed_01']
*
* PART I
*
* Use autodiscrimination to decode any of the bar code types
* supported by HALCON (except PharmaCode) or determine the bar
* code type of unknown bar codes
* *使用自动识别对任何条形码类型进行解码
*由HALCON(PharmaCode除外)支持或确定未知条形码的条形码类型
for IdxExample := 0 to 1 by 1
* Use different parameters for each run
* First: Search for all bar code types
* Second: Search only expected bar code types
*每次运行使用不同的参数
*第一:搜索所有条形码类型
*第二:只搜索预期的条形码类型
*定义显示信息
Message := 'Usage of autodiscrimination to decode'
Message[1] := 'unknown bar code types.'
Message[2] := ' '
*搜索所有条码类型的参数和显示信息
if (IdxExample == 0)
CodeTypes := 'auto'
Message[3] := 'Part I:'
Message[4] := 'Compare performance between automatic'
Message[5] := 'bar code discrimination and search for'
Message[6] := 'specific code types only.'
Message[7] := ' '
Message[8] := 'In the first run we use CodeType=\'' + CodeTypes + '\' to look'
Message[9] := 'for all bar code types known to HALCON'
Message[10] := ' '
Message[11] := 'Please note that'
Message[12] := ' - Wrong bar codes may be found'
Message[13] := ' - Execution times are longer than necessary'
Color := [gen_tuple_const(|Message| - 2,'black'),gen_tuple_const(3,'red')]
*搜索期望条码类型的参数和显示信息
else
CodeTypes := ['EAN-13','Code 39','GS1 DataBar Omnidir']
Message := 'In the second run we look for the expected types only:'
Message[1] := sum(' ' + CodeTypes)
Message[2] := 'This reduces the decoding time and'
Message[3] := 'increases the decoding reliability.'
Color := gen_tuple_const(|Message|,'black')
endif
dev_clear_window ()
disp_message (WindowHandle, Message, 'window', 12, 12, Color, 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Run the test 2 times with different parameters
*使用不同的参数运行测试2次
for IdxIma := 0 to |ExampleImagesAny| - 1 by 1
FileName := ExampleImagesAny[IdxIma]
read_image (Image, FileName)
*
* Set display defaults
*设置一些默认参数
get_image_size (Image, Width, Height)
dev_set_window_extents (-1, -1, Width, Height)
dev_display (Image)
disp_message (WindowHandle, ['Looking for bar code(s) of type:',sum(' ' + CodeTypes)], 'window', 12, 12, 'black', 'true')
*
* Find and decode bar codes. Measure the time needed.
*查找并解码条形码。测量所需的时间。
*开始第一次计时
count_seconds (Start)
*寻找条形码
find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
*结束计时
count_seconds (Stop)
*计算时间
Duration := (Stop - Start) * 1000
*
* Check, if results are correct
*检查结果是否正确
Start := sum(ExpectedNumCodes[0:IdxIma]) - ExpectedNumCodes[IdxIma]
DataExp := ExpectedData[Start:Start + ExpectedNumCodes[IdxIma] - 1]
WrongIndex := []
*找到的值是否包含在期望的值中
for I := 0 to |DecodedDataStrings| - 1 by 1
tuple_find (DataExp, DecodedDataStrings[I], Indices)
if (Indices == -1)
WrongIndex := [WrongIndex,I]
endif
endfor
*定义一个颜色数组
Color := ['black',gen_tuple_const(|DecodedDataStrings|,'forest green')]
Color[WrongIndex + 1] := 'red'
*
* Display results and the execution time
*显示结果和执行时间
for I := 1 to |DecodedDataStrings| by 1
*选择条码区域
select_obj (SymbolRegions, ObjectSelected, I)
dev_set_color (Color[I])
dev_display (ObjectSelected)
endfor
*获取条码类型
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
*显示信息
disp_message (WindowHandle, ['Found bar code(s) in ' + Duration$'.0f' + ' ms:',DecodedDataTypes + ': ' + DecodedDataStrings], 'window', 5 * 12, 12, Color, 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endfor
endfor
*
* Part II
*
* Now we decode multiple bar code types
*
* Display information for the user
*现在我们解码多种条形码类型
*为用户显示信息
Message := 'Part II'
Message[1] := ' '
Message[2] := 'Now we demonstrate the usage of autodiscrimination'
Message[3] := 'to decode multiple mixed bar code types within one image.'
dev_clear_window ()
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
for IdxExample := 0 to 1 by 1
if (IdxExample)
*
* We could scan for all bar codes except certain bar code
* families...
*我们可以扫描所有条形码,除了某些条形码系列。。。
* 获取条码类型名称
get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
NoGS1 := '~' + regexp_select(AllCodeTypes,'GS1.*')
NoUPC := '~' + regexp_select(AllCodeTypes,'UPC.*')
CodeTypes := ['auto',NoGS1,NoUPC]
CodeTypesDescription := 'all types, except GS1 and UPC variants'
else
*
* ...or (as we do here) scan only for the EAN family and
* Code 39
** ...或者(就像我们在这里做的)只扫描EAN系列和代码39
*获取条码类型名称
get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
AllEAN := regexp_select(AllCodeTypes,'EAN-13.*')
CodeTypes := [AllEAN,'Code 39']
CodeTypesDescription := 'Code 39 and all EAN variants'
endif
*
* Demonstrate autodiscrimination for mixed bar code types
*演示混合条形码类型的自动识别
for IdxIma := 0 to |ExampleImagesMixed| - 1 by 1
FileName := ExampleImagesMixed[IdxIma]
read_image (Image, FileName)
*
* Display image and description
*显示图像和描述
get_image_size (Image, Width, Height)
dev_set_window_extents (-1, -1, Width / 2, Height / 2)
dev_display (Image)
disp_message (WindowHandle, ['Looking for bar code(s) of type:',' ' + CodeTypesDescription], 'window', 12, 12, 'black', 'true')
*
* Decode mixed bar codes
*解码混合条形码
find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
*
* Display decoded data and symbol region
*显示解码数据和符号区域
dev_set_color ('forest green')
dev_display (SymbolRegions)
*获取条形码类型参数
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
area_center (SymbolRegions, Area, Rows, Columns)
disp_message (WindowHandle, DecodedDataTypes + ': ' + DecodedDataStrings, 'image', Rows, Columns - 160, 'forest green', 'true')
*是否跳出循环
if (IdxIma < |ExampleImagesMixed| - 1 or IdxExample == 0)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
endfor
*
* Close the bar code reader to clean up memory
*关闭条形码阅读器以清除内存
stop ()
clear_bar_code_model (BarCodeHandle)
逐段分析:
*创建一个条形码阅读器的模型。
*第一个参数为通用参数名字,第二个参数为通用参数的值,第三个为模型的句柄
create_bar_code_model ([], [], BarCodeHandle)
*设置条码读取器参数,是否验证空白区域是否干净
set_bar_code_param (BarCodeHandle, 'quiet_zone', 'true')
*设置一些参数
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('forest green')
*定义要使用的示例图像
*第一部分
*定义每一种条码的样本图像
ExampleImagesAny := ['barcode/ean13/ean1301','barcode/code39/code3901','barcode/gs1databar_omnidir/gs1databar_omnidir_01']
*期望条码数量
ExpectedNumCodes := [1,2,1]
*期望数据
ExpectedData := ['4008535118906','92274A','R74-2001-010','(01)00098431358722']
*第二部分
*混合图像
ExampleImagesMixed := ['barcode/mixed/barcode_mixed_04','barcode/mixed/barcode_mixed_03','barcode/mixed/barcode_mixed_01']
*第一部分
*使用自动识别对任何条形码类型进行解码
*由HALCON(PharmaCode除外)支持或确定未知条形码的条形码类型
for IdxExample := 0 to 1 by 1
*每次运行使用不同的参数
*第一:搜索所有条形码类型
*第二:只搜索预期的条形码类型
*定义显示信息
Message := 'Usage of autodiscrimination to decode'
Message[1] := 'unknown bar code types.'
Message[2] := ' '
*搜索所有条码类型的参数和显示信息
if (IdxExample == 0)
CodeTypes := 'auto'
Message[3] := 'Part I:'
Message[4] := 'Compare performance between automatic'
Message[5] := 'bar code discrimination and search for'
Message[6] := 'specific code types only.'
Message[7] := ' '
Message[8] := 'In the first run we use CodeType=\'' + CodeTypes + '\' to look'
Message[9] := 'for all bar code types known to HALCON'
Message[10] := ' '
Message[11] := 'Please note that'
Message[12] := ' - Wrong bar codes may be found'
Message[13] := ' - Execution times are longer than necessary'
*为了让最后两行文字显示红色
Color := [gen_tuple_const(|Message| - 2,'black'),gen_tuple_const(3,'red')]
*搜索期望条码类型的参数和显示信息
else
CodeTypes := ['EAN-13','Code 39','GS1 DataBar Omnidir']
Message := 'In the second run we look for the expected types only:'
Message[1] := sum(' ' + CodeTypes)
Message[2] := 'This reduces the decoding time and'
Message[3] := 'increases the decoding reliability.'
Color := gen_tuple_const(|Message|,'black')
endif
*清除窗口
dev_clear_window ()
*显示信息
disp_message (WindowHandle, Message, 'window', 12, 12, Color, 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
![](https://img-blog.csdnimg.cn/408a46a42df640cf9895443aa9621aae.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_13,color_FFFFFF,t_70,g_se,x_16)
*使用不同的参数运行测试2次
for IdxIma := 0 to |ExampleImagesAny| - 1 by 1
*图片路径
FileName := ExampleImagesAny[IdxIma]
read_image (Image, FileName)
*设置一些默认参数
get_image_size (Image, Width, Height)
dev_set_window_extents (-1, -1, Width, Height)
dev_display (Image)
*显示提示
disp_message (WindowHandle, ['Looking for bar code(s) of type:',sum(' ' + CodeTypes)], 'window', 12, 12, 'black', 'true')
![](https://img-blog.csdnimg.cn/32f2bd87326649d496f84ff0061e6772.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_16,color_FFFFFF,t_70,g_se,x_16)
*查找并解码条形码。测量所需的时间。
*开始第一次计时
count_seconds (Start)
*寻找条形码
find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
*结束计时
count_seconds (Stop)
*计算时间
Duration := (Stop - Start) * 1000
![](https://img-blog.csdnimg.cn/28bae1f6fdc541d59b99c06ceb89b76a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_9,color_FFFFFF,t_70,g_se,x_16)
*检查结果是否正确
Start := sum(ExpectedNumCodes[0:IdxIma]) - ExpectedNumCodes[IdxIma]
DataExp := ExpectedData[Start:Start + ExpectedNumCodes[IdxIma] - 1]
WrongIndex := []
*找到的值是否包含在期望的值中
for I := 0 to |DecodedDataStrings| - 1 by 1
tuple_find (DataExp, DecodedDataStrings[I], Indices)
if (Indices == -1)
WrongIndex := [WrongIndex,I]
endif
endfor
*定义一个颜色数组
Color := ['black',gen_tuple_const(|DecodedDataStrings|,'forest green')]
Color[WrongIndex + 1] := 'red'
*显示结果和执行时间
for I := 1 to |DecodedDataStrings| by 1
*选择条码区域
select_obj (SymbolRegions, ObjectSelected, I)
dev_set_color (Color[I])
dev_display (ObjectSelected)
endfor
*获取条码类型
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
*显示信息
disp_message (WindowHandle, ['Found bar code(s) in ' + Duration$'.0f' + ' ms:',DecodedDataTypes + ': ' + DecodedDataStrings], 'window', 5 * 12, 12, Color, 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endfor
endfor
![](https://img-blog.csdnimg.cn/a2b27c2637bd4617a2623c273642e4ec.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_16,color_FFFFFF,t_70,g_se,x_16)
*现在我们解码多种条形码类型
*为用户显示信息
Message := 'Part II'
Message[1] := ' '
Message[2] := 'Now we demonstrate the usage of autodiscrimination'
Message[3] := 'to decode multiple mixed bar code types within one image.'
dev_clear_window ()
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
![](https://img-blog.csdnimg.cn/70646f3dd86e4e56ae877132138b63c8.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_20,color_FFFFFF,t_70,g_se,x_16)
for IdxExample := 0 to 1 by 1
if (IdxExample)
*我们可以扫描所有条形码,除了某些条形码系列。。。
*获取条码类型名称
get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
NoGS1 := '~' + regexp_select(AllCodeTypes,'GS1.*')
NoUPC := '~' + regexp_select(AllCodeTypes,'UPC.*')
CodeTypes := ['auto',NoGS1,NoUPC]
CodeTypesDescription := 'all types, except GS1 and UPC variants'
else
*或者(就像我们在这里做的)只扫描EAN系列和代码39
*获取条码类型名称
get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
AllEAN := regexp_select(AllCodeTypes,'EAN-13.*')
CodeTypes := [AllEAN,'Code 39']
CodeTypesDescription := 'Code 39 and all EAN variants'
endif
*演示混合条形码类型的自动识别
for IdxIma := 0 to |ExampleImagesMixed| - 1 by 1
FileName := ExampleImagesMixed[IdxIma]
read_image (Image, FileName)
*显示图像和描述
get_image_size (Image, Width, Height)
dev_set_window_extents (-1, -1, Width / 2, Height / 2)
dev_display (Image)
disp_message (WindowHandle, ['Looking for bar code(s) of type:',' ' + CodeTypesDescription], 'window', 12, 12, 'black', 'true')
*解码混合条形码
find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings)
*显示解码数据和符号区域
dev_set_color ('forest green')
dev_display (SymbolRegions)
*获取条形码类型参数
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
area_center (SymbolRegions, Area, Rows, Columns)
disp_message (WindowHandle, DecodedDataTypes + ': ' + DecodedDataStrings, 'image', Rows, Columns - 160, 'forest green', 'true')
![](https://img-blog.csdnimg.cn/e5714590f195432bb8dff5d256a3fab1.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9vbUJpdUJpdQ==,size_14,color_FFFFFF,t_70,g_se,x_16)
*是否跳出循环
if (IdxIma < |ExampleImagesMixed| - 1 or IdxExample == 0)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
endfor
*关闭条形码阅读器以清除内存
stop ()
clear_bar_code_model (BarCodeHandle)