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.Runtime.InteropServices;
20 using System.Threading.Tasks;
21 using static Interop.MediaVision;
23 namespace Tizen.Multimedia
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.
29 public static class BarcodeDetector
32 /// Detects barcode(s) on source and reads message from it.
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>
41 public static async Task<List<Barcode>> DetectAsync(MediaVisionSource source, BarcodeDetectorEngineConfiguration config, Rectangle roi)
43 TaskCompletionSource<List<Barcode>> tcsBarcodeList = new TaskCompletionSource<List<Barcode>>();
44 Interop.MediaVision.Rectangle rectangle = new Interop.MediaVision.Rectangle()
52 // Define native callback
53 Interop.MediaVision.BarCodeDetector.MvBarcodeDetectedCallback detectedCb = (IntPtr mvSource, IntPtr engineCfg, IntPtr barcodeLocations, IntPtr messages, IntPtr types, int numberOfBarcodes, IntPtr userData) =>
57 Log.Info(MediaVisionLog.Tag, String.Format("Barcodes detected, count : {0}", numberOfBarcodes));
58 List<Barcode> barcodes = new List<Barcode>();
59 if (numberOfBarcodes > 0)
61 IntPtr[] msgPtr = new IntPtr[numberOfBarcodes];
62 Marshal.Copy(messages, msgPtr, 0, numberOfBarcodes);
64 // Prepare list of barcodes
65 for (int i = 0; i < numberOfBarcodes; i++)
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()
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)
80 Log.Info(MediaVisionLog.Tag, String.Format("Location : {0}, Message : {1}, Type : {2}", quadrangle.ToString(), message, type));
81 Barcode barcode = new Barcode()
83 Location = quadrangle,
87 barcodes.Add(barcode);
88 barcodeLocations = IntPtr.Add(barcodeLocations, sizeof(int) * 8);
89 types = IntPtr.Add(barcodeLocations, sizeof(BarcodeType));
93 if (!tcsBarcodeList.TrySetResult(barcodes))
95 Log.Info(MediaVisionLog.Tag, "Failed to set result");
96 tcsBarcodeList.TrySetException(new InvalidOperationException("Failed to set result"));
101 Log.Info(MediaVisionLog.Tag, "exception :" + ex.ToString());
102 tcsBarcodeList.TrySetException(ex);
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;