ConvertBarcode.cs
5.1 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
using System;
using System.Collections.Generic;
using System.Drawing;
using Model;
namespace BLL
{
public static class ConvertBarcode
{
private static int width;
private static int height;
private static string text;
private static readonly ZXing.BarcodeWriter writer = new();
private static readonly Dictionary<PrintLabelFieldType, Func<Bitmap>> code = new()
{
{ PrintLabelFieldType.Text, Text },
{ PrintLabelFieldType.Code39, Code39 },
{ PrintLabelFieldType.Code93, Code93 },
{ PrintLabelFieldType.Code128, Code128 },
{ PrintLabelFieldType.DataMatrix, DATA_MATRIX },
{ PrintLabelFieldType.QRCode, QR_CODE },
{ PrintLabelFieldType.PDF417, PDF_417 }
};
public static Bitmap StrToCode(PrintLabelFieldType type, string s, SizeF size)
{
text = s;
width = Convert.ToInt32(size.Width);
height = Convert.ToInt32(size.Height);
if (string.IsNullOrEmpty(text))
return null;
return code[type].Invoke();
}
private static Bitmap Text()
{
return null;
}
private static Bitmap Code39()
{
try
{
ZXing.Common.EncodingOptions opt = new()
{
Width = width,
Height = height,
Margin = 2,
PureBarcode = true
};
writer.Options = opt;
writer.Format = ZXing.BarcodeFormat.CODE_39;
return writer.Write(text);
}
catch (Exception ex)
{
LogNet.log.Error("Code39", ex);
return null;
}
}
private static Bitmap Code93()
{
try
{
ZXing.Common.EncodingOptions opt = new()
{
Width = width,
Height = height,
Margin = 2,
PureBarcode = true
};
writer.Options = opt;
writer.Format = ZXing.BarcodeFormat.CODE_93;
return writer.Write(text);
}
catch (Exception ex)
{
LogNet.log.Error("Code93", ex);
return null;
}
}
private static Bitmap Code128()
{
try
{
ZXing.Common.EncodingOptions opt = new()
{
Width = width,
Height = height,
Margin = 2,
PureBarcode = true
};
writer.Options = opt;
writer.Format = ZXing.BarcodeFormat.CODE_128;
return writer.Write(text);
}
catch (Exception ex)
{
LogNet.log.Error("Code128", ex);
return null;
}
}
private static Bitmap DATA_MATRIX()
{
try
{
ZXing.Datamatrix.DatamatrixEncodingOptions opt = new()
{
Width = width,
Height = height,
Margin = 2,
PureBarcode = true
};
writer.Options = opt;
writer.Format = ZXing.BarcodeFormat.DATA_MATRIX;
return writer.Write(text);
}
catch (Exception ex)
{
LogNet.log.Error("DATA_MATRIX", ex);
return null;
}
}
private static Bitmap QR_CODE()
{
try
{
ZXing.QrCode.QrCodeEncodingOptions opt = new()
{
DisableECI = true,
CharacterSet = "UTF-8",
//Width = width,
//Height = height,
Width = 300,
Height = 300,
Margin = 1,
PureBarcode = true
};
writer.Options = opt;
writer.Format = ZXing.BarcodeFormat.QR_CODE;
return writer.Write(text);
}
catch (Exception ex)
{
LogNet.log.Error("QR_CODE", ex);
return null;
}
}
private static Bitmap PDF_417()
{
try
{
ZXing.PDF417.PDF417EncodingOptions opt = new()
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = 100,
//Height = height,
Margin = 2,
PureBarcode = true
};
writer.Options = opt;
writer.Format = ZXing.BarcodeFormat.PDF_417;
return writer.Write(text);
}
catch (Exception ex)
{
LogNet.log.Error("PDF_417", ex);
return null;
}
}
}
}