Capturing Image Using Camera API & Uploading it to Server

In this post, we will see how to capture the image using Camera API and store it to app base path so as to gain permission to later upload it to server. We will see the way to store the details of the image on the rhom ( SQLite database ), then later upload the file to server on the time of sync process.

Capturing Image Using Device Camera:
As described on the previous post, A Deep Look into Camera API of Rhomobile, we need to use take picture method to capture the photo.

Below is the code to capture the photo from device camera.

def take_picture

album_id = @params['album_id']
camera_option =
{
:camera_type => “main”,
:color_model => “RGB”,
:enable_editing => false,
:flash_mode => 'auto',
}

Camera::take_picture(url_for(:action =>
:camera_callback,
:query=>
{
:id => album_id }),
camera_option)
end

Here after the Camera API captures the photo, it will be redirected to the callback method with parameters as described in callback_params. The callback_params will be having an additional parameter ‘id’ that we send with the callback method.

Storing the Image:
On the callback method, we need to store the image details on the local database and store the image file to the base app path.

def camera_callback

# Storing the image details to local db
file_name = @params['image_uri'].to_s().split("/")[2]
newImage = Image.new()
newImage.image_uri = @params['image_uri']
newImage.album_id = @params['id']
newImage.filename = file_name.to_s
newImage.created_at = Time.now.utc
newImage.updated_at = Time.now.utc
newImage.save

# Store the image file to base app path
file_blob_path = File.join(Rho::RhoApplication::get_blob_path(@params['image_uri']))
file_content = File.binread(file_blob_path)
file_real_path = File.join(Rho::RhoApplication::get_base_app_path(), file_name)
f = File.new(file_real_path, "wb")
f.write(file_content)
f.close
end

Note: The @params['image_uri'] contains the path to the blob data stored on the local app folder in blob format not in the proper image (JPG or PNG) format.

So to be able to upload the image to server, we need to convert the blob data to image format and do a simple file transfer using upload_file method.

Upload the image to server:
During the sync process, we need to upload the record and the image file to server. Assuming the server API to accept the parameter 'album_id' for mapping the image file to the respective album, let’s see how to achieve it.

def image_upload_to_server
images = Image.find_by_sql("select * from Image where filename is not null")
unless images.eql?(nil)
images.each do |image|
# Get the image real path
image_path = File.join(Rho::RhoApplication::get_base_app_path(), image.filename)
data = Rho::AsyncHttp.upload_file(
:url => "#{$server_url}/photo/upload?album_id=#{image.album_id}",
:filename => image_path,
:headers => {},
:body => ""
)
end
end
end

Note: It is better to have a file transfer to server for image rather than sending the blob data to server through GET or POST method as some of the data can be trimmed or get lost during the process.

Written By: Ashis Kumar, Software Developer, Mindfire Solutions

Advertisements

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