ZXingCodeHelper.cs
7.8 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;
using ZXing.Multi.QrCode;
namespace CodeLibrary
{
public class ZXingCodeHelper
{
public static List<string> DeCodes(Bitmap map, string codeType)
{
if (codeType.ToUpper().Equals("QR CODE"))
{
return DecodeQRCodes(map);
}
else if (codeType.ToUpper().Equals("DATA MATRIX ECC 200"))
{
return DecodeCodes(map);
}
else if (codeType.ToUpper().Equals("BARCODE"))
{
return DecodeCodes(map);
}
else
{
return DecodeCodes(map);
}
}
public static List<string> DecodeCodes(Bitmap bmp)
{
MultiFormatReader mreader = new MultiFormatReader();
ZXing.Multi.GenericMultipleBarcodeReader genericMultiple = new ZXing.Multi.GenericMultipleBarcodeReader(mreader);
LuminanceSource source = new BitmapLuminanceSource(bmp);
BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source));
Result[] rr = genericMultiple.decodeMultiple(binarybitmap);
List<string> result = new List<string>();
if (rr != null)
{
foreach (Result res in rr)
{
if (res != null)
{
string text = res.ToString();
if (!IsGBCode(text))
{
text = ConvertISO88591ToEncoding(text, Encoding.Default);
}
result.Add(text);
}
}
}
return result;
}
public static List<string> DecodeBarCodes(Bitmap bmp)
{
List<string> result = new List<string>();
BarcodeReader br = new BarcodeReader();
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.CODE_39, BarcodeFormat.CODE_128 };
br.Options = decodeOption;
Result res = br.Decode(bmp);
if (res != null)
{
result.Add(res.ToString());
}
return result;
}
public static List<string> DecodeDMCodes(Bitmap bmp)
{
ZXing.Datamatrix.DataMatrixReader qc = new ZXing.Datamatrix.DataMatrixReader();
List<string> result = new List<string>();
LuminanceSource source = new BitmapLuminanceSource(bmp);
BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source));
IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
hints.Add(DecodeHintType.CHARACTER_SET, "UTF-8");
hints.Add(DecodeHintType.TRY_HARDER, "3");
Result res = qc.decode(binarybitmap, hints);
if (res != null)
{
result.Add(res.ToString());
}
return result;
}
public static List<string> DecodeQRCodes(Bitmap bmp)
{
QRCodeMultiReader qc = new QRCodeMultiReader();
List<string> result = new List<string>();
LuminanceSource source = new BitmapLuminanceSource(bmp);
BinaryBitmap binarybitmap = new BinaryBitmap(new HybridBinarizer(source));
IDictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
hints.Add(DecodeHintType.CHARACTER_SET, "GB2312");
hints.Add(DecodeHintType.TRY_HARDER, "3");
Result[] r = qc.decodeMultiple(binarybitmap, hints);
if (r != null)
{
foreach (Result res in r)
{
if (res != null)
{
string text = res.ToString();
//if (!IsGBCode(text))
//{
// string chStr = "生产日期(或厂家批次):";
// int index = text.IndexOf(chStr);
// if (index > 0)
// {
// string sub1 = text.Substring(0, index);
// int sub3startIndex =index+ chStr.Length;
// string sub3 = text.Substring(sub3startIndex,text.Length- sub3startIndex);
// string sub1r = ConvertISO88591ToEncoding(sub1, Encoding.Default);
// string sub3r = ConvertISO88591ToEncoding(sub3, Encoding.Default);
// text = sub1r + chStr + sub3r;
// }
// else
// {
// text = ConvertISO88591ToEncoding(text, Encoding.Default);
// }
//}
result.Add(text);
}
}
}
return result;
}
//public static string DecodeQRCode(Bitmap bmp)
//{
// string text = "";
// DecodingOptions option = new DecodingOptions();
// //option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D };
// option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
// BarcodeReader br = new BarcodeReader();
// br.Options = option;
// Result rs = br.Decode(bmp);
// if (rs == null)
// {
// text = "";
// }
// else
// {
// text = rs.ToString();
// if (!IsGBCode(text))
// {
// text = ConvertISO88591ToEncoding(text, Encoding.Default);
// }
// }
// return text;
//}
//转换
private static string ConvertISO88591ToEncoding(string srcString, Encoding dstEncode)
{
String sResult;
Encoding ISO88591Encoding = Encoding.GetEncoding("ISO-8859-1");
Encoding GB2312Encoding = Encoding.GetEncoding("GB2312"); //这个地方很特殊,必须利用GB2312编码
byte[] srcBytes = ISO88591Encoding.GetBytes(srcString);
//将原本存储ISO-8859-1的字节数组当成GB2312转换成目标编码(关键步骤)
byte[] dstBytes = Encoding.Convert(GB2312Encoding, dstEncode, srcBytes);
char[] dstChars = new char[dstEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
dstEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);//利用char数组存储字符
sResult = new string(dstChars);
return sResult;
}
/// <summary>
/// 判断一个word是否为GB2312编码的汉字
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
private static bool IsGBCode(string word)
{
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code
{
return false;
}
else
{
byte byte1 = bytes[0];
byte byte2 = bytes[1];
if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判断是否是GB2312
{
return true;
}
else
{
return false;
}
}
}
}
}