[MediaVision] Fix Design QR bug (#5245)
[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.Vision
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> 4 </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 void SetDesignQrOptions(QrConfiguration qrConfig, BarcodeGenerationConfiguration config)
35         {
36             if (config != null && qrConfig != null)
37             {
38                 if (qrConfig.EmbedImagePath != null)
39                 {
40                     config.EmbedImagePath = qrConfig.EmbedImagePath;
41                 }
42
43                 config.DataShape = qrConfig.DataShape;
44                 config.FinderShape = qrConfig.FinderShape;
45             }
46         }
47
48         private static MediaVisionSource GenerateSource(BarcodeGenerationConfiguration config,
49             string message, BarcodeType type, int qrMode, int qrEcc, int qrVersion)
50         {
51             if (message == null)
52             {
53                 throw new ArgumentNullException(nameof(message));
54             }
55
56             ValidationUtil.ValidateEnum(typeof(BarcodeType), type, nameof(type));
57
58             MediaVisionSource source = new MediaVisionSource();
59             try
60             {
61                 InteropBarcode.GenerateSource(EngineConfiguration.GetHandle(config),
62                     message, type, qrMode, qrEcc, qrVersion, source.Handle).
63                     Validate("Failed to generate source");
64             }
65             catch (Exception)
66             {
67                 source.Dispose();
68                 throw;
69             }
70             return source;
71         }
72
73         /// <summary>
74         /// Generates a QR image with the specified message.
75         /// </summary>
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/>
82         ///     -or-<br/>
83         ///     <paramref name="message"/> is null.
84         /// </exception>
85         /// <exception cref="ArgumentException">
86         ///     <paramref name="message"/> is too long.<br/>
87         ///     -or-<br/>
88         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
89         ///     </exception>
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)
94         {
95             return GenerateSource(message, qrConfig, null);
96         }
97
98         /// <summary>
99         /// Generates a QR image with the specified message with <see cref="BarcodeGenerationConfiguration"/>.
100         /// </summary>
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>
106         /// <remarks>
107         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
108         ///     because the text visibility is not supported in the QR code.
109         /// </remarks>
110         /// <exception cref="ArgumentNullException">
111         ///     <paramref name="qrConfig"/> is null.<br/>
112         ///     -or-<br/>
113         ///     <paramref name="message"/> is null.
114         /// </exception>
115         /// <exception cref="ArgumentException">
116         ///     <paramref name="message"/> is too long.<br/>
117         ///     -or-<br/>
118         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
119         /// </exception>
120         /// <exception cref="NotSupportedException">
121         ///     The feature is not supported.<br/>
122         ///     -or-<br/>
123         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
124         /// </exception>
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)
130         {
131             if (qrConfig == null)
132             {
133                 throw new ArgumentNullException(nameof(qrConfig));
134             }
135
136             if (config != null)
137             {
138                 if (config.TextVisibility == Visibility.Visible)
139                 {
140                     throw new NotSupportedException("Text can't be visible in QR.");
141                 }
142             }
143             else
144             {
145                 if (qrConfig.DataShape != QrShape.Rectangular || qrConfig.FinderShape != QrShape.Rectangular ||
146                     qrConfig.EmbedImagePath != null)
147                 {
148                     config = new BarcodeGenerationConfiguration();
149                 }
150             }
151
152             SetDesignQrOptions(qrConfig, config);
153
154             return GenerateSource(config, message, BarcodeType.QR, (int)qrConfig.Mode,
155                 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
156         }
157
158         /// <summary>
159         /// Generates a barcode image with the specified message.
160         /// </summary>
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/>
167         ///     -or-<br/>
168         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
169         ///     -or-<br/>
170         ///     <paramref name="type"/> is invalid.<br/>
171         ///     -or-<br/>
172         ///     <paramref name="message"/> contains illegal characters.
173         /// </exception>
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)
177         {
178             return GenerateSource(message, type, null);
179         }
180
181         /// <summary>
182         /// Generates a barcode image with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
183         /// </summary>
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/>
192         ///     -or-<br/>
193         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
194         ///     -or-<br/>
195         ///     <paramref name="type"/> is invalid.<br/>
196         ///     -or-<br/>
197         ///     <paramref name="message"/> contains illegal characters.
198         /// </exception>
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)
204         {
205             if (type == BarcodeType.QR)
206             {
207                 throw new ArgumentException($"Invalid barcode type : {type}.");
208             }
209
210             return GenerateSource(config, message, type, NoneQrMode, NoneErrorCorrection, 0);
211         }
212
213         private static void GenerateImage(BarcodeGenerationConfiguration config,
214             string message, BarcodeType type, BarcodeImageConfiguration imageConfig,
215             int qrMode, int qrEcc, int qrVersion)
216         {
217             if (message == null)
218             {
219                 throw new ArgumentNullException(nameof(message));
220             }
221
222             if (imageConfig == null)
223             {
224                 throw new ArgumentNullException(nameof(imageConfig));
225             }
226
227             ValidationUtil.ValidateEnum(typeof(BarcodeType), type, nameof(type));
228
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");
233         }
234
235         /// <summary>
236         /// Generates a QR image file with the specified message.
237         /// </summary>
238         /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
239         /// <remarks>
240         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
241         ///     because the text visibility is not supported in the QR code.
242         /// </remarks>
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/>
248         ///     -or-<br/>
249         ///     <paramref name="qrConfig"/> is null.<br/>
250         ///     -or-<br/>
251         ///     <paramref name="imageConfig"/> is null.
252         /// </exception>
253         /// <exception cref="ArgumentException">
254         ///     <paramref name="message"/> is too long.<br/>
255         ///     -or-<br/>
256         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
257         /// </exception>
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)
264         {
265             GenerateImage(message, qrConfig, imageConfig, null);
266         }
267
268         /// <summary>
269         /// Generates a QR image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
270         /// </summary>
271         /// <feature>http://tizen.org/feature/vision.barcode_generation</feature>
272         /// <remarks>
273         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
274         ///     because the text visibility is not supported in the QR code.
275         /// </remarks>
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/>
283         ///     -or-<br/>
284         ///     <paramref name="qrConfig"/> is null.<br/>
285         ///     -or-<br/>
286         ///     <paramref name="imageConfig"/> is null.
287         /// </exception>
288         /// <exception cref="ArgumentException">
289         ///     <paramref name="message"/> is too long.<br/>
290         ///     -or-<br/>
291         ///     <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
292         /// </exception>
293         /// <exception cref="UnauthorizedAccessException">No permission to access a file.</exception>
294         /// <exception cref="NotSupportedException">
295         ///     The feature is not supported.<br/>
296         ///     -or-<br/>
297         ///     <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
298         /// </exception>
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)
303         {
304             if (qrConfig == null)
305             {
306                 throw new ArgumentNullException(nameof(qrConfig));
307             }
308
309             if (config != null)
310             {
311                 if (config.TextVisibility == Visibility.Visible)
312                 {
313                     throw new NotSupportedException("Text can't be visible in QR.");
314                 }
315             }
316             else
317             {
318                 if (qrConfig.DataShape != QrShape.Rectangular || qrConfig.FinderShape != QrShape.Rectangular ||
319                     qrConfig.EmbedImagePath != null)
320                 {
321                     config = new BarcodeGenerationConfiguration();
322                 }
323             }
324
325             SetDesignQrOptions(qrConfig, config);
326
327             GenerateImage(config, message, BarcodeType.QR, imageConfig, (int)qrConfig.Mode,
328                 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
329         }
330
331         /// <summary>
332         /// Generates a barcode image file with the specified message.
333         /// </summary>
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/>
341         ///     -or-<br/>
342         ///     <paramref name="imageConfig"/> is null.
343         /// </exception>
344         /// <exception cref="ArgumentException">
345         ///     <paramref name="message"/> is too long.<br/>
346         ///     -or-<br/>
347         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
348         ///     -or-<br/>
349         ///     <paramref name="type"/> is invalid.<br/>
350         ///     -or-<br/>
351         ///     <paramref name="message"/> contains illegal characters.
352         /// </exception>
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)
357         {
358             GenerateImage(message, type, imageConfig, null);
359         }
360
361         /// <summary>
362         /// Generates a barcode image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
363         /// </summary>
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/>
372         ///     -or-<br/>
373         ///     <paramref name="imageConfig"/> is null.
374         /// </exception>
375         /// <exception cref="ArgumentException">
376         ///     <paramref name="message"/> is too long.<br/>
377         ///     -or-<br/>
378         ///     <paramref name="type"/> is <see cref="BarcodeType.QR"/>.<br/>
379         ///     -or-<br/>
380         ///     <paramref name="type"/> is invalid.<br/>
381         ///     -or-<br/>
382         ///     <paramref name="message"/> contains illegal characters.
383         /// </exception>
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)
390         {
391             if (type == BarcodeType.QR)
392             {
393                 throw new ArgumentException($"Invalid barcode type : {type}.");
394             }
395             GenerateImage(config, message, type, imageConfig, NoneQrMode, NoneErrorCorrection, 0);
396         }
397     }
398 }