• Introduction
  • Dev Environment Setup
  • Developing Apps
  • Data Handling
  • Device Capabilities
  • Testing & Debugging
  • Extending

Camera

From version 5.1 of RhoMobile Suite, you can access the device’s Camera capabilities by using the Camera API

Enabling The Camera API

To use the Camera API, include the following extension in your build.yml :::ruby extensions: [“mediacapture”]

After building with this extension, you will have access to the API from within your code

    Rho.Camera.takePicture({}, picture_taken_callback);     
    
    Rho::Camera.takePicture({}, :picture_taken_callback)
    

Choosing the Camera

On most devices there are both front and rear facing cameras. By default, the rear camera is chosen and can be used by calling the Rho.Camera namespace. However, the Camera API also supports defining instances of the Camera object in a variety of different ways. This allows you greater control and flexibility in using the API.

Specifying By Name

One way you can ensure the use of a certain camera on a device is by naming an instance of that camera with the getCameraByType method:

    var frontCam = Rho.Camera.getCameraByType('front');
    frontCam.takePicture({}, picture_taken_callback);       
    
    frontCam = Rho::Camera.getCameraByType('front');
    frontCam.takePicture({}, :picture_taken_callback)
    

Note: On Windows Mobile/CE the options are color for the camera and imager for the B/W 2D Imager

Finding All Cameras

You can also use the enumerate method to identify all cameras available on the device. This method will return an array of Camera instances:

    var cameras = [];
    cameras = Rho.Camera.enumerate();

    // each object in the cameras array will be a Camera object
    // and can use the properties and methods available

    
    $cameras = Rho::Camera.enumerate

    # each object in the cameras array will be a Camera object
    # and can use the properties and methods available
    
    

Capturing a Picture

The takePicture method is used to capture an image and optionally pass in an options object as the first parameter that can control the behavior of the API. The second parameter shown in the example is the executed callback function:

    // These properties can also be set individually on the instance
    // In this example we are setting the properties when 
    // executing the takePicture method

    Rho.Camera.takePicture({
        compressionFormat : "png",
        desiredWidth :1024,
        desiredHeight :768,
        flashMode: "on"
    }, picture_taken_callback);

    
    # These properties can also be set individually on the instance
    # In this example we are setting the properties when 
    # executing the takePicture method

    Rho::Camera.takePicture({
        compressionFormat => "png",
        desiredWidth =>1024,
        desiredHeight =>768,
        flashMode=> "on"
    }, picture_taken_callback);
    
    

Callback Status

The callback function will always be executed and will contain an object that will have different properties depending on the result. The status property should always be checked to see if the call was successful or if the user canceled the action.

    function picture_taken_callback(params){

        // params.status
        // ok: picture was taken
        // error: an error occured
        // cancel: user canceled the action

        if(params.status =='ok'){

        }

        if(params.status =='error'){

        }

        if(params.status =='cancel'){

        }

    }
    

    
    def picture_taken_callback

        // params.status
        // ok: picture was taken
        // error: an error occured
        // cancel: user canceled the action
        
        if (@params["status"]=="ok")

        end

    end
    
    

Retrieving the Image File

In the callback of the takePicture method, a parameter called imageUri will contain the location of the caputered image

    Rho.Camera.takePicture({},
        function(params){
            console.log(params.imageUri)

            //Ex on Android: 
            // -> /data/data/com.rhomobile.rms51/rhodata/db/db-files/IMG_20150512_081051.jpg 
        });
    
    Rho::Camera.takePicture({},
        lambda{ |params|
            puts(params.imageUri)

            //Ex on Android: 
            // -> /data/data/com.rhomobile.rms51/rhodata/db/db-files/IMG_20150512_081051.jpg 
        });
    
    

Retrieving the Image as DataUri

In some cases, you may want to store the image as a DataURI string instead of an actual file. This is useful when the image size required is a lower resolution and you want to store it directly in a database. To accomplish this, you need to first specify the output format you would like by using the outputFormat property. You should specify the ouptput format before you execute the takePicture method. The imageUri paramter of the callback of the takePicture method will contain the dataUri string representation of the image that was captured.

    Rho.Camera.takePicture({
            outputFormat:Rho.Camera.OUTPUT_FORMAT_DATAURI
        },
        function(params){
            console.log(params.imageUri)

            // ->data:image/jpeg;base64,/9j/4WaXRXhpZgAATU0AKgAAA....
        });
    
    Rho::Camera.takePicture({
            outputFormat =>Rho::Camera.OUTPUT_FORMAT_DATAURI
        },
        lambda{ |params|
            puts(params.imageUri)

            #data:image/jpeg;base64,/9j/4WaXRXhpZgAATU0AKgAAA....
        });
    
    

Saving to the Gallery

You can also save the image to the device’s photo gallery by setting the saveToDeviceGallery property to true:

    Rho.Camera.takePicture({
            saveToDeviceGallery:true
        },
        function(params){
            //check status
        });
    
    Rho::Camera.takePicture({
            saveToDeviceGallery=>true
        },
        lambda{ |params|
            
            #check status
        });
    
    

Useful Properties

compressionFormat : STRING

Description

The format of the captured image in subsequent calls to takePicture(). If you do not define this property when you use choose_picture with iOS, the type of image in Gallery will be recognized, and the same format will be used for saving the image to applications data. In windows and WP8 devices the format will be always jpg type.
This Property shall accept/return one among the values mentioned in constant section which starts with COMPRESSION_FORMAT_

Params

Default: jpg

Access

  • Instance: This property can be accessed via an instance object of this class: myObject.compressionFormat

  • Default Instance: This property can be accessed via the default instance object of this class

Javascript: Rho.Camera.compressionFormat

Ruby: Rho::Camera.compressionFormat

flashMode : STRING

Description

Specifies the flash behavior when taking a picture.
This Property shall accept/return one among the values mentioned in constant section which starts with FLASH_

Access

  • Instance: This property can be accessed via an instance object of this class: myObject.flashMode

  • Default Instance: This property can be accessed via the default instance object of this class

Javascript: Rho.Camera.flashMode

Ruby: Rho::Camera.flashMode

Back to Top