2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using InteropBarcode = Interop.MediaVision.BarcodeGenerator;
20 namespace Tizen.Multimedia.Vision
23 /// Provides the ability to generate barcodes and QR codes.
24 /// Different encoding types <see cref="QrMode"/>, error correction codes <see cref="ErrorCorrectionLevel"/>,
25 /// and code versions are supported for QRCodes.
27 /// <seealso cref="BarcodeGenerationConfiguration"/>
28 /// <since_tizen> 4 </since_tizen>
29 public static class BarcodeGenerator
31 private const int NoneErrorCorrection = (int)ErrorCorrectionLevel.High + 1;
32 private const int NoneQrMode = (int)QrMode.Utf8 + 1;
34 private static void SetDesignQrOptions(QrConfiguration qrConfig, BarcodeGenerationConfiguration config)
36 if (config != null && qrConfig != null)
38 if (qrConfig.EmbedImagePath != null)
40 config.EmbedImagePath = qrConfig.EmbedImagePath;
43 config.DataShape = qrConfig.DataShape;
44 config.FinderShape = qrConfig.FinderShape;
48 private static MediaVisionSource GenerateSource(BarcodeGenerationConfiguration config,
49 string message, BarcodeType type, int qrMode, int qrEcc, int qrVersion)
53 throw new ArgumentNullException(nameof(message));
56 ValidationUtil.ValidateEnum(typeof(BarcodeType), type, nameof(type));
58 MediaVisionSource source = new MediaVisionSource();
61 InteropBarcode.GenerateSource(EngineConfiguration.GetHandle(config),
62 message, type, qrMode, qrEcc, qrVersion, source.Handle).
63 Validate("Failed to generate source");
74 /// Generates a QR image with the specified message.
76 /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
77 /// <param name="message">The message to be encoded in the barcode.</param>
78 /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
79 /// <returns><see cref="MediaVisionSource"/> containing the generated QR image.</returns>
80 /// <exception cref="ArgumentNullException">
81 /// <paramref name="qrConfig"/> is null.<br/>
83 /// <paramref name="message"/> is null.
85 /// <exception cref="ArgumentException">
86 /// <paramref name="message"/> is too long.<br/>
88 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
90 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
91 /// <seealso cref="QrMode"/>
92 /// <since_tizen> 4 </since_tizen>
93 public static MediaVisionSource GenerateSource(string message, QrConfiguration qrConfig)
95 return GenerateSource(message, qrConfig, null);
99 /// Generates a QR image with the specified message with <see cref="BarcodeGenerationConfiguration"/>.
101 /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
102 /// <param name="message">The message to be encoded in the barcode.</param>
103 /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
104 /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
105 /// <returns><see cref="MediaVisionSource"/> containing the generated QR image.</returns>
107 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
108 /// because the text visibility is not supported in the QR code.
110 /// <exception cref="ArgumentNullException">
111 /// <paramref name="qrConfig"/> is null.<br/>
113 /// <paramref name="message"/> is null.
115 /// <exception cref="ArgumentException">
116 /// <paramref name="message"/> is too long.<br/>
118 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
120 /// <exception cref="NotSupportedException">
121 /// The feature is not supported.<br/>
123 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
125 /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
126 /// <seealso cref="QrMode"/>
127 /// <since_tizen> 4 </since_tizen>
128 public static MediaVisionSource GenerateSource(string message, QrConfiguration qrConfig,
129 BarcodeGenerationConfiguration config)
131 if (qrConfig == null)
133 throw new ArgumentNullException(nameof(qrConfig));
138 if (config.TextVisibility == Visibility.Visible)
140 throw new NotSupportedException("Text can't be visible in QR.");
145 if (qrConfig.DataShape != QrShape.Rectangular || qrConfig.FinderShape != QrShape.Rectangular ||
146 qrConfig.EmbedImagePath != null)
148 config = new BarcodeGenerationConfiguration();
152 SetDesignQrOptions(qrConfig, config);
154 return GenerateSource(config, message, BarcodeType.QR, (int)qrConfig.Mode,
155 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
159 /// Generates a barcode image with the specified message.
161 /// <param name="message">The message to be encoded in the barcode.</param>
162 /// <param name="type">Type of the barcode to be generated.</param>
163 /// <returns><see cref="MediaVisionSource"/> containing the generated barcode image.</returns>
164 /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
165 /// <exception cref="ArgumentException">
166 /// <paramref name="message"/> is too long.<br/>
168 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
170 /// <paramref name="type"/> is invalid.<br/>
172 /// <paramref name="message"/> contains illegal characters.
174 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
175 /// <since_tizen> 4 </since_tizen>
176 public static MediaVisionSource GenerateSource(string message, BarcodeType type)
178 return GenerateSource(message, type, null);
182 /// Generates a barcode image with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
184 /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
185 /// <param name="message">The message to be encoded in the barcode.</param>
186 /// <param name="type">Type of the barcode to be generated.</param>
187 /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
188 /// <returns><see cref="MediaVisionSource"/> containing the generated barcode image.</returns>
189 /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
190 /// <exception cref="ArgumentException">
191 /// <paramref name="message"/> is too long.<br/>
193 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
195 /// <paramref name="type"/> is invalid.<br/>
197 /// <paramref name="message"/> contains illegal characters.
199 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
200 /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
201 /// <since_tizen> 4 </since_tizen>
202 public static MediaVisionSource GenerateSource(string message, BarcodeType type,
203 BarcodeGenerationConfiguration config)
205 if (type == BarcodeType.QR)
207 throw new ArgumentException($"Invalid barcode type : {type}.");
210 return GenerateSource(config, message, type, NoneQrMode, NoneErrorCorrection, 0);
213 private static void GenerateImage(BarcodeGenerationConfiguration config,
214 string message, BarcodeType type, BarcodeImageConfiguration imageConfig,
215 int qrMode, int qrEcc, int qrVersion)
219 throw new ArgumentNullException(nameof(message));
222 if (imageConfig == null)
224 throw new ArgumentNullException(nameof(imageConfig));
227 ValidationUtil.ValidateEnum(typeof(BarcodeType), type, nameof(type));
229 InteropBarcode.GenerateImage(EngineConfiguration.GetHandle(config), message,
230 imageConfig.Width, imageConfig.Height, type, qrMode, qrEcc, qrVersion,
231 imageConfig.Path, imageConfig.Format).
232 Validate("Failed to generate image");
236 /// Generates a QR image file with the specified message.
238 /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
240 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
241 /// because the text visibility is not supported in the QR code.
243 /// <param name="message">The message to be encoded in the barcode.</param>
244 /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
245 /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains information about the file to be generated.</param>
246 /// <exception cref="ArgumentNullException">
247 /// <paramref name="message"/> is null.<br/>
249 /// <paramref name="qrConfig"/> is null.<br/>
251 /// <paramref name="imageConfig"/> is null.
253 /// <exception cref="ArgumentException">
254 /// <paramref name="message"/> is too long.<br/>
256 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
258 /// <exception cref="UnauthorizedAccessException">No permission to access a file.</exception>
259 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
260 /// <seealso cref="QrMode"/>
261 /// <since_tizen> 4 </since_tizen>
262 public static void GenerateImage(string message, QrConfiguration qrConfig,
263 BarcodeImageConfiguration imageConfig)
265 GenerateImage(message, qrConfig, imageConfig, null);
269 /// Generates a QR image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
271 /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
273 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
274 /// because the text visibility is not supported in the QR code.
276 /// <param name="message">The message to be encoded in the barcode.</param>
277 /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
278 /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
279 /// information about the file to be generated.</param>
280 /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
281 /// <exception cref="ArgumentNullException">
282 /// <paramref name="message"/> is null.<br/>
284 /// <paramref name="qrConfig"/> is null.<br/>
286 /// <paramref name="imageConfig"/> is null.
288 /// <exception cref="ArgumentException">
289 /// <paramref name="message"/> is too long.<br/>
291 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
293 /// <exception cref="UnauthorizedAccessException">No permission to access a file.</exception>
294 /// <exception cref="NotSupportedException">
295 /// The feature is not supported.<br/>
297 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
299 /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
300 /// <since_tizen> 4 </since_tizen>
301 public static void GenerateImage(string message, QrConfiguration qrConfig,
302 BarcodeImageConfiguration imageConfig, BarcodeGenerationConfiguration config)
304 if (qrConfig == null)
306 throw new ArgumentNullException(nameof(qrConfig));
311 if (config.TextVisibility == Visibility.Visible)
313 throw new NotSupportedException("Text can't be visible in QR.");
318 if (qrConfig.DataShape != QrShape.Rectangular || qrConfig.FinderShape != QrShape.Rectangular ||
319 qrConfig.EmbedImagePath != null)
321 config = new BarcodeGenerationConfiguration();
325 SetDesignQrOptions(qrConfig, config);
327 GenerateImage(config, message, BarcodeType.QR, imageConfig, (int)qrConfig.Mode,
328 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
332 /// Generates a barcode image file with the specified message.
334 /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
335 /// <param name="message">The message to be encoded in the barcode.</param>
336 /// <param name="type">Type of the barcode to be generated.</param>
337 /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
338 /// information about the file to be generated.</param>
339 /// <exception cref="ArgumentNullException">
340 /// <paramref name="message"/> is null.<br/>
342 /// <paramref name="imageConfig"/> is null.
344 /// <exception cref="ArgumentException">
345 /// <paramref name="message"/> is too long.<br/>
347 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
349 /// <paramref name="type"/> is invalid.<br/>
351 /// <paramref name="message"/> contains illegal characters.
353 /// <exception cref="UnauthorizedAccessException">No permission to access a file.</exception>
354 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
355 /// <since_tizen> 4 </since_tizen>
356 public static void GenerateImage(string message, BarcodeType type, BarcodeImageConfiguration imageConfig)
358 GenerateImage(message, type, imageConfig, null);
362 /// Generates a barcode image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
364 /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
365 /// <param name="message">The message to be encoded in the barcode.</param>
366 /// <param name="type">Type of the barcode to be generated.</param>
367 /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
368 /// information about the file to be generated.</param>
369 /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
370 /// <exception cref="ArgumentNullException">
371 /// <paramref name="message"/> is null.<br/>
373 /// <paramref name="imageConfig"/> is null.
375 /// <exception cref="ArgumentException">
376 /// <paramref name="message"/> is too long.<br/>
378 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
380 /// <paramref name="type"/> is invalid.<br/>
382 /// <paramref name="message"/> contains illegal characters.
384 /// <exception cref="UnauthorizedAccessException">No permission to access a file.</exception>
385 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
386 /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
387 /// <since_tizen> 4 </since_tizen>
388 public static void GenerateImage(string message,
389 BarcodeType type, BarcodeImageConfiguration imageConfig, BarcodeGenerationConfiguration config)
391 if (type == BarcodeType.QR)
393 throw new ArgumentException($"Invalid barcode type : {type}.");
395 GenerateImage(config, message, type, imageConfig, NoneQrMode, NoneErrorCorrection, 0);