/
BarcodeGenerator.cs
executable file
·357 lines (335 loc) · 17.2 KB
/
BarcodeGenerator.cs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using InteropBarcode = Interop.MediaVision.BarcodeGenerator;
namespace Tizen.Multimedia.Vision
{
/// <summary>
/// Provides the ability to generate barcodes and QR codes.
/// Different encoding types <see cref="QrMode"/>, error correction codes <see cref="ErrorCorrectionLevel"/>,
/// and code versions are supported for QRCodes.
/// </summary>
/// <seealso cref="BarcodeGenerationConfiguration"/>
/// <since_tizen> 4 </since_tizen>
public static class BarcodeGenerator
{
private const int NoneErrorCorrection = (int)ErrorCorrectionLevel.High + 1;
private const int NoneQrMode = (int)QrMode.Utf8 + 1;
private static MediaVisionSource GenerateSource(BarcodeGenerationConfiguration config,
string message, BarcodeType type, int qrMode, int qrEcc, int qrVersion)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
ValidationUtil.ValidateEnum(typeof(BarcodeType), type, nameof(type));
MediaVisionSource source = new MediaVisionSource();
try
{
InteropBarcode.GenerateSource(EngineConfiguration.GetHandle(config),
message, type, qrMode, qrEcc, qrVersion, source.Handle).
Validate("Failed to generate source");
}
catch (Exception)
{
source.Dispose();
throw;
}
return source;
}
/// <summary>
/// Generates a QR image with the specified message.
/// </summary>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
/// <returns><see cref="MediaVisionSource"/> containing the generated QR image.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="qrConfig"/> is null.<br/>
/// -or-<br/>
/// <paramref name="message"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
/// </exception>
/// <exception cref="NotSupportedException">The feature is not supported.</exception>
/// <seealso cref="QrMode"/>
/// <since_tizen> 4 </since_tizen>
public static MediaVisionSource GenerateSource(string message, QrConfiguration qrConfig)
{
return GenerateSource(message, qrConfig, null);
}
/// <summary>
/// Generates a QR image with the specified message with <see cref="BarcodeGenerationConfiguration"/>.
/// </summary>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
/// <param name="config">The configuration of the barcode generator. This value can be null.</param>
/// <returns><see cref="MediaVisionSource"/> containing the generated QR image.</returns>
/// <remarks>
/// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
/// because the text visibility is not supported in the QR code.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="qrConfig"/> is null.<br/>
/// -or-<br/>
/// <paramref name="message"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
/// </exception>
/// <exception cref="NotSupportedException">
/// The feature is not supported.<br/>
/// -or-<br/>
/// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
/// </exception>
/// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
/// <seealso cref="QrMode"/>
/// <since_tizen> 4 </since_tizen>
public static MediaVisionSource GenerateSource(string message, QrConfiguration qrConfig,
BarcodeGenerationConfiguration config)
{
if (qrConfig == null)
{
throw new ArgumentNullException(nameof(qrConfig));
}
if (config != null)
{
if (config.TextVisibility == Visibility.Visible)
{
throw new NotSupportedException("Text can't be visible in QR.");
}
}
return GenerateSource(config, message, BarcodeType.QR, (int)qrConfig.Mode,
(int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
}
/// <summary>
/// Generates a barcode image with the specified message.
/// </summary>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="type">Type of the barcode to be generated.</param>
/// <returns><see cref="MediaVisionSource"/> containing the generated barcode image.</returns>
/// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
/// -or-<br/>
/// <paramref name="type"/> is invalid.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains illegal characters.
/// </exception>
/// <exception cref="NotSupportedException">The feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public static MediaVisionSource GenerateSource(string message, BarcodeType type)
{
return GenerateSource(message, type, null);
}
/// <summary>
/// Generates a barcode image with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
/// </summary>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="type">Type of the barcode to be generated.</param>
/// <param name="config">The configuration of the barcode generator. This value can be null.</param>
/// <returns><see cref="MediaVisionSource"/> containing the generated barcode image.</returns>
/// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
/// -or-<br/>
/// <paramref name="type"/> is invalid.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains illegal characters.
/// </exception>
/// <exception cref="NotSupportedException">The feature is not supported.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
/// <since_tizen> 4 </since_tizen>
public static MediaVisionSource GenerateSource(string message, BarcodeType type,
BarcodeGenerationConfiguration config)
{
if (type == BarcodeType.QR)
{
throw new ArgumentException($"Invalid barcode type : {type}.");
}
return GenerateSource(config, message, type, NoneQrMode, NoneErrorCorrection, 0);
}
private static void GenerateImage(BarcodeGenerationConfiguration config,
string message, BarcodeType type, BarcodeImageConfiguration imageConfig,
int qrMode, int qrEcc, int qrVersion)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
if (imageConfig == null)
{
throw new ArgumentNullException(nameof(imageConfig));
}
ValidationUtil.ValidateEnum(typeof(BarcodeType), type, nameof(type));
InteropBarcode.GenerateImage(EngineConfiguration.GetHandle(config), message,
imageConfig.Width, imageConfig.Height, type, qrMode, qrEcc, qrVersion,
imageConfig.Path, imageConfig.Format).
Validate("Failed to generate image");
}
/// <summary>
/// Generates a QR image file with the specified message.
/// </summary>
/// <remarks>
/// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
/// because the text visibility is not supported in the QR code.
/// </remarks>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
/// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains information about the file to be generated.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="message"/> is null.<br/>
/// -or-<br/>
/// <paramref name="qrConfig"/> is null.<br/>
/// -or-<br/>
/// <paramref name="imageConfig"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
/// </exception>
/// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
/// <exception cref="NotSupportedException">The feature is not supported.</exception>
/// <seealso cref="QrMode"/>
/// <since_tizen> 4 </since_tizen>
public static void GenerateImage(string message, QrConfiguration qrConfig,
BarcodeImageConfiguration imageConfig)
{
GenerateImage(message, qrConfig, imageConfig, null);
}
/// <summary>
/// Generates a QR image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
/// </summary>
/// <remarks>
/// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
/// because the text visibility is not supported in the QR code.
/// </remarks>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
/// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
/// information about the file to be generated.</param>
/// <param name="config">The configuration of the barcode generator. This value can be null.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="message"/> is null.<br/>
/// -or-<br/>
/// <paramref name="qrConfig"/> is null.<br/>
/// -or-<br/>
/// <paramref name="imageConfig"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
/// </exception>
/// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
/// <exception cref="NotSupportedException">
/// The feature is not supported.<br/>
/// -or-<br/>
/// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
/// </exception>
/// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
/// <since_tizen> 4 </since_tizen>
public static void GenerateImage(string message, QrConfiguration qrConfig,
BarcodeImageConfiguration imageConfig, BarcodeGenerationConfiguration config)
{
if (qrConfig == null)
{
throw new ArgumentNullException(nameof(qrConfig));
}
if (config != null)
{
if (config.TextVisibility == Visibility.Visible)
{
throw new NotSupportedException("Text can't be visible in QR.");
}
}
GenerateImage(config, message, BarcodeType.QR, imageConfig, (int)qrConfig.Mode,
(int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
}
/// <summary>
/// Generates a barcode image file with the specified message.
/// </summary>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="type">Type of the barcode to be generated.</param>
/// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
/// information about the file to be generated.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="message"/> is null.<br/>
/// -or-<br/>
/// <paramref name="imageConfig"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
/// -or-<br/>
/// <paramref name="type"/> is invalid.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains illegal characters.
/// </exception>
/// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
/// <exception cref="NotSupportedException">The feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public static void GenerateImage(string message, BarcodeType type, BarcodeImageConfiguration imageConfig)
{
GenerateImage(message, type, imageConfig, null);
}
/// <summary>
/// Generates a barcode image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
/// </summary>
/// <param name="message">The message to be encoded in the barcode.</param>
/// <param name="type">Type of the barcode to be generated.</param>
/// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
/// information about the file to be generated.</param>
/// <param name="config">The configuration of the barcode generator. This value can be null.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="message"/> is null.<br/>
/// -or-<br/>
/// <paramref name="imageConfig"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="message"/> is too long.<br/>
/// -or-<br/>
/// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
/// -or-<br/>
/// <paramref name="type"/> is invalid.<br/>
/// -or-<br/>
/// <paramref name="message"/> contains illegal characters.
/// </exception>
/// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
/// <exception cref="NotSupportedException">The feature is not supported.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
/// <since_tizen> 4 </since_tizen>
public static void GenerateImage(string message,
BarcodeType type, BarcodeImageConfiguration imageConfig, BarcodeGenerationConfiguration config)
{
if (type == BarcodeType.QR)
{
throw new ArgumentException($"Invalid barcode type : {type}.");
}
GenerateImage(config, message, type, imageConfig, NoneQrMode, NoneErrorCorrection, 0);
}
}
}