The Camera API provides access to your device’s Camera(s)
Use this API to give users the ability to take a photo via one of the device’s cameras and return it to the application. Photos can either be stored on the device and optionally transfered to an online server using the Network API or can be returned as a DataURI object for storing or display.
In order to use this API you must include the following extension in your build.yml
extensions: ["mediacapture"]
Be sure to review the JavaScript API Usage guide for important information about using this API in JavaScript
Be sure to review the Ruby API Usage guide for important information about using this API in Ruby
Choose a picture from the album.
Parameters
Provide a set of properties to configure an image, for example to specify the image size or color mode.
Valid `properties` for this parameter are the properties avaliable to this API module. Check the property sectionNot providing properties to this function will use the Camera’s default properties, or those previously set on the Camera instance.
Async Callback Returning Parameters: HASH
Whether or not the image was successfully chosen from the gallery. The returned string will be one of ‘ok’, ‘cancel’ or ‘error’.
The URI to the chosen image.
The height of the image.
The width of the image.
The format of the image, either ‘png’ or ‘jpg’.
If the returned status is ‘error’ then this field will contain more information on the error.
It is recommended to use imageUri in preference to this parameter.
It is recommended to use imageHeight in preference to this parameter.
It is recommended to use imageWidth in preference to this parameter.
It is recommended to use imageFormat in preference to this parameter.
Synchronous Return:
Method Access:
Rho.Camera.choosePicture(HASH propertyMap, CallBackHandler callback)
Rho::Camera.choosePicture(HASH propertyMap, CallBackHandler callback)
This method will return all of object/value pairs for the propertyNames of the API class.
Parameters
Async Callback Returning Parameters: HASH
Synchronous Return:
Map of all available properties
: this method also supports async callbacks - check the Callback tab for callback return parameters.
Method Access:
myObject.getAllProperties(CallBackHandler callback)
Rho.Camera.getAllProperties(CallBackHandler callback)
Rho::Camera.getAllProperties(CallBackHandler callback)
Returns the camera of requested type if that camera exist – else return nil.
Parameters
CameraType: main or front.
Async Callback Returning Parameters: SELF_INSTANCE
Synchronous Return:
Camera with requested type or nil if not requested type does not exist.
: this method also supports async callbacks - check the Callback tab for callback return parameters.Method Access:
Rho.Camera.getCameraByType(STRING cameraType, CallBackHandler callback)
Rho::Camera.getCameraByType(STRING cameraType, CallBackHandler callback)
This method will return an object that represents the default instance of the API Class. For example Camera.getDefault will return a Camera object that represents the default camera.
Synchronous Return:
Default object of Module.
Method Access:
Rho.Camera.getDefault()
Rho::Camera.getDefault()
This method will return a set of object/value pairs for the list of the propertyName that is passed in. The propertyNames must be a valid property of the API class.
Parameters
List of properties I want to know about
Async Callback Returning Parameters: HASH
Synchronous Return:
Map of properties I want to know about
: this method also supports async callbacks - check the Callback tab for callback return parameters.
Method Access:
myObject.getProperties(ARRAY arrayofNames, CallBackHandler callback)
Rho.Camera.getProperties(ARRAY arrayofNames, CallBackHandler callback)
Rho::Camera.getProperties(ARRAY arrayofNames, CallBackHandler callback)
This method will return the value of the propertyName that is passed in. The propertyName must be a valid property of the API class.
Parameters
The property to return info about.
Async Callback Returning Parameters: STRING
Synchronous Return:
The property to return info about.
: this method also supports async callbacks - check the Callback tab for callback return parameters.Method Access:
myObject.getProperty(STRING propertyName, CallBackHandler callback)
Rho.Camera.getProperty(STRING propertyName, CallBackHandler callback)
Rho::Camera.getProperty(STRING propertyName, CallBackHandler callback)
Save an image to the device gallery.
Parameters
The real path to the image that is to be saved in the device gallery. You need to use the RhoApplication method get_blob_path to have the real path, such as Rho::RhoApplication::get_blob_path(img.image_uri).
Synchronous Return:
Method Access:
Rho.Camera.saveImageToDeviceGallery(STRING pathToImage)
Rho::Camera.saveImageToDeviceGallery(STRING pathToImage)
This method allows you to set the attributes of the default object instance by passing in an object of the same class.
Parameters
An instance object that is of the same class.
Synchronous Return:
Method Access:
Rho.Camera.setDefault(SELF_INSTANCE: Rho::Camera defaultInstance)
Rho::Camera.setDefault(SELF_INSTANCE: Rho::Camera defaultInstance)
This method will set the values of a list of properties for the API class. The propertyName must be a valid property for the class and must also not be read only.
Parameters
Map of properties I want to set
Synchronous Return:
Method Access:
myObject.setProperties(HASH propertyMap)
Rho.Camera.setProperties(HASH propertyMap)
Rho::Camera.setProperties(HASH propertyMap)
This method will set the value of a property for the API class. The propertyName must be a valid property for the class and must also not be read only.
Parameters
The one property name that I want to set
The one property value that I want to set
Synchronous Return:
Method Access:
myObject.setProperty(STRING propertyName, STRING propertyValue)
Rho.Camera.setProperty(STRING propertyName, STRING propertyValue)
Rho::Camera.setProperty(STRING propertyName, STRING propertyValue)
Start the camera application to take a picture. The user can capture the displayed image either by interacting with the resident camera app or pressing the trigger or enter key, depending on the device in use.
Parameters
Provide a set of properties to configure the camera, for example to specify the flashMode or compressionFormat.
Valid `properties` for this parameter are the properties avaliable to this API module. Check the property sectionNot providing properties to this function will use the Camera’s default properties, or those previously set on the Camera instance.
Async Callback Returning Parameters: HASH
Whether or not the image was successfully captured. The returned string will be one of ‘ok’, ‘cancel’ or ‘error’.
If the specified ‘outputFormat’ was ‘image’ then this field is the URI to the taken image stored on the device, this image will have an auto-generated name. If the specified ‘outputFormat’ was ‘dataUri’ then this field will be the image encoded as a Data URI.
The actual height of the image that was captured, this may differ from the requested height as the Camera will only support a finite resolutions.
The actual width of the image that was captured, this may differ from the requested width as the Camera will only support a finite resolutions.
The format in which the image was captured, either ‘png’ or ‘jpg’.
If the returned status is ‘error’ then this field will contain more information on the error.
It is recommended to use imageUri in preference to this parameter.
It is recommended to use imageHeight in preference to this parameter.
It is recommended to use imageWidth in preference to this parameter.
It is recommended to use imageFormat in preference to this parameter.
Synchronous Return:
Method Access:
myObject.takePicture(HASH propertyMap, CallBackHandler callback)
Rho.Camera.takePicture(HASH propertyMap, CallBackHandler callback)
Rho::Camera.takePicture(HASH propertyMap, CallBackHandler callback)
Camera type: back or front.
Default: main
Possible Values (STRING):
Back camera.
Front camera.
Property Access:
myObject.cameraType
Rho.Camera.cameraType
Rho::Camera.cameraType
Path to a sound file resident on the device which will be played when the image is captured.
Property Access:
myObject.captureSound
Rho.Camera.captureSound
Rho::Camera.captureSound
Where supported by the hardware this property can be used to select whether to capture a color or a grayscale image.
Default: rgb
Possible Values (STRING):
A colour image is captured.
A grayscale image is captured.
Property Access:
myObject.colorModel
Rho.Camera.colorModel
Rho::Camera.colorModel
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.
Default: jpg
Possible Values (STRING):
JPG compression.
PNG compression.
Property Access:
myObject.compressionFormat
Rho.Camera.compressionFormat
Rho::Camera.compressionFormat
Camera hardware is limited to taking photos in a finite number of resolutions, eg 2048x1536, 640x480 etc. Specifying a desiredHeight will request to take the photo with the specified height but if it is not supported by the hardware then the closest match will be selected. The callback received when a photo is taken contains the actual resolution of the captured photo.
Property Access:
myObject.desiredHeight
Rho.Camera.desiredHeight
Rho::Camera.desiredHeight
Camera hardware is limited to taking photos in a finite number of resolutions, eg 2048x1536, 640x480 etc. Specifying a desiredWidth will request to take the photo with the specified width but if it is not supported by the hardware then the closest match will be selected. The callback received when a photo is taken contains the actual resolution of the captured photo.
Property Access:
myObject.desiredWidth
Rho.Camera.desiredWidth
Rho::Camera.desiredWidth
Enables post photo capture image customizing; image will captured reduced to screen size (not full size).
Default: true
Property Access:
myObject.enableEditing
Rho.Camera.enableEditing
Rho::Camera.enableEditing
The path without filename extension to store captured image in subsequent calls to takePicture(). The filename extension will be added automatically according to compressionFormat property value.
Property Access:
myObject.fileName
Rho.Camera.fileName
Rho::Camera.fileName
Specifies the flash behavior when taking a picture.
Possible Values (STRING):
Flash will be used.
Flash will not be used.
Flash will be used if lighting conditions deem it necessary.
Flash with red eye reduction is used.
The flash is turned on in torch mode.
Property Access:
myObject.flashMode
Rho.Camera.flashMode
Rho::Camera.flashMode
The maximum height of images which can be captured.
Property Access:
myObject.maxHeight
Rho.Camera.maxHeight
Rho::Camera.maxHeight
The maximum width of images which can be captured.
Property Access:
myObject.maxWidth
Rho.Camera.maxWidth
Rho::Camera.maxWidth
Specifies the way to return the captured image to the application.
Default: image
Possible Values (STRING):
The captured image is stored on the device. The callback associated with takePicture will contain the location of the image.
Image will be returned as a Data URI object, this is a base 64 encoding of the image and can be used to easily embed the image on the page or store the image in a database.
Platforms:Android,WM
Property Access:
myObject.outputFormat
Rho.Camera.outputFormat
Rho::Camera.outputFormat
In cases where the resident camera application on the device is not used this API is used to control the position of the viewfinder preview window when taking a photo.
Property Access:
myObject.previewHeight
Rho.Camera.previewHeight
Rho::Camera.previewHeight
In cases where the resident camera application on the device is not used this API is used to control the position of the viewfinder preview window when taking a photo.
Property Access:
myObject.previewLeft
Rho.Camera.previewLeft
Rho::Camera.previewLeft
In cases where the resident camera application on the device is not used this API is used to control the position of the viewfinder preview window when taking a photo.
Property Access:
myObject.previewTop
Rho.Camera.previewTop
Rho::Camera.previewTop
In cases where the resident camera application on the device is not used this API is used to control the position of the viewfinder preview window when taking a photo.
Property Access:
myObject.previewWidth
Rho.Camera.previewWidth
Rho::Camera.previewWidth
If true, the picture you take will be added to the device photo gallery. At Android default value is true.
Default: false
Property Access:
myObject.saveToDeviceGallery
Rho.Camera.saveToDeviceGallery
Rho::Camera.saveToDeviceGallery
List of resolutions supported by camera.
Property Access:
myObject.supportedSizeList
Rho.Camera.supportedSizeList
Rho::Camera.supportedSizeList
Uses the system Camera application to take a picture instead of rhodes' camera. The system Camera application does not accept most rhodes' camera settings and can be adjusted by UI.
Default: false
Property Access:
myObject.useSystemViewfinder
Rho.Camera.useSystemViewfinder
Rho::Camera.useSystemViewfinder
Take an image with as little code as possible, using all default values.
function take_picture_with_default_camera() { // Capture an image from the default camera on the device, using the default image settings Rho.Camera.takePicture({}, picture_taken_callback); } function picture_taken_callback(params) { // Did we receive an image? if (params["status"]=="ok") { // Assuming we have an <img id="#captured_image"> tag, we will be able to see the image that was just captured $("#captured_image").attr("src", Rho.Application.expandDatabaseBlobFilePath(params["imageUri"])); } }
def take_picture_with_default_camera # Capture an image from the default camera on the device, using the default image settings Rho::Camera.takePicture({}, :picture_taken_callback) end def picture_taken_callback # Did we really take a picture? if (@params["status"]=="ok") # If so, show it Rho::WebView.navigate(url_for(:action => :show_picture, :query => {:image => Rho::Application.expandDatabaseBlobFilePath(@params["imageUri"])})) else # Otherwise we are done here Rho::WebView.navigate(url_for(:action => :index)) end end
On devices with more than one camera, you can select which camera to use for taking pictures.
<div id="camera_list"> </div> var cameras = []; function choose_camera() { // get all available cameras cameras = Rho.Camera.enumerate(); // build an HTML list var cameraList = "<ul>"; for (var cameraIndex=0; cameraIndex<cameras.length; cameraIndex++) { var camera = cameras[cameraIndex]; // Create a link for each camera with an onclick handler cameraList = cameraList +'<li><a href="#" class="take_picture_with_selected_camera" onclick="take_picture_with_camera('+cameraIndex+')">'+camera.cameraType+'</a></li>'; } cameraList = cameraList+"</ul>"; // make camera list visible to the user $("#camera_list").html(cameraList); } function take_picture_with_camera(cameraIndex) { var camera = cameras[cameraIndex]; camera.takePicture({}, picture_taken_callback); }
# controller.rb def choose_camera $cameras = Rho::Camera.enumerate render end def take_picture_using_chosen_camera camera = $cameras[@params["cameraIndex"].to_i] camera.takePicture({}, url_for(:action => :picture_taken_callback)) end # choose_camera.erb <ul data-role="listview"> <% $cameras.each_with_index do |camera, index| %> <li><a href="<%= url_for(:action => :take_picture_using_chosen_camera, :query => {:camera_index => index}) %>"><%= camera.cameraType %></li> <% end %> </ul>
Apart from taking new pictures, you can also save images to the built-in gallery. In the following examples, the picture we are adding to the gallery is one that was just taken with the camera, but you can add any other image you can access by filename.
function take_picture_and_save_it_to_gallery() { Rho.Camera.choosePicture({}, picture_taken_callback_save_to_gallery); } function picture_taken_callback_save_to_gallery(params) { if (params["status"]=="ok") { Rho.Camera.saveImageToDeviceGallery(Rho.Application.expandDatabaseBlobFilePath(params["imageUri"])); alert("Image saved to gallery"); } }
def take_picture_and_save_it_to_gallery Rho::Camera.choosePicture({}, url_for(:action => :picture_taken_callback_save_to_gallery)) end def picture_taken_callback_save_to_gallery # Did we really take a picture? if (@params["status"]=="ok") # If so, save it to the gallery Rho::Camera.saveImageToDeviceGallery(Rho::Application.expandDatabaseBlobFilePath(@params["imageUri"])) end Rho::WebView.navigate(url_for(:action => :index)) end
You can tweak multiple options to get an image exactly as you need it
function control_image_properties() { // Instead of accepting the defaults, let's set some properties to our liking // We will ask for a PNG file... Rho.Camera.compressionFormat = "png"; // ...a particular image size... Rho.Camera.desiredWidth = 1024; Rho.Camera.desiredHeight = 768; // ...and force the flash to be used Rho.Camera.flashMode = "on"; // Now, take the picture Rho.Camera.takePicture({}, picture_taken_callback); } function picture_taken_callback(params) { // Did we receive an image? if (params["status"]=="ok") { // show it in our <img id="captured_image"> tag $("#captured_image").attr("src", Rho.Application.expandDatabaseBlobFilePath(params["imageUri"])); } }
def control_image_properties # Instead of accepting the defaults, let's set some properties to our liking # We will ask for a PNG file... Rho::Camera.compressionFormat = "png" # ...a particular image size... Rho::Camera.desiredWidth = 1024 Rho::Camera.desiredHeight = 768 # ...and force the flash to be used Rho::Camera.flashMode = "on" # Now, take the picture Rho::Camera.takePicture({}, :picture_taken_callback) end def picture_taken_callback # Did we really take a picture? if (@params["status"]=="ok") # If so, show it Rho::WebView.navigate(url_for(:action => :show_picture, :query => {:image => Rho::Application.expandDatabaseBlobFilePath(@params["imageUri"])})) else # Otherwise we are done here Rho::WebView.navigate(url_for(:action => :index)) end end
You can get all available camera properties in a single call.
function determine_camera_capabilities() { var capabilitiesList = "<ul>"; // Get all capabilities of the camera... var capabilities = Rho.Camera.getAllProperties(); // ... compose a nicely formatted list with their names and values ... for (var capability in capabilities) { capabilitiesList+="<li>"+capability+": "+capabilities[capability]+"</li>"; } capabilitiesList += "</ul>"; // ... and show it $("#camera_capabilities").html(capabilitiesList); }
# controller.rb def determine_camera_capabilities # Get all camera properties @properties = Rho::Camera.getAllProperties render end # determine_camera_capabilities.erb <ul data-role="listview"> <% @properties.each_pair do |name,value| %> <li><%= name %> : <%= value %></li> <% end %> </ul>
Apart from taking new pictures, the Camera API also lets you access existing images on the device’s gallery.
function select_picture_from_gallery() { Rho.Camera.choose_picture({}, picture_taken_callback); } function picture_taken_callback(params) { // Did we receive an image? if (params["status"]=="ok") { // Show it in an <img> tag $("#captured_image").attr("src", Rho.Application.expandDatabaseBlobFilePath(params["imageUri"])); } }
def select_picture_from_gallery Rho::Camera.choosePicture({}, url_for(:action => :picture_taken_callback)) end def picture_taken_callback # Did we really take a picture? if (@params["status"]=="ok") # If so, show it Rho::WebView.navigate(url_for(:action => :show_picture, :query => {:image => Rho::Application.expandDatabaseBlobFilePath(@params["imageUri"])})) else # Otherwise we are done here Rho::WebView.navigate(url_for(:action => :index)) end end