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.
get_camera_info
save_image_to_device_gallery
take_picture
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
}
Lets
go through each options in details,
1.
camera_type :
Possible
values are “main” or “front”.
Default
is “main”.
2.
desired_width :
The
value to be in px.
Default
is the maximum possible camera width.
3.
desired_height :
The
value to be in px.
Default
is the maximum possible camera height.
4.
color_model :
Possible
values are “RGB” or “Grayscale”.
Default
is “RGB”.
5.
format :
Possible
values are “png” or “jpg”.
Default
is “jpg”.
6.
flash_mode :
Possible
values are “on” | “off” | “auto” | “red-eye”
| “torch”.
Default
is “auto”. This option is for Android only.
7.
enable_editing :
Posible
value Boolean true / false.
Default
is true. This option is for iPhone only.
9.
save_to_shared_gallery :
Possible
values are Boolean true | false.
Default
is false. This option is for iOs only.
How
to use APIs:
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
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 the operation completes, with parameter as hash
described in
callback_params.
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.
The
callback_method will be called after the operation
completes, with
parameter as hash described in callback_params.
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.
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
}
Lets
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.
Thats
all. We can now explore device camera with the above APIs.