CodeInfo.cs
2.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace IDHIKCamera
{
/// <summary>
/// 条码信息
/// </summary>
[Serializable]
public class CodeInfo
{
public string CodeStr = "";
public int X = 0;
public int Y = 0;
public string CodeType;
public double Orientation = 0;
public CodeInfo()
{
}
public CodeInfo(string codeStr, int x, int y)
{
this.CodeStr = codeStr;
this.X = x;
this.Y = y;
}
public CodeInfo(string codeStr, int x, int y, string type)
{
this.CodeType = type;
this.CodeStr = codeStr;
this.X = x;
this.Y = y;
}
public string GetCodeStr()
{
return Gb2312Correct(CodeStr);
}
/// <summary>
/// 判断字符串中是否包含中文
/// </summary>
/// <param name="str">需要判断的字符串</param>
/// <returns>判断结果</returns>
public static bool HasChinese(string str)
{
return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
}
/// <summary>
/// utf8文字用gb2312格式显示时候乱码,需要转换为gb2312
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Gb2312Correct(string text)
{
//string ascii= Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(text));
//return ascii;
if (!HasChinese(text))
{
return text;
}
//声明字符集
System.Text.Encoding utf8, gb2312;
//utf8
utf8 = System.Text.Encoding.GetEncoding("utf-8");
//gb2312
gb2312 = System.Text.Encoding.GetEncoding("gb2312");
byte[] gb;
gb = utf8.GetBytes(text);
//utf8.GetString(System.Text.Encoding.Convert(utf8, gb2312, gb));
gb = System.Text.Encoding.Convert(utf8, gb2312, gb);
//返回转换后的字符
string s = utf8.GetString(gb);
int sp = 0;
while (true)
{
sp = s.IndexOf("?", sp + 1);
if (sp < 0)
break;
if (s.Substring(sp, 2) != "?;")
{
s = s.Insert(sp + 1, ";");
}
}
return s;
}
}
}