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 System.Collections.Generic;
19 using System.Threading.Tasks;
20 using InteropBarcode = Interop.MediaVision.BarcodeDetector;
21 using Unmanaged = Interop.MediaVision;
23 namespace Tizen.Multimedia.Vision
26 /// Provides the ability to detect barcodes on image sources.
28 /// <since_tizen> 3 </since_tizen>
29 public static class BarcodeDetector
32 /// Detects barcodes on the source and reads the message from it.
34 /// <param name="source">The <see cref="MediaVisionSource"/> instance.</param>
35 /// <param name="roi">Region of interest - rectangular area on the source which will be used for
36 /// barcode detection. Note that roi should be inside area on the source.</param>
37 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
38 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
39 /// <exception cref="ObjectDisposedException"><paramref name="source"/> already has been disposed of.</exception>
40 /// <returns>A task that represents the asynchronous detect operation.</returns>
41 /// <seealso cref="Barcode"/>
42 /// <since_tizen> 3</since_tizen>
43 public static async Task<IEnumerable<Barcode>> DetectAsync(MediaVisionSource source,
46 return await DetectAsync(source, roi, null);
50 /// Detects barcodes on the source and reads the message from it with <see cref="BarcodeDetectionConfiguration"/>.
52 /// <param name="source">The <see cref="MediaVisionSource"/> instance.</param>
53 /// <param name="roi">Region of interest - rectangular area on the source which will be used for
54 /// barcode detection. Note that roi should be inside area on the source.</param>
55 /// <param name="config">The configuration of the barcode detector. This value can be null.</param>
56 /// <returns>A task that represents the asynchronous detect operation.</returns>
57 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
58 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
59 /// <exception cref="ObjectDisposedException">
60 /// <paramref name="source"/> already has been disposed of.<br/>
62 /// <paramref name="config"/> already has been disposed of.
64 /// <seealso cref="Barcode"/>
65 /// <since_tizen> 3</since_tizen>
66 public static async Task<IEnumerable<Barcode>> DetectAsync(MediaVisionSource source,
67 Rectangle roi, BarcodeDetectionConfiguration config)
71 throw new ArgumentNullException(nameof(source));
74 var tcs = new TaskCompletionSource<IEnumerable<Barcode>>();
76 using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
78 InteropBarcode.Detect(source.Handle, EngineConfiguration.GetHandle(config),
79 roi.ToMarshalable(), cb.Target).Validate("Failed to detect barcode.");
81 return await tcs.Task;
85 private static Barcode[] CreateBarcodes(Unmanaged.Quadrangle[] locations, string[] messages,
86 BarcodeType[] types, int numberOfBarcodes)
88 Barcode[] barcodes = new Barcode[numberOfBarcodes];
90 for (int i = 0; i < numberOfBarcodes; i++)
92 barcodes[i] = new Barcode(locations[i].ToApiStruct(), messages[i], types[i]);
94 Log.Info(MediaVisionLog.Tag, barcodes[i].ToString());
100 private static InteropBarcode.DetectedCallback GetCallback(TaskCompletionSource<IEnumerable<Barcode>> tcs)
102 return (IntPtr mvSource, IntPtr engineCfg, Unmanaged.Quadrangle[] locations, string[] messages,
103 BarcodeType[] types, int numberOfBarcodes, IntPtr userData) =>
105 Log.Info(MediaVisionLog.Tag, $"Barcodes detected, count : {numberOfBarcodes}");
109 tcs.TrySetResult(CreateBarcodes(locations, messages, types, numberOfBarcodes));
113 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to handle barcode detection callback", e);
114 tcs.TrySetException(e);