-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathZxingBarcodes.cs
More file actions
233 lines (198 loc) · 7.41 KB
/
ZxingBarcodes.cs
File metadata and controls
233 lines (198 loc) · 7.41 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Text;
using Majorsilence.Reporting.Rdl;
#if DRAWINGCOMPAT
using Draw2 = Majorsilence.Drawing;
#else
using Draw2 = System.Drawing;
#endif
using System.ComponentModel;
using System.Xml;
using ZXing;
namespace Majorsilence.Reporting.Cri
{
public class ZxingBarcodes : ICustomReportItem
{
private readonly float OptimalHeight;
private readonly float OptimalWidth;
protected ZXing.BarcodeFormat format;
// special chars for datamatrix, gs1 128
protected string FrontMatter = "";
protected string EndMatter = "";
#region ICustomReportItem Members
// optimal height and width are set to 35.91mm, which is the default for most barcodes.
public ZxingBarcodes() : this(35.91f, 65.91f)
{
}
public ZxingBarcodes(float optimalHeight, float optimalWidth)
{
OptimalHeight = optimalHeight;
OptimalWidth = optimalWidth;
}
bool ICustomReportItem.IsDataRegion()
{
return false;
}
void ICustomReportItem.DrawImage(ref Draw2.Bitmap bm)
{
DrawImage(ref bm, _code);
}
/// <summary>
/// Does the actual drawing of the image.
/// </summary>
/// <param name="bm"></param>
/// <param name="qrcode"></param>
internal void DrawImage(ref Draw2.Bitmap bm, string qrcode)
{
#if DRAWINGCOMPAT
var writer = new ZXing.SkiaSharp.BarcodeWriter();
#elif NETSTANDARD2_0 || NET5_0_OR_GREATER
var writer = new ZXing.Windows.Compatibility.BarcodeWriter();
#else
var writer = new ZXing.BarcodeWriter();
#endif
writer.Format = format;
writer.Options.Hints[EncodeHintType.CHARACTER_SET] = "UTF-8";
using Draw2.Graphics g = Draw2.Graphics.FromImage(bm);
float mag = PixelConversions.GetMagnification(g, bm.Width, bm.Height, OptimalHeight, OptimalWidth);
int barHeight = PixelConversions.PixelXFromMm(g, OptimalHeight * mag);
int barWidth = PixelConversions.PixelYFromMm(g, OptimalWidth * mag);
writer.Options.Height = barHeight;
writer.Options.Width = barWidth;
try
{
// TODO: move to program startup
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
}
catch (InvalidOperationException)
{
// The provider has already been registered.
}
var barcodeBitmap = writer.Write(qrcode);
// Fill background with white
g.FillRectangle(Draw2.Brushes.White, 0, 0, bm.Width, bm.Height);
// Draw the barcode aligned to the left (top-left corner at 0, 0)
g.DrawImage(barcodeBitmap, 0, 0);
}
/// <summary>
/// Design time: Draw a hard coded BarCode for design time; Parameters can't be
/// relied on since they aren't available.
/// </summary>
/// <param name="bm"></param>
void ICustomReportItem.DrawDesignerImage(ref Draw2.Bitmap bm)
{
DrawImage(ref bm, "https://github.com/majorsilence/My-FyiReporting");
}
private string _code = "";
void ICustomReportItem.SetProperties(IDictionary<string, object> props)
{
try
{
// fallback to standard "Code" property
_code = props["Code"].ToString();
if (props.TryGetValue("AztecCode", out object codeValueA))
{
// Backwards Compatibility: if the property is present, use it
_code = codeValueA.ToString();
}
else if (props.TryGetValue("QrCode", out object codeValueQ))
{
// Backwards Compatibility: if the property is present, use it
_code = codeValueQ.ToString();
}
else {
// fallback to standard "Code" property
_code = props["Code"].ToString();
}
}
catch (KeyNotFoundException)
{
throw new Exception("Code property must be specified");
}
}
object ICustomReportItem.GetPropertiesInstance(System.Xml.XmlNode iNode)
{
ZxingBarCodeProperties bcp = new ZxingBarCodeProperties(this, iNode);
foreach (XmlNode n in iNode.ChildNodes)
{
if (n.Name != "CustomProperty")
continue;
string pname = XmlHelpers.GetNamedElementValue(n, "Name", "");
switch (pname)
{
case "Code":
bcp.SetCode(XmlHelpers.GetNamedElementValue(n, "Value", ""));
break;
default:
break;
}
}
return bcp;
}
public void SetPropertiesInstance(System.Xml.XmlNode node, object inst)
{
node.RemoveAll(); // Get rid of all properties
ZxingBarCodeProperties bcp = inst as ZxingBarCodeProperties;
if (bcp == null)
return;
XmlHelpers.CreateChild(node, "Code", bcp.Code);
}
/// <summary>
/// Design time call: return string with <CustomReportItem> ... </CustomReportItem> syntax for
/// the insert. The string contains a variable {0} which will be substituted with the
/// configuration name. This allows the name to be completely controlled by
/// the configuration file.
/// </summary>
/// <returns></returns>
string ICustomReportItem.GetCustomReportItemXml()
{
return "<CustomReportItem><Type>{0}</Type>" +
string.Format("<Height>{0}mm</Height><Width>{1}mm</Width>", OptimalHeight, OptimalWidth) +
"<CustomProperties>" +
"<CustomProperty>" +
"<Name>Code</Name>" +
"<Value>Enter Your Value</Value>" +
"</CustomProperty>" +
"</CustomProperties>" +
"</CustomReportItem>";
}
#endregion
#region IDisposable Members
void IDisposable.Dispose()
{
return;
}
#endregion
/// <summary>
/// BarCodeProperties- All properties are type string to allow for definition of
/// a runtime expression.
/// </summary>
public class ZxingBarCodeProperties
{
string _Code;
ZxingBarcodes _bc;
XmlNode _node;
internal ZxingBarCodeProperties(ZxingBarcodes bc, XmlNode node)
{
_bc = bc;
_node = node;
}
internal void SetCode(string ns)
{
_Code = ns;
}
[Category("Code"),
Description("The text string to be encoded as a PDF417 barcode.")]
public string Code
{
get { return _Code; }
set
{
_Code = value;
_bc.SetPropertiesInstance(_node, this);
}
}
}
}
}