How do I Capture Images in LiveCode Mobile?

This lesson describes how to detect the camera features of iOS and Android devices and take photos from cameras that are available. Screen captures and sample code are provided.

Introduction

iOS and Android devices come with a number of features that can be accessed via LiveCode. In this lesson we will see how to detect what camera features are available and how to tell LiveCode how to use the camera features. Taking a photo with LiveCode launches the native camera control interface on the iOS and Android device and then returns control back to LiveCode.

Creating the Test Buttons

In this example we create six buttons that allow you to probe a device for available cameras and take photos using the cameras that are available on the device. The six buttons have the following code associated with them.

The camera button allows you to take a picture with the default device camera:

on mouseUp
    # the X, Y dimensions for an image are not supported on Android and are ignored
    mobilePickPhoto "camera", 212, 122
    if the result is text then
        put the result into image "The_Image"
    else
        put the result into field "The_Result"
    end if
end mouseUp

The front camera button allows you to take a picture with the front device camera:

on mouseUp
    # the X, Y dimensions for an image are not supported on Android and are ignored
    mobilePickPhoto "front camera", 212, 122
    if the result is text then
        put the result into image "The_Image"
    else
        put the result into field "The_Result"
    end if
end mouseUp

The rear camera button allows you to take a picture with the rear device camera:

on mouseUp
    # the X, Y dimensions for an image are not supported on Android and are ignored
    mobilePickPhoto "rear camera", 212, 122
    if the result is text then
        put the result into image "The_Image"
    else
        put the result into field "The_Result"
    end if
end mouseUp

The features button allows you to detect and list the camera features that are available on the device:

on mouseUp
    mobileCameraFeatures
    put the result into field "The_Result"
end mouseUp

The features front button allows you to detect and list the front camera features that are available on the device:

on mouseUp
    mobileCameraFeatures "front"
    put the result into field "The_Result"
end mouseUp

The features rear button allows you to detect and list the rear camera features that are available on the device:

on mouseUp
    mobileCameraFeatures "rear"
    put the result into field "The_Result"
end mouseUp

In a purpose built application you may want to combine the commands and raise custom dialogs to inform the user what cameras, if any, are available.

Detecting the Features

You can detect all of the camera features that are available by selecting the features button, as is shown in the above figure. This would return a list of cameras that are available or empty, if the device has no cameras available. Similarly, pressing the features front or features rear buttons lists the features that are available to the specific camera selected, or empty if the selected camera is not supported.

Trying to take a Photo with an Unavailable Camera

If a camera was selected for the purpose of taking a photo, and the devices did not support that camera, then mobilePickPhoto returns a string with the text: "source not available", as is shown in the above figure.

Taking a Photo

If a camera was selected that was supported by the iOS device, then the native device camera control is launched, allowing you to take and retake photos.

Displaying the Photo on the LiveCode Card

After you have taken a successful photo, such as the one shown in this example, control is returned to the LiveCode application. The size of the displayed images is controlled by parameters passed to mobilePickPhoto.

Note: The parameters passed to mobilePickPhoto that control the size of the photo are ignored in Android. To set the image size to be used on Android use the following

set the width of the templateImage to <width>
set the height of the templateImage to <height>
mobilePickPhoto "camera"