Camera API - StansAssets/com.stansassets.android-native GitHub Wiki

The article will describe camera related API available with the plugin. Make sure that camera service is enabled inside the Plugin Editor UI.

Camera Image Capture

The code example below will demonstrate how to allow a user to capture a photo with the device camera and then use it inside Unity app.

using SA.Android.Camera;
...

int maxSize = 1024;
AN_Camera.CaptureImage(maxSize, (result) => {
    PrintCaptureResult(result);
});

private void PrintCaptureResult(AN_CameraCaptureResult result) {
    if (result.IsFailed) {
        AN_Logger.Log("Filed:  " + result.Error.Message);
        return;
    }

    AN_Logger.Log("result.Media.Type: " + result.Media.Type);
    AN_Logger.Log("result.Media.Path: " + result.Media.Path);
    AN_Logger.Log("result.Media.Thumbnail: " + result.Media.Thumbnail);

    ApplyImageToGUI(result.Media.Thumbnail);
    AN_Camera.DeleteCapturedMedia();
}

private void ApplyImageToGUI(Texture2D image) {
    //m_image is a UnityEngine.UI.RawImage
    m_image.texture = image;

    //m_sprite is a UnityEngine.UI.Image
    m_sprite.sprite = image.ToSprite();
}

Camera Video Capture

You may use the same approach for the video capturing

using SA.Android.Camera;
...

int maxSize = 1024;
AN_Camera.CaptureVideo(maxSize, (result) => {
    PrintCaptureResult(result);
});

private void PrintCaptureResult(AN_CameraCaptureResult result) {
    if (result.IsFailed) {
        AN_Logger.Log("Filed:  " + result.Error.Message);
        return;
    }

    AN_Logger.Log("result.Media.Type: " + result.Media.Type);
    AN_Logger.Log("result.Media.Path: " + result.Media.Path);
    AN_Logger.Log("result.Media.Thumbnail: " + result.Media.Thumbnail);

    ApplyImageToGUI(result.Media.Thumbnail);
    AN_Camera.DeleteCapturedMedia();
}

private void ApplyImageToGUI(Texture2D image) {
    //m_image is a UnityEngine.UI.RawImage
    m_image.texture = image;

    //m_sprite is a UnityEngine.UI.Image
    m_sprite.sprite = image.ToSprite();
}

Clean Up Resources

There is one more thing you should know. Since the native part is transferring only scale thumbnails of captured media (scaled according to the maxSize property value). You may what to have an access to the actually captured media (image or video), that's why we have a Path property inside the AN_Media object. When you no longer need a saved media, you can simply delete it using the DeleteCapturedMedia method.

using SA.Android.Camera;
...

AN_Camera.DeleteCapturedMedia();