/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using InteropImage = Interop.MediaVision.Image;
namespace Tizen.Multimedia
{
///
/// Provides the ability to track images on image sources.
///
/// 3
public static class ImageTracker
{
///
/// Tracks the given image tracking model on the current frame.
///
/// The current image of sequence where image tracking model will be tracked.
/// The image tracking model which processed as target of tracking.
/// A task that represents the asynchronous tracking operation.
///
/// is null.\n
/// -or-\n
/// is null.
///
/// The feature is not supported.
///
/// has already been disposed of.\n
/// -or-\n
/// has already been disposed of.
///
/// has no target.
///
/// 3
public static async Task TrackAsync(MediaVisionSource source,
ImageTrackingModel trackingModel)
{
return await TrackAsync(source, trackingModel, null);
}
///
/// Tracks the given image tracking model on the current frame and .
///
/// The current image of sequence where image tracking model will be tracked.
/// The image tracking model which processed as target of tracking.
/// The configuration used for tracking. This value can be null.
/// A task that represents the asynchronous tracking operation.
///
/// is null.\n
/// -or-\n
/// is null.
///
/// The feature is not supported.
///
/// has already been disposed of.\n
/// -or-\n
/// has already been disposed of.\n
/// -or-\n
/// has already been disposed of.
///
/// has no target.
///
/// 3
public static async Task TrackAsync(MediaVisionSource source,
ImageTrackingModel trackingModel, ImageTrackingConfiguration config)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (trackingModel == null)
{
throw new ArgumentNullException(nameof(trackingModel));
}
TaskCompletionSource tcs = new TaskCompletionSource();
using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
{
InteropImage.Track(source.Handle, trackingModel.Handle, EngineConfiguration.GetHandle(config),
cb.Target).Validate("Failed to perform image tracking.");
return await tcs.Task;
}
}
private static InteropImage.TrackedCallback GetCallback(TaskCompletionSource tcs)
{
return (IntPtr sourceHandle, IntPtr imageTrackingModelHandle, IntPtr engineCfgHandle, IntPtr locationPtr, IntPtr _) =>
{
try
{
Quadrangle region = null;
if (locationPtr != IntPtr.Zero)
{
region = Marshal.PtrToStructure(locationPtr).ToApiStruct();
}
Log.Info(MediaVisionLog.Tag, $"Image tracked, region : {region}");
if (!tcs.TrySetResult(region))
{
Log.Info(MediaVisionLog.Tag, "Failed to set track result");
}
}
catch (Exception e)
{
MultimediaLog.Error(MediaVisionLog.Tag, "Failed to handle track result", e);
tcs.TrySetException(e);
}
};
}
}
}