Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / BarcodeGenerator.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using InteropBarcode = Interop.MediaVision.BarcodeGenerator;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
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.
26     /// </summary>
27     /// <seealso cref="BarcodeGenerationConfiguration"/>
28     /// <since_tizen> 3</since_tizen>
29     public static class BarcodeGenerator
30     {
31         private const int NoneErrorCorrection = (int)ErrorCorrectionLevel.High + 1;
32         private const int NoneQrMode = (int)QrMode.Utf8 + 1;
33
34         private static MediaVisionSource GenerateSource(BarcodeGenerationConfiguration config,
35             string message, BarcodeType type, int qrMode, int qrEcc, int qrVersion)
36         {
37             if (message == null)
38             {
39                 throw new ArgumentNullException(nameof(message));
40             }
41
42             ValidationUtil.ValidateEnum(typeof(BarcodeType), type);
43
44             MediaVisionSource source = new MediaVisionSource();
45             try
46             {
47                 InteropBarcode.GenerateSource(EngineConfiguration.GetHandle(config),
48                     message, type, qrMode, qrEcc, qrVersion, source.Handle).
49                     Validate("Failed to generate source");
50             }
51             catch (Exception)
52             {
53                 source.Dispose();
54                 throw;
55             }
56             return source;
57         }
58
59         /// <summary>
60         /// Generates a QR image with the specified message.
61         /// </summary>
62         /// <param name="message">The message to be encoded in the barcode.</param>
63         /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
64         /// <returns><see cref="MediaVisionSource"/> containing the generated QR image.</returns>
65         /// <exception cref="ArgumentNullException">
66         ///     <paramref name="qrConfig"/> is null.\n
67         ///     -or-\n
68         ///     <paramref name="message"/> is null.
69         /// </exception>
70         /// <exception cref="ArgumentException">
71         ///     <paramref name="message"/> is too long.\n
72         ///     -or-\n
73         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
74         ///     </exception>
75         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
76         /// <seealso cref="QrMode"/>
77         /// <since_tizen> 3</since_tizen>
78         public static MediaVisionSource GenerateSource(string message, QrConfiguration qrConfig)
79         {
80             return GenerateSource(message, qrConfig, null);
81         }
82
83         /// <summary>
84         /// Generates a QR image with the specified message with <see cref="BarcodeGenerationConfiguration"/>.
85         /// </summary>
86         /// <param name="message">The message to be encoded in the barcode.</param>
87         /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
88         /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
89         /// <returns><see cref="MediaVisionSource"/> containing the generated QR image.</returns>
90         /// <remarks>
91         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
92         ///     because the text visibility is not supported in the QR code.
93         /// </remarks>
94         /// <exception cref="ArgumentNullException">
95         ///     <paramref name="qrConfig"/> is null.\n
96         ///     -or-\n
97         ///     <paramref name="message"/> is null.
98         /// </exception>
99         /// <exception cref="ArgumentException">
100         ///     <paramref name="message"/> is too long.\n
101         ///     -or-\n
102         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
103         /// </exception>
104         /// <exception cref="NotSupportedException">
105         ///     The feature is not supported.\n
106         ///     -or-\n
107         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
108         /// </exception>
109         /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
110         /// <seealso cref="QrMode"/>
111         /// <since_tizen> 3</since_tizen>
112         public static MediaVisionSource GenerateSource(string message, QrConfiguration qrConfig,
113             BarcodeGenerationConfiguration config)
114         {
115             if (qrConfig == null)
116             {
117                 throw new ArgumentNullException(nameof(qrConfig));
118             }
119
120             if (config != null)
121             {
122                 if (config.TextVisibility == Visibility.Visible)
123                 {
124                     throw new NotSupportedException("Text can't be visible in QR.");
125                 }
126             }
127
128             return GenerateSource(config, message, BarcodeType.QR, (int)qrConfig.Mode,
129                 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
130         }
131
132         /// <summary>
133         /// Generates a barcode image with the specified message.
134         /// </summary>
135         /// <param name="message">The message to be encoded in the barcode.</param>
136         /// <param name="type">Type of the barcode to be generated.</param>
137         /// <returns><see cref="MediaVisionSource"/> containing the generated barcode image.</returns>
138         /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
139         /// <exception cref="ArgumentException">
140         ///     <paramref name="message"/> is too long.\n
141         ///     -or-\n
142         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.\n
143         ///     -or-\n
144         ///     <paramref name="type"/> is invalid.
145         ///     -or-\n
146         ///     <paramref name="message"/> contains illegal characters.
147         /// </exception>
148         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
149         /// <since_tizen> 3</since_tizen>
150         public static MediaVisionSource GenerateSource(string message, BarcodeType type)
151         {
152             return GenerateSource(message, type, null);
153         }
154
155         /// <summary>
156         /// Generates a barcode image with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
157         /// </summary>
158         /// <param name="message">The message to be encoded in the barcode.</param>
159         /// <param name="type">Type of the barcode to be generated.</param>
160         /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
161         /// <returns><see cref="MediaVisionSource"/> containing the generated barcode image.</returns>
162         /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
163         /// <exception cref="ArgumentException">
164         ///     <paramref name="message"/> is too long.\n
165         ///     -or-\n
166         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.
167         ///     -or-\n
168         ///     <paramref name="type"/> is invalid.
169         ///     -or-\n
170         ///     <paramref name="message"/> contains illegal characters.
171         /// </exception>
172         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
173         /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
174         /// <since_tizen> 3</since_tizen>
175         public static MediaVisionSource GenerateSource(string message, BarcodeType type,
176             BarcodeGenerationConfiguration config)
177         {
178             if (type == BarcodeType.QR)
179             {
180                 throw new ArgumentException($"Invalid barcode type : {type}.");
181             }
182
183             return GenerateSource(config, message, type, NoneQrMode, NoneErrorCorrection, 0);
184         }
185
186         private static void GenerateImage(BarcodeGenerationConfiguration config,
187             string message, BarcodeType type, BarcodeImageConfiguration imageConfig,
188             int qrMode, int qrEcc, int qrVersion)
189         {
190             if (message == null)
191             {
192                 throw new ArgumentNullException(nameof(message));
193             }
194
195             if (imageConfig == null)
196             {
197                 throw new ArgumentNullException(nameof(imageConfig));
198             }
199
200             ValidationUtil.ValidateEnum(typeof(BarcodeType), type);
201
202             InteropBarcode.GenerateImage(EngineConfiguration.GetHandle(config), message,
203                 imageConfig.Width, imageConfig.Height, type, qrMode, qrEcc, qrVersion,
204                 imageConfig.Path, imageConfig.Format).
205                 Validate("Failed to generate image");
206         }
207
208         /// <summary>
209         /// Generates a QR image file with the specified message.
210         /// </summary>
211         /// <remarks>
212         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
213         ///     because the text visibility is not supported in the QR code.
214         /// </remarks>
215         /// <param name="message">The message to be encoded in the barcode.</param>
216         /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
217         /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains information about the file to be generated.</param>
218         /// <exception cref="ArgumentNullException">
219         ///     <paramref name="message"/> is null.\n
220         ///     -or-\n
221         ///     <paramref name="qrConfig"/> is null.\n
222         ///     -or-\n
223         ///     <paramref name="imageConfig"/> is null.
224         /// </exception>
225         /// <exception cref="ArgumentException">
226         ///     <paramref name="message"/> is too long.\n
227         ///     -or-\n
228         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
229         /// </exception>
230         /// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
231         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
232         /// <seealso cref="QrMode"/>
233         /// <since_tizen> 3</since_tizen>
234         public static void GenerateImage(string message, QrConfiguration qrConfig,
235             BarcodeImageConfiguration imageConfig)
236         {
237             GenerateImage(message, qrConfig, imageConfig, null);
238         }
239
240         /// <summary>
241         /// Generates a QR image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
242         /// </summary>
243         /// <remarks>
244         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
245         ///     because the text visibility is not supported in the QR code.
246         /// </remarks>
247         /// <param name="message">The message to be encoded in the barcode.</param>
248         /// <param name="qrConfig">The <see cref="QrConfiguration"/> instance.</param>
249         /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
250         ///     information about the file to be generated.</param>
251         /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
252         /// <exception cref="ArgumentNullException">
253         ///     <paramref name="message"/> is null.\n
254         ///     -or-\n
255         ///     <paramref name="qrConfig"/> is null.\n
256         ///     -or-\n
257         ///     <paramref name="imageConfig"/> is null.
258         /// </exception>
259         /// <exception cref="ArgumentException">
260         ///     <paramref name="message"/> is too long.\n
261         ///     -or-\n
262         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
263         /// </exception>
264         /// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
265         /// <exception cref="NotSupportedException">
266         ///     The feature is not supported.\n
267         ///     -or-\n
268         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
269         /// </exception>
270         /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
271         /// <since_tizen> 3</since_tizen>
272         public static void GenerateImage(string message, QrConfiguration qrConfig,
273             BarcodeImageConfiguration imageConfig, BarcodeGenerationConfiguration config)
274         {
275             if (qrConfig == null)
276             {
277                 throw new ArgumentNullException(nameof(qrConfig));
278             }
279
280             if (config != null)
281             {
282                 if (config.TextVisibility == Visibility.Visible)
283                 {
284                     throw new NotSupportedException("Text can't be visible in QR.");
285                 }
286             }
287
288             GenerateImage(config, message, BarcodeType.QR, imageConfig, (int)qrConfig.Mode,
289                 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
290         }
291
292         /// <summary>
293         /// Generates a barcode image file with the specified message.
294         /// </summary>
295         /// <param name="message">The message to be encoded in the barcode.</param>
296         /// <param name="type">Type of the barcode to be generated.</param>
297         /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
298         ///     information about the file to be generated.</param>
299         /// <exception cref="ArgumentNullException">
300         ///     <paramref name="message"/> is null.\n
301         ///     -or-\n
302         ///     <paramref name="imageConfig"/> is null.
303         /// </exception>
304         /// <exception cref="ArgumentException">
305         ///     <paramref name="message"/> is too long.\n
306         ///     -or-\n
307         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.
308         ///     -or-\n
309         ///     <paramref name="type"/> is invalid.
310         ///     -or-\n
311         ///     <paramref name="message"/> contains illegal characters.
312         /// </exception>
313         /// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
314         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
315         /// <since_tizen> 3</since_tizen>
316         public static void GenerateImage(string message, BarcodeType type, BarcodeImageConfiguration imageConfig)
317         {
318             GenerateImage(message, type, imageConfig, null);
319         }
320
321         /// <summary>
322         /// Generates a barcode image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
323         /// </summary>
324         /// <param name="message">The message to be encoded in the barcode.</param>
325         /// <param name="type">Type of the barcode to be generated.</param>
326         /// <param name="imageConfig">The <see cref="BarcodeImageConfiguration"/> that contains
327         ///     information about the file to be generated.</param>
328         /// <param name="config">The configuration of the barcode generator. This value can be null.</param>
329         /// <exception cref="ArgumentNullException">
330         ///     <paramref name="message"/> is null.\n
331         ///     -or-\n
332         ///     <paramref name="imageConfig"/> is null.
333         /// </exception>
334         /// <exception cref="ArgumentException">
335         ///     <paramref name="message"/> is too long.\n
336         ///     -or-\n
337         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.
338         ///     -or-\n
339         ///     <paramref name="type"/> is invalid.
340         ///     -or-\n
341         ///     <paramref name="message"/> contains illegal characters.
342         /// </exception>
343         /// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
344         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
345         /// <exception cref="ObjectDisposedException"><paramref name="config"/> already has been disposed of.</exception>
346         /// <since_tizen> 3</since_tizen>
347         public static void GenerateImage(string message,
348             BarcodeType type, BarcodeImageConfiguration imageConfig, BarcodeGenerationConfiguration config)
349         {
350             if (type == BarcodeType.QR)
351             {
352                 throw new ArgumentException($"Invalid barcode type : {type}.");
353             }
354             GenerateImage(config, message, type, imageConfig, NoneQrMode, NoneErrorCorrection, 0);
355         }
356     }
357 }