A Deep Look Into Camera API of Rhomobile

In this post, we will go through the available methods for Camera API in details using Rhodes. This post will just explain the methods and uses of the Camera API from ruby excluding the rho-element javascript API.

Camera Permission:
In order to use the device camera, we need to get the permission by defining the capabilities in build.yml file.

Capabilities:

      • Camera

This denotes the Rhomobile build stage to add respective access permissions to the application keeping the platform and required settings on focus.

Available APIs:
Rhomobile provides the listed APIs to interact with the Device Camera.

      1. get_camera_info
      2. save_image_to_device_gallery
      3. take_picture
      4. choose_picture

Camera Options:
The APIs (take_picture & choose_picture) takes a hash as options for extra configurations, if we need to set.

camera_option = {
:camera_type => “main”,
:desired_width => “640”,
:desired_height => “480”,
:color_model => “RGB”,
:format => “png”,
:enable_editing => false,
:flash_mode => “auto”,
:save_to_shared_gallery => true
}

Let’s go through each options in details,

Options Possible Values Default Notes
camera_type 1. main2. front main
desired_width The   value to be in px maximum   possible camera width
color_model 1. RGB2.   Grayscale RGB
format 1. png2. jpg jpg
flash_mode 1. on2. off3. auto4.   red-eye

5. torch

auto Android   only
enable_editing 1. true2. false TRUE iPhone   only
save_to_shared_gallery 1. true2. false FALSE iOs only

How to use APIs:

  1. get_camera_info

This method takes parameter ( camera_type ) and returns the hash value with width and height as numeric value and returns NIL if camera_type is not supported.

camera_info = Camera::get_camera_info(camera_type)
camera_info will be something like
{ :width => 640, :height => 480 } | NIL
  1. Choose_Picture

This method is used to select the image file from the gallery or sdcard. This method takes two parameters callback_method and camera_option.

Camera::choose_picture(callback_method,camera_option)

The callback_method (“/app/camera/callback_method”) will be called after theoperation completes, with parameter as hash described in callback_params.

  1. Take_Picture

This method is used to open the camera view and snap a photo. This method takes two parameters callback_method and camera_option as like above one.

Camera::take_picture(callback_method,camera_option)

The callback_method will be called after the operation completes, with parameter as hash described in callback_params.

  1. Save_Image_to_Device_Gallery

This method is used to store the generated or created image file to device gallery. This method takes parameter as the image path.

image_path =
Rho::RhoApplication::get_blob_path(image_uri)
Camera::save_image_to_device_gallery(image_path)

The image_uri will be the uri of the image in the callback_params.

Camera Callback Parameters:
The callback method with be called by POST method and the passed parameters are:

{
:status => “ok”,
:image_uri => “some uri”,
:message => “”,
:image_format => “jpg”,
:image_width => 640,
:image_height => 480
}

Let’s go through each parameters in details,

1. status : Possible values are “ok” or “cancel” or “error”.

2. message : Some error message as string if the status is “error”.

3. image_uri : The uri of the taken or choosen image.

4. image_format : Possible values are “png” or “jpg”.

5. image_width : Width of the image in numeric value.

6. image_height : Height of the image in numeric value.

Written By: Ashis Kumar, Software Developer, Mindfire Solutions

Advertisements

One thought on “A Deep Look Into Camera API of Rhomobile

  1. Pingback: Capturing Image Using Camera API & Uploading it to Server | MindfireMobile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s