[MediaVision] Applied coding rules
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / 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
19 namespace Tizen.Multimedia
20 {
21     /// <summary>
22     /// This class contains the Media Vision barcode generate API.\n
23     /// These APIs can be used for generating the barcodes and QR codes.
24     /// Different encoding types <see cref="QrMode"/> , error correction codes <see cref="ErrorCorrectionLevel"/>  and code versions are supported for QRCodes.
25     /// </summary>
26     public static class BarcodeGenerator
27     {
28         /// <summary>
29         /// Generates <see cref="MediaVisionSource"/> with barcode image.
30         /// </summary>
31         /// <remarks>
32         /// If the text attribute of engine configuration is set to <see cref="TextAttribute.Visible"/> , InvalidOperationException will be thrown when type is <see cref="BarcodeType.QR"/>
33         /// </remarks>
34         /// <param name="config">The configuration of the bar code generator engine</param>
35         /// <param name="message">The message to be encoded in the barcode</param>
36         /// <param name="type">Type of the barcode to be generated</param>
37         /// <param name="qrConfig">The QrConfig object - required for QR codes only</param>
38         /// <returns>
39         /// <param name="source">The media vision source object which will be used to fill by the buffer with generated image</param>
40         /// </returns>
41         /// <code>
42         /// 
43         /// </code>
44         public static MediaVisionSource GenerateSource(BarcodeGeneratorEngineConfiguration config, string message, BarcodeType type, QrConfiguration qrConfig = null)
45         {
46             if (string.IsNullOrEmpty(message) ||
47                 type == BarcodeType.Undefined ||
48                 (type == BarcodeType.QR &&
49                 (qrConfig == null ||
50                 qrConfig.ErrorCorrectionLevel == ErrorCorrectionLevel.Unavailable ||
51                 qrConfig.Mode == QrMode.Unavailable ||
52                 qrConfig.Version < 1 ||
53                 qrConfig.Version > 40)))
54             {
55                 throw new ArgumentException("Invalid parameter");
56             }
57
58             MediaVisionSource source = new MediaVisionSource();
59             int ret = (type == BarcodeType.QR) ?
60                         Interop.MediaVision.BarCodeGenerator.GenerateSource(config._engineHandle, message, type, qrConfig.Mode, qrConfig.ErrorCorrectionLevel, qrConfig.Version, source._sourceHandle) :
61                         Interop.MediaVision.BarCodeGenerator.GenerateSource(config._engineHandle, message, type, QrMode.Unavailable, ErrorCorrectionLevel.Unavailable, 0, source._sourceHandle);
62             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to generate source");
63             return source;
64         }
65
66         /// <summary>
67         /// Generates image file with barcode.
68         /// </summary>
69         /// <remarks>
70         /// If the text attribute of engine configuration is set to <see cref="TextAttribute.Visible"/> ,
71         /// InvalidOperationException will be thrown when type is <see cref="BarcodeType.QR"/>\n
72         /// </remarks>
73         /// <param name="config">The configuration of the bar code generator engine</param>
74         /// <param name="message">The message to be encoded in the barcode</param>
75         /// <param name="type">Type of the barcode to be generated</param>
76         /// <param name="imageConfig">The BarcodeImageConfig object that contains information about the file to be generated</param>
77         /// <param name="qrConfig">The QrConfig object - required for QR codes only</param>
78         /// <code>
79         /// 
80         /// </code>
81         public static void GenerateImage(BarcodeGeneratorEngineConfiguration config, string message, BarcodeType type, BarcodeImageConfiguration imageConfig, QrConfiguration qrConfig = null)
82         {
83             if (string.IsNullOrEmpty(message) ||
84                 imageConfig == null ||
85                 string.IsNullOrEmpty(imageConfig.Path) ||
86                 type == BarcodeType.Undefined ||
87                 (type == BarcodeType.QR &&
88                 (qrConfig == null ||
89                 qrConfig.ErrorCorrectionLevel == ErrorCorrectionLevel.Unavailable ||
90                 qrConfig.Mode == QrMode.Unavailable ||
91                 qrConfig.Version < 1 ||
92                 qrConfig.Version > 40)))
93             {
94                 throw new ArgumentException("Invalid parameter");
95             }
96
97             int ret = (type == BarcodeType.QR) ?
98                         Interop.MediaVision.BarCodeGenerator.GenerateImage(config._engineHandle,
99                                                                             message,
100                                                                             imageConfig.Width,
101                                                                             imageConfig.Height,
102                                                                             type,
103                                                                             qrConfig.Mode,
104                                                                             qrConfig.ErrorCorrectionLevel,
105                                                                             qrConfig.Version,
106                                                                             imageConfig.Path,
107                                                                             imageConfig.Format) :
108                         Interop.MediaVision.BarCodeGenerator.GenerateImage(config._engineHandle,
109                                                                             message,
110                                                                             imageConfig.Width,
111                                                                             imageConfig.Height,
112                                                                             type,
113                                                                             QrMode.Unavailable,
114                                                                             ErrorCorrectionLevel.Unavailable,
115                                                                             0,
116                                                                             imageConfig.Path,
117                                                                             imageConfig.Format);
118             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to generate image");
119         }
120     }
121 }