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
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> 3</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 MediaVisionSource GenerateSource(BarcodeGenerationConfiguration config,
35 string message, BarcodeType type, int qrMode, int qrEcc, int qrVersion)
39 throw new ArgumentNullException(nameof(message));
42 ValidationUtil.ValidateEnum(typeof(BarcodeType), type);
44 MediaVisionSource source = new MediaVisionSource();
47 InteropBarcode.GenerateSource(EngineConfiguration.GetHandle(config),
48 message, type, qrMode, qrEcc, qrVersion, source.Handle).
49 Validate("Failed to generate source");
60 /// Generates a QR image with the specified message.
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
68 /// <paramref name="message"/> is null.
70 /// <exception cref="ArgumentException">
71 /// <paramref name="message"/> is too long.\n
73 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
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)
80 return GenerateSource(message, qrConfig, null);
84 /// Generates a QR image with the specified message with <see cref="BarcodeGenerationConfiguration"/>.
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>
91 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
92 /// because the text visibility is not supported in the QR code.
94 /// <exception cref="ArgumentNullException">
95 /// <paramref name="qrConfig"/> is null.\n
97 /// <paramref name="message"/> is null.
99 /// <exception cref="ArgumentException">
100 /// <paramref name="message"/> is too long.\n
102 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
104 /// <exception cref="NotSupportedException">
105 /// The feature is not supported.\n
107 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
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)
115 if (qrConfig == null)
117 throw new ArgumentNullException(nameof(qrConfig));
122 if (config.TextVisibility == Visibility.Visible)
124 throw new NotSupportedException("Text can't be visible in QR.");
128 return GenerateSource(config, message, BarcodeType.QR, (int)qrConfig.Mode,
129 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
133 /// Generates a barcode image with the specified message.
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
142 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.\n
144 /// <paramref name="type"/> is invalid.
146 /// <paramref name="message"/> contains illegal characters.
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)
152 return GenerateSource(message, type, null);
156 /// Generates a barcode image with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
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
166 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.
168 /// <paramref name="type"/> is invalid.
170 /// <paramref name="message"/> contains illegal characters.
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)
178 if (type == BarcodeType.QR)
180 throw new ArgumentException($"Invalid barcode type : {type}.");
183 return GenerateSource(config, message, type, NoneQrMode, NoneErrorCorrection, 0);
186 private static void GenerateImage(BarcodeGenerationConfiguration config,
187 string message, BarcodeType type, BarcodeImageConfiguration imageConfig,
188 int qrMode, int qrEcc, int qrVersion)
192 throw new ArgumentNullException(nameof(message));
195 if (imageConfig == null)
197 throw new ArgumentNullException(nameof(imageConfig));
200 ValidationUtil.ValidateEnum(typeof(BarcodeType), type);
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");
209 /// Generates a QR image file with the specified message.
212 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
213 /// because the text visibility is not supported in the QR code.
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
221 /// <paramref name="qrConfig"/> is null.\n
223 /// <paramref name="imageConfig"/> is null.
225 /// <exception cref="ArgumentException">
226 /// <paramref name="message"/> is too long.\n
228 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
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)
237 GenerateImage(message, qrConfig, imageConfig, null);
241 /// Generates a QR image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
244 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> must be <see cref="Visibility.Invisible"/>,
245 /// because the text visibility is not supported in the QR code.
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
255 /// <paramref name="qrConfig"/> is null.\n
257 /// <paramref name="imageConfig"/> is null.
259 /// <exception cref="ArgumentException">
260 /// <paramref name="message"/> is too long.\n
262 /// <paramref name="message"/> contains characters which are illegal by the <see cref="QrMode"/>.
264 /// <exception cref="UnauthorizedAccessException">No permission to write a file.</exception>
265 /// <exception cref="NotSupportedException">
266 /// The feature is not supported.\n
268 /// <see cref="BarcodeGenerationConfiguration.TextVisibility"/> is the <see cref="Visibility.Visible"/>.
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)
275 if (qrConfig == null)
277 throw new ArgumentNullException(nameof(qrConfig));
282 if (config.TextVisibility == Visibility.Visible)
284 throw new NotSupportedException("Text can't be visible in QR.");
288 GenerateImage(config, message, BarcodeType.QR, imageConfig, (int)qrConfig.Mode,
289 (int)qrConfig.ErrorCorrectionLevel, qrConfig.Version);
293 /// Generates a barcode image file with the specified message.
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
302 /// <paramref name="imageConfig"/> is null.
304 /// <exception cref="ArgumentException">
305 /// <paramref name="message"/> is too long.\n
307 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.
309 /// <paramref name="type"/> is invalid.
311 /// <paramref name="message"/> contains illegal characters.
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)
318 GenerateImage(message, type, imageConfig, null);
322 /// Generates a barcode image file with the specified message and <see cref="BarcodeGenerationConfiguration"/>.
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
332 /// <paramref name="imageConfig"/> is null.
334 /// <exception cref="ArgumentException">
335 /// <paramref name="message"/> is too long.\n
337 /// <paramref name="type"/> is <see cref="BarcodeType.QR"/>.
339 /// <paramref name="type"/> is invalid.
341 /// <paramref name="message"/> contains illegal characters.
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)
350 if (type == BarcodeType.QR)
352 throw new ArgumentException($"Invalid barcode type : {type}.");
354 GenerateImage(config, message, type, imageConfig, NoneQrMode, NoneErrorCorrection, 0);