Image Editing

This documentation outlines the guidelines for utilizing the Image Module SDK, which offers powerful image editing tools. The SDK provides straightforward methods to incorporate advanced image editing functionalities into your

Prerequisites

  • Ensure you have integrated the TruVideo Core SDK into your project and completed with the authentication process

  • Camera module will provide MediaItem or simply provide filepath to upload (optional)

Edit an Image

To edit an image, follow these steps:

Step 1 : Import Classes and init Callback Wrapper

#if IOS
using Foundation;
using UIKit;
using TruvideoImageiOS;
#endif

#if ANDROID
using Application = Android.App.Application;
using TruVideoImageAndroidBinding;
using TruVideoImageLib = TruVideoImageAndroid.DotnetTruvideoImage;
#endif


#if ANDROID
 private Task<T> ExecuteImageCallbackAsync<T>(Action<ImageCallback> action) {
        var tcs = new TaskCompletionSource<T>();

        action.Invoke(new ImageCallback(
            success => MainThread.BeginInvokeOnMainThread(() =>
                tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
            failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
        ));

        return tcs.Task;
    }
#endif

Step 2 : Get Result File path where to place the save image

This function returns the result path using the provided file name as input.

#if ANDROID
var outputPath = await ExecuteImageCallbackAsync<string>(cb => TruVideoImageLib.GetResultPath(Application.Context,  inputPath, cb));
#endif

#if IOS
String directory = Path.GetDirectoryName(filePath) ?? throw new InvalidOperationException();
String fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
String outputFileName = $"{fileNameWithoutExtension}_EditedImage.jpg";
String outputPath = Path.Combine(directory, outputFileName);
Uri outputUri = new Uri(outputPath);

Step 3 : Call Image Edit

After obtaining the result path, invoke the launchImageEdit function to edit

#if ANDROID
var imageResult = await ExecuteImageCallbackAsync<string>(cb => TruVideoImageLib.EditImage(Application.Context, inputPath, outputPath, cb));
#endif

#if IOS
TruvideoImageSdk.Shared.EditImage(inputUrl: fileUri, outputUrl: outputUri, viewController: viewController,
    (resultUrl, error) =>
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
        });
    });

Finalize :

After completing this steps the function should look like

#if IOS
using Foundation;
using UIKit;
using TruvideoImageiOS;
#endif

#if ANDROID
using Application = Android.App.Application;
using TruVideoImageAndroidBinding;
using TruVideoImageLib = TruVideoImageAndroid.DotnetTruvideoImage;
#endif

#if ANDROID
 private Task<T> ExecuteImageCallbackAsync<T>(Action<ImageCallback> action) {
        var tcs = new TaskCompletionSource<T>();

        action.Invoke(new ImageCallback(
            success => MainThread.BeginInvokeOnMainThread(() =>
                tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
            failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
        ));

        return tcs.Task;
    }
#endif

#if ANDROID
var inputPath = filePath.ToString();
var outputPath = await ExecuteImageCallbackAsync<string>(cb => TruVideoImageLib.GetResultPath(Application.Context,  inputPath, cb));
var imageResult = await ExecuteImageCallbackAsync<string>(cb => TruVideoImageLib.EditImage(Application.Context, inputPath, outputPath, cb));
LogTextEditor.Text = $"Edited Image : {imageResult}";        
#endif

#if IOS
String filePath = inputPath;
Uri fileUri = new Uri(filePath);
String directory = Path.GetDirectoryName(filePath) ?? throw new InvalidOperationException();
String fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
String outputFileName = $"{fileNameWithoutExtension}_EditedImage.jpg";
String outputPath = Path.Combine(directory, outputFileName);
Uri outputUri = new Uri(outputPath);
var viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;

TruvideoImageSdk.Shared.EditImage(inputUrl: fileUri, outputUrl: outputUri, viewController: viewController,
    (resultUrl, error) =>
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            
            if (error != null)
            {
                // Handle error
                Console.WriteLine($"Error to edit Image: {error.LocalizedDescription}");
                LogTextEditor.Text = $"Error to edit Image: {error.LocalizedDescription}";
                return;
            }

            // Handle success
            Console.WriteLine($"Image Edited at: {resultUrl}");
            LogTextEditor.Text = $"Image Edited at: {resultUrl}";
            string resultFilePath = resultUrl.Path; // Use Path instead of LocalPath

            // Move or rename the result file to the desired output path
            if (File.Exists(resultFilePath))
            {
                File.Move(resultFilePath, outputPath, true);
                Console.WriteLine($"Edited Image saved at: {outputPath}");
                LogTextEditor.Text = $"Edited Image saved at: {outputPath}";
            }
        });
    });
#endif

Learn More

Explore advanced capabilities of the Image Module in the Image Module Reference.

Last updated

Was this helpful?