DHBarCodeHelper.cs
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeLibrary
{
public class DHBarCodeHelper
{ /// <summary>
/// 根据图片解析二维码
/// </summary>
/// <param name="ho_Image">Halcon中的图片对象</param>
/// <param name="codeCount">二维码数量</param>
/// <param name="codeParamPath">二维码参数路径,""表示不使用参数</param>
/// <param name="paramType">二维码类型,不传类型默认Data Matrix ECC 200</param>
/// <returns>解析到的二维码</returns>
public static List<CodeInfo> DecodeCode(HObject ho_Image, int codeCount, string codeParamPath, params string[] paramType)
{
List<string> codeType = new List<string>(paramType.ToList());
if (codeType.Count<string>() <= 0)
{
codeType.Add("CODE_39");
}
List<CodeInfo> codeList = new List<CodeInfo>();
foreach (string t in codeType)
{
List<CodeInfo> array = GetCode(ho_Image, t, codeParamPath, codeCount);
codeList.AddRange(array.ToArray<CodeInfo>());
}
return codeList;
}
private static List<CodeInfo> GetCode(HObject ho_Image, string symbolType, string hv_model_path, int codeCount)
{
List<CodeInfo> codeList = new List<CodeInfo>();
try
{
HTuple hv_Area = null;
HTuple hv_Row1 = null;
HTuple hv_Column = null;
HTuple hv_PointOrder = null;
HObject ho_SymbolXLDs;
HTuple hv_ResultHandles = null;
HTuple hv_DecodedDataStrings = null;
HTuple hv_DataCodeHandle = null;
HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
HOperatorSet.CreateBarCodeModel(symbolType, "default_parameters", "maximum_recognition", out hv_DataCodeHandle);
//string hv_model_path = GetCodeParamFilePath(symbolType);
if (!hv_model_path.Equals("") && File.Exists(hv_model_path))
{
HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
}
ho_SymbolXLDs.Dispose();
if (codeCount <= 0)
{
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
}
else
{
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
"stop_after_result_num", codeCount, out hv_ResultHandles, out hv_DecodedDataStrings);
}
HOperatorSet.AreaCenterXld(ho_SymbolXLDs, out hv_Area, out hv_Row1, out hv_Column, out hv_PointOrder);
if (HalconWindow != null)
{
ShowImage(HalconWindow, ho_Image, ho_SymbolXLDs);
}
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
string[] resultList = hv_DecodedDataStrings.SArr;
if (resultList.Length > 0)
{
for (int i = 0; i < hv_DecodedDataStrings.SArr.Length; i++)
{
try
{
int x = (int)Math.Round(hv_Column.DArr[i]);
int y = (int)Math.Round(hv_Row1.DArr[i]);
string str = hv_DecodedDataStrings.SArr[i];
CodeInfo code = new CodeInfo(str, x, y);
codeList.Add(code);
}
catch (Exception ex)
{
HDLogUtil.error("处理二维码出错:索引=" + i + "," + resultList.ToString());
}
}
}
return codeList;
}
catch (Exception ex)
{
return codeList;
}
}
}
}