首页 > 其他分享 >Halcon实用系列-识别二维条码

Halcon实用系列-识别二维条码

时间:2022-11-03 11:26:06浏览次数:48  
标签:条码 hv Dispose Halcon 二维 ho HTuple DataCodeHandle new

在做项目时,之前使用的是某康的智能读码器,综合考虑成本,可通过相机拍照来读取图片的二维码,我这边用Halcon来实现。

Halcon代码如下:

 1 *创建模型
 2 create_data_code_2d_model('Data Matrix ECC 200', [], [], DataCodeHandle)
 3 
 4 *设置条码极性
 5 set_data_code_2d_param(DataCodeHandle, 'polarity', 'any')
 6 
 7 *解码时长的设置,超时直接将条码丢弃
 8 set_data_code_2d_param(DataCodeHandle, 'timeout', 200)
 9 
10 *开始二维码识别
11 read_image (Image, 'C:/Users/Administrator/Desktop/AirPods/Image_20220815110012088.bmp')
12 
13 *开始计时
14 count_seconds(Seconds)
15 
16 *解析二维码
17 find_data_code_2d(Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
18 
19 *结束计时
20 count_seconds(Seconds1)
21 
22 *累计耗时
23 time:=Seconds1-Seconds
24 
25 
26 *判断解码结果是否有结果
27 tuple_length(DecodedDataStrings, Length)
28 if (Length>0)
29     *//获取区域
30     concat_obj(SymbolXLDs, SymbolXLDs, ObjectsConcat)
31     
32     dev_clear_window ()
33     dev_display (Image)
34     *绘制区域
35     dev_display(ObjectsConcat)
36     
37     *显示二维码
38     dev_disp_text(DecodedDataStrings, 'window', 'top', 'left', 'black', [], [])
39 endif
View Code

 显示效果:

可以看出几十毫秒可以解读条码,如要识别其他类型的二维码,可更改create_data_code_2d_model算子的参数。

那么如何将Halcon代码转化为C#可调用的方法?

1 先将Halcon代码导出,得到cs文件

 2 在VS中添加Halcon的引用,并将方法调整,我这里是结合OpencvSHarp

调整后的方法:

 1         /// <summary>
 2         ///  识别 Data Matrix ECC 200
 3         /// </summary>
 4         /// <param name="ho_InputImage">图片对象</param>
 5         /// <returns></returns>
 6         private string ReadDataMatrix(HObject ho_InputImage)
 7         {
 8             string result = string.Empty;
 9             // Local iconic variables 
10             HObject ho_SymbolXLDs;
11             HObject ho_ObjectsConcat = null;
12             // Local control variables 
13             HTuple hv_DataCodeHandle = new HTuple(), hv_Seconds = new HTuple();
14             HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple();
15             HTuple hv_Seconds1 = new HTuple(), hv_time = new HTuple();
16             HTuple hv_Length = new HTuple();
17             // Initialize local and output iconic variables 
18             HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
19             HOperatorSet.GenEmptyObj(out ho_ObjectsConcat);
20             try
21             {
22 
23                 //创建模型
24                 hv_DataCodeHandle.Dispose();
25                 //QR Code
26                 HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
27                 //HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
28 
29                 //设置条码极性
30                 HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "polarity", "any");
31 
32                 //解码时长的设置,超时直接将条码丢弃
33                 HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", 300);
34 
35                 //**********开始二维码识别****************
36 
37                 //开始计时
38                 hv_Seconds.Dispose();
39                 HOperatorSet.CountSeconds(out hv_Seconds);
40 
41                 //解析二维码
42                 ho_SymbolXLDs.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose();
43                 HOperatorSet.FindDataCode2d(ho_InputImage, out ho_SymbolXLDs, hv_DataCodeHandle,
44                     new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
45 
46                 //结束计时
47                 hv_Seconds1.Dispose();
48                 HOperatorSet.CountSeconds(out hv_Seconds1);
49 
50                 //累计耗时
51                 hv_time.Dispose();
52                 using (HDevDisposeHelper dh = new HDevDisposeHelper())
53                 {
54                     hv_time = hv_Seconds1 - hv_Seconds;
55                 }
56                 //MessageBox.Show(hv_time);
57                 //判断解码结果是否有结果
58                 hv_Length.Dispose();
59                 HOperatorSet.TupleLength(hv_DecodedDataStrings, out hv_Length);
60                 if ((int)(new HTuple(hv_Length.TupleGreater(0))) != 0)
61                 {
62                     result = hv_DecodedDataStrings;
63                 }
64             }
65             catch (Exception ex)
66             {
67                 return result;
68             }
69             finally
70             {
71                 ho_InputImage.Dispose();
72                 ho_SymbolXLDs.Dispose();
73                 ho_ObjectsConcat.Dispose();
74 
75                 hv_DataCodeHandle.Dispose();
76                 hv_Seconds.Dispose();
77                 hv_ResultHandles.Dispose();
78                 hv_DecodedDataStrings.Dispose();
79                 hv_Seconds1.Dispose();
80                 hv_time.Dispose();
81                 hv_Length.Dispose();
82             }
83             return result;
84         }
View Code

此处还涉及Mat对象转换为HObject

 1         /// <summary>
 2         ///  Mat转换为HObject
 3         /// </summary>
 4         /// <param name="mat"></param>
 5         /// <returns></returns>
 6         public HImage Mat2HObject(Mat mat)
 7         {
 8             HImage hImage = new HImage();
 9             try
10             {
11                 if (mat.Type() == MatType.CV_8UC1)
12                 {
13                     hImage.GenImage1("byte", mat.Width, mat.Height, mat.Data);
14                 }
15                 else
16                 {
17                     hImage.GenImageInterleaved(mat.Data, "rgb", mat.Width, mat.Height, 0, "byte", mat.Width, mat.Height, 0, 0, -1, 0);
18                 }
19             }
20             catch (Exception)
21             {
22 
23                 
24             }
25             finally
26             {
27                 mat.Dispose();
28             }
29             return hImage;
30         }
View Code

 

标签:条码,hv,Dispose,Halcon,二维,ho,HTuple,DataCodeHandle,new
From: https://www.cnblogs.com/arvinzd/p/16851369.html

相关文章