/*
* 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.Collections.Generic;
using System.Threading.Tasks;
using InteropBarcode = Interop.MediaVision.BarcodeDetector;
using Unmanaged = Interop.MediaVision;
namespace Tizen.Multimedia.Vision
{
///
/// Provides the ability to detect barcodes on image sources.
///
/// 3
public static class BarcodeDetector
{
///
/// Detects barcodes on the source and reads the message from it.
///
/// The instance.
/// 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.
/// is null.
/// The feature is not supported.
/// already has been disposed of.
/// A task that represents the asynchronous detect operation.
///
/// 3
public static async Task> DetectAsync(MediaVisionSource source,
Rectangle roi)
{
return await DetectAsync(source, roi, null);
}
///
/// Detects barcodes on the source and reads the message from it with .
///
/// The instance.
/// 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.
/// The configuration of the barcode detector. This value can be null.
/// A task that represents the asynchronous detect operation.
/// is null.
/// The feature is not supported.
///
/// already has been disposed of.\n
/// -or-\n
/// already has been disposed of.
///
///
/// 3
public static async Task> DetectAsync(MediaVisionSource source,
Rectangle roi, BarcodeDetectionConfiguration config)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
var tcs = new TaskCompletionSource>();
using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
{
InteropBarcode.Detect(source.Handle, EngineConfiguration.GetHandle(config),
roi.ToMarshalable(), cb.Target).Validate("Failed to detect barcode.");
return await tcs.Task;
}
}
private static Barcode[] CreateBarcodes(Unmanaged.Quadrangle[] locations, string[] messages,
BarcodeType[] types, int numberOfBarcodes)
{
Barcode[] barcodes = new Barcode[numberOfBarcodes];
for (int i = 0; i < numberOfBarcodes; i++)
{
barcodes[i] = new Barcode(locations[i].ToApiStruct(), messages[i], types[i]);
Log.Info(MediaVisionLog.Tag, barcodes[i].ToString());
}
return barcodes;
}
private static InteropBarcode.DetectedCallback GetCallback(TaskCompletionSource> tcs)
{
return (IntPtr mvSource, IntPtr engineCfg, Unmanaged.Quadrangle[] locations, string[] messages,
BarcodeType[] types, int numberOfBarcodes, IntPtr userData) =>
{
Log.Info(MediaVisionLog.Tag, $"Barcodes detected, count : {numberOfBarcodes}");
try
{
tcs.TrySetResult(CreateBarcodes(locations, messages, types, numberOfBarcodes));
}
catch (Exception e)
{
MultimediaLog.Error(MediaVisionLog.Tag, "Failed to handle barcode detection callback", e);
tcs.TrySetException(e);
}
};
}
}
}