Pages

Sunday 11 August 2013

Capturing Image using Camera API in Rhomobile & Uploading it to Server

In this post, we will see how to capture the image using Camera API of Rhomobile 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]
  image = Image.new()
  image.image_uri = @params['image_uri']
  image.album_id = @params['id']
  image.filename = file_name.to_s
  image.created_at = Time.now.utc
  image.updated_at = Time.now.utc      
  image.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

Upload the image to server:
During the sync process, we need to upload the record and the image file to server as follows,

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 => "#{@@url}/photo/upload?album_id=#{image.album_id}",       
                  :filename => image_path,
                  :headers => {}, 
                  :body => ""
                )
    end
  end
end
That's all, the image is now uploaded to the server.

1 comment:

  1. Hi Ashish
    Thanks for posting on net it is very helpful
    I just had one Query what is the album_id here can it be anything which i provide ??

    ReplyDelete