Skip to content

Camera API

levching edited this page Apr 14, 2020 · 4 revisions

The article will describe the cross-platform camera-related API available with the plugin. Make sure that camera service is enabled inside the Plugin Editor UI. Once you've activated the platforms you interested in, you may use Camera API, as described with the code samples below.

CameraAPI

Note: If your application is locked to the landscape mode, you may have issues with iOS camera view. Please check iOS Native documentation for a workaround.

Capture Image From Camera

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

using SA.CrossPlatform.App;
...

var camera = UM_Application.CameraService;

int maxThumbnailSize = 1024;
camera.TakePicture(maxThumbnailSize, (result) => {
   if(result.IsSucceeded) {
        UM_Media mdeia = result.Media;

        Texture2D image = mdeia.Thumbnail;
        Debug.Log("Thumbnail width: " + image.width + " / height: " + image.height);
        Debug.Log("mdeia.Type: " + mdeia.Type);
        Debug.Log("mdeia.Path: " + mdeia.Path);
    } else {
        Debug.Log("failed to take a picture: " + result.Error.FullMessage);
    }
});

You may wonder what the maxThumbnailSize parameter does and why you might need it. The reason is the following. Use may take a huge photos/images using a device camera, and you probably do not want whole 4k or 8k photo to be transferred to your application since the transfer processes can take a while + it will result in a noticeable memory usage spike.

That's why you can set the max image size. If the user will take a picture that is bigger then maxThumbnailSize parameter value. The plugin will resize an image on a native part, saving photo format and dimensions of course.

Note: On iOS, Path can be null when we taking a picture directly from a camera. iOS will not save it on a disk by default.

Capture Video From Camera

The approach is similar. You will have the same result object, the Thumbnail of the result will be the Thumbnail of the captured video. And you can get actual video using the media Path property. See the example below:

using SA.CrossPlatform.App;
...

var camera = UM_Application.CameraService;

int maxThumbnailSize = 1024;
camera.TakeVideo(maxThumbnailSize, (result) => {
    if (result.IsSucceeded) {
        UM_Media media = result.Media;

        Texture2D image = media.Thumbnail;
        Debug.Log("Thumbnail width: " + image.width + " / height: " + image.height);
        Debug.Log("mdeia.Type: " + media.Type);
        Debug.Log("mdeia.Path: " + media.Path);
    } else {
        Debug.Log("failed to take a picture: " + result.Error.FullMessage);
    }
});
Clone this wiki locally