Capture Media
This documentation provides guidelines on capturing images and recording videos using the TruVideo Camera SDK. The SDK offers simple methods to integrate camera functionalities into your application.
Prerequisites
Ensure you have integrated the TruVideo Core SDK into your project and completed with the authentication process
Capturing an Image
To capture an image, follow these steps:
Step 1 : Create Async Callback Wrapper
#if ANDROID
private Task<T> ExecuteCameraCallbackAsync<T>(Action<CameraCallback> action)
{
var tcs = new TaskCompletionSource<T>();
action.Invoke(new CameraCallback(
success => MainThread.BeginInvokeOnMainThread(() =>
tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
));
return tcs.Task;
}
#endif
#if IOS
private Task<T> ExecuteCallbackAsync<T>(Action<Action<string, NSError>> nativeCall) {
var tcs = new TaskCompletionSource<T>();
nativeCall((nsResult, error) => {
if (error != null)
{
tcs.SetException(new Exception(error.LocalizedDescription));
}
else {
// Convert NSString to string
string result = nsResult.ToString();
tcs.TrySetResult((T)Convert.ChangeType(result, typeof(T)));
// tcs.SetResult(result);
}
});
return tcs.Task;
}
#endif
Step 2 : Create new configuration to give to the camera
#if ANDROID
var config = new ModeTypeConfig();
config.RawType = ModeType.VideoAndPicture;
config.VideoCount = null;
config.PictureCount = null;
config.VideoDuration = null;
var orientation = OrientationMode.Portrait;
var mode = FlashMode.Off;
var lensType = LensType.Back;
#if IOS
var lensFacing = LensType.Back;
var flashMode = FlashMode.Off;
var orientation = OrientationMode.Portrait;
var mode = ModeTypeConfig.VideoAndPicture(null, null, null);
Step 3 : Present the camera with the configuration
#if ANDROID
var cameraResult = await ExecuteCameraCallbackAsync<string>(cb => DotnetCameraLib.ShowCamera(Application.Context, lensType, mode, orientation, "",config, cb));
var captures = JsonSerializer.Deserialize<List<TruvideoSdkCameraMedia>>(cameraResult);
#if IOS
void ShowCameraHandler(NSArray<NSString> paths)
{
MainThread.BeginInvokeOnMainThread(() =>
{
if (paths.Count > 0)
{
LogTextEditor.Text += "Selected media paths:\n" + paths + "\n";
_selectedMedia = paths;
}
else
{
LogTextEditor.Text += "No selected media\n";
}
});
}
var currentViewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
TruvideoCamera.Shared.ShowCamera(lensFacing, flashMode, orientation, "", mode, currentViewController,ShowCameraHandler);
Finalize :
After completing this steps the function should look like
// import classes
#if IOS
using UIKit;
using Foundation;
using TruvideoCameraiOS;
#endif
#if ANDROID
using Application = Android.App.Application;
using TruVideoCameraAndroidBinding;
using JsonSerializer = System.Text.Json.JsonSerializer;
using DotnetCameraLib = TruVideoCameraAndroid.DotnetCameraTruvideo;
#endif
// create Async wrapper
#if ANDROID
private Task<T> ExecuteCameraCallbackAsync<T>(Action<CameraCallback> action)
{
var tcs = new TaskCompletionSource<T>();
action.Invoke(new CameraCallback(
success => MainThread.BeginInvokeOnMainThread(() =>
tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
));
return tcs.Task;
}
#endif
#if IOS
private Task<T> ExecuteCallbackAsync<T>(Action<Action<string, NSError>> nativeCall) {
var tcs = new TaskCompletionSource<T>();
nativeCall((nsResult, error) => {
if (error != null)
{
tcs.SetException(new Exception(error.LocalizedDescription));
}
else {
// Convert NSString to string
string result = nsResult.ToString();
tcs.TrySetResult((T)Convert.ChangeType(result, typeof(T)));
// tcs.SetResult(result);
}
});
return tcs.Task;
}
#endif
// call function to init camera
#if ANDROID
SubscribeToCameraEvents();
var config = new ModeTypeConfig();
config.RawType = ModeType.VideoAndPicture;
config.VideoCount = null;
config.PictureCount = null;
config.VideoDuration = null;
var orientation = OrientationMode.Portrait;
var mode = FlashMode.Off;
var lensType = LensType.Back;
var cameraResult = await ExecuteCameraCallbackAsync<string>(cb => DotnetCameraLib.ShowCamera(Application.Context, lensType, mode, orientation, "",config, cb));
var captures = JsonSerializer.Deserialize<List<TruvideoSdkCameraMedia>>(cameraResult);
_cameraList = new List<string>();
if (captures != null)
foreach (var capture in captures)
{
Console.WriteLine(
$"FileRahul: {capture.FilePath}, Resolution: {capture.Resolution.Width}x{capture.Resolution.Height}");
_cameraList.Add(capture.FilePath);
}
LogTextEditor.Text = "Selected media paths: " + string.Join(", ", _cameraList);
#endif
#if IOS
var lensFacing = LensType.Back;
var flashMode = FlashMode.Off;
var orientation = OrientationMode.Portrait;
var mode = ModeTypeConfig.VideoAndPicture(null, null, null);
SubscribeToCameraEvents();
void ShowCameraHandler(NSArray<NSString> paths)
{
MainThread.BeginInvokeOnMainThread(() =>
{
if (paths.Count > 0)
{
LogTextEditor.Text += "Selected media paths:\n" + paths + "\n";
_selectedMedia = paths;
}
else
{
LogTextEditor.Text += "No selected media\n";
}
});
}
var currentViewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
TruvideoCamera.Shared.ShowCamera(lensFacing, flashMode, orientation, "", mode, currentViewController,
ShowCameraHandler);
#endif
What’s Next?
Now that you can capture media, you’re ready to:
By following this guide, you’ve enabled rich media capture in your iOS app with just a few lines of code. Let’s keep going!
Last updated
Was this helpful?