[MediaVision] Applied coding rules
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaVision / BarcodeDetector.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 System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
21 using static Interop.MediaVision;
22
23 namespace Tizen.Multimedia
24 {
25     /// <summary>
26     /// This class contains the Media Vision barcode detect API.\n
27     /// These APIs can be used for detecting barcodes on image sources, reading encoded messages, getting barcode types.
28     /// </summary>
29     public static class BarcodeDetector
30     {
31         /// <summary>
32         /// Detects barcode(s) on source and reads message from it.
33         /// </summary>
34         /// <param name="source">The media vision source object</param>
35         /// <param name="config">The configuration of the barcode detector engine </param>
36         /// <param name="roi">Region of interest - rectangular area on the source which will be used for barcode detection Note that roi should be inside area on the source.</param>
37         /// <returns>Returns list of barcode detected asynchronously</returns>
38         /// <code>
39         ///
40         /// </code>
41         public static async Task<List<Barcode>> DetectAsync(MediaVisionSource source, BarcodeDetectorEngineConfiguration config, Rectangle roi)
42         {
43             TaskCompletionSource<List<Barcode>> tcsBarcodeList = new TaskCompletionSource<List<Barcode>>();
44             Interop.MediaVision.Rectangle rectangle = new Interop.MediaVision.Rectangle()
45             {
46                 x = roi.Point.X,
47                 y = roi.Point.Y,
48                 width = roi.Width,
49                 height = roi.Height
50             };
51
52             // Define native callback
53             Interop.MediaVision.BarCodeDetector.MvBarcodeDetectedCallback detectedCb = (IntPtr mvSource, IntPtr engineCfg, IntPtr barcodeLocations, IntPtr messages, IntPtr types, int numberOfBarcodes, IntPtr userData) =>
54             {
55                 try
56                 {
57                     Log.Info(MediaVisionLog.Tag, String.Format("Barcodes detected, count : {0}", numberOfBarcodes));
58                     List<Barcode> barcodes = new List<Barcode>();
59                     if (numberOfBarcodes > 0)
60                     {
61                         IntPtr[] msgPtr = new IntPtr[numberOfBarcodes];
62                         Marshal.Copy(messages, msgPtr, 0, numberOfBarcodes);
63
64                         // Prepare list of barcodes
65                         for (int i = 0; i < numberOfBarcodes; i++)
66                         {
67                             Interop.MediaVision.Quadrangle location = (Interop.MediaVision.Quadrangle)Marshal.PtrToStructure(barcodeLocations, typeof(Interop.MediaVision.Quadrangle));
68                             string message = Marshal.PtrToStringAnsi(msgPtr[i]);
69                             BarcodeType type = (BarcodeType)Marshal.ReadInt32(types);
70                             Quadrangle quadrangle = new Quadrangle()
71                             {
72                                 Points = new Point[4]
73                                 {
74                                     new Point(location.x1, location.y1),
75                                     new Point(location.x2, location.y2),
76                                     new Point(location.x3, location.y3),
77                                     new Point(location.x4, location.y4)
78                                 }
79                             };
80                             Log.Info(MediaVisionLog.Tag, String.Format("Location : {0}, Message : {1}, Type : {2}", quadrangle.ToString(), message, type));
81                             Barcode barcode = new Barcode()
82                             {
83                                 Location = quadrangle,
84                                 Message = message,
85                                 Type = type
86                             };
87                             barcodes.Add(barcode);
88                             barcodeLocations = IntPtr.Add(barcodeLocations, sizeof(int) * 8);
89                             types = IntPtr.Add(barcodeLocations, sizeof(BarcodeType));
90                         }
91                     }
92
93                     if (!tcsBarcodeList.TrySetResult(barcodes))
94                     {
95                         Log.Info(MediaVisionLog.Tag, "Failed to set result");
96                         tcsBarcodeList.TrySetException(new InvalidOperationException("Failed to set result"));
97                     }
98                 }
99                 catch (Exception ex)
100                 {
101                     Log.Info(MediaVisionLog.Tag, "exception :" + ex.ToString());
102                     tcsBarcodeList.TrySetException(ex);
103                 }
104             };
105             int ret = Interop.MediaVision.BarCodeDetector.Detect(source._sourceHandle, config._engineHandle, rectangle, detectedCb, IntPtr.Zero);
106             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to detect barcode.");
107             return await tcsBarcodeList.Task;
108         }
109     }
110 }