Showing posts with label camera. Show all posts
Showing posts with label camera. Show all posts

Sunday, 29 September 2013

Camera lauch on button click(Capture/Crop App)

Capture Images and Crop Images using Intent

Crop Captured image(Capture/Crop App)

Capture Images and Crop Images using Intent

Show Croped image(Capture/Crop App)

Capture Images and Crop Images using Intent

Create new Android Project
Project Name: Take picture from Camera
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Take_Pic_Camera
Package Name: com.shaikhhamadali.blogspot.take_pic_camera
Create layout file: activity_launch_camera 

  1. create layout: 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LaunchCamera"
    android:background="#000000" >

    <ImageView
        android:id="@+id/imVCature_pic"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:contentDescription="catured picture"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="400dp"
        android:text="Take Picture" />

</RelativeLayout>

2.code of activity:


package com.shaikhhamadali.blogspot.take_pic_camera;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class LaunchCamera extends Activity {
 ImageView imVCature_pic;
 Button btnCapture;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_launch_camera);
  initializeControls();
 }

 private void initializeControls() {
  imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
  btnCapture=(Button)findViewById(R.id.btnCapture);
  btnCapture.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    /* create an instance of intent
     * pass action android.media.action.IMAGE_CAPTURE 
     * as argument to launch camera
     */
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    /*create instance of File with name img.jpg*/
    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
    /*put uri as extra in intent object*/
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    /*start activity for result pass intent as argument and request code */
    startActivityForResult(intent, 1);
   }
  });

 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  //if request code is same we pass as argument in startActivityForResult
  if(requestCode==1){
   //create instance of File with same name we created before to get image from storage
   File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
   //Crop the captured image using an other intent
   try {
    /*the user's device may not support cropping*/
    cropCapturedImage(Uri.fromFile(file));
   }
   catch(ActivityNotFoundException aNFE){
    //display an error message if user device doesn't support
    String errorMessage = "Sorry - your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();
   }
  }
  if(requestCode==2){
   //Create an instance of bundle and get the returned data
   Bundle extras = data.getExtras();
   //get the cropped bitmap from extras
   Bitmap thePic = extras.getParcelable("data");
   //set image bitmap to image view
   imVCature_pic.setImageBitmap(thePic);
  }
 }
 //create helping method cropCapturedImage(Uri picUri)
 public void cropCapturedImage(Uri picUri){
  //call the standard crop action intent 
  Intent cropIntent = new Intent("com.android.camera.action.CROP");
  //indicate image type and Uri of image
  cropIntent.setDataAndType(picUri, "image/*");
  //set crop properties
  cropIntent.putExtra("crop", "true");
  //indicate aspect of desired crop
  cropIntent.putExtra("aspectX", 1);
  cropIntent.putExtra("aspectY", 1);
  //indicate output X and Y
  cropIntent.putExtra("outputX", 256);
  cropIntent.putExtra("outputY", 256);
  //retrieve data on return
  cropIntent.putExtra("return-data", true);
  //start the activity - we handle returning in onActivityResult
  startActivityForResult(cropIntent, 2);
 }
}

3. note that:

  • you can use this on button onclick,on action_down,on the fly etc.
  • you can capture image by multiple ways one of them i have described above.
  • you can crop image by multiple ways too but this one is using intent.
  • about Camera.

4. conclusion:

  • Some information about how to launch camera using intent and crop image using intent too.
  • Know how to decode image from storage to bitmap from my previous post.
  • know what is Bitmap factory options and how to use them from my previous post.

5. about the post:

  • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!
6. Source Code:
        you can download the source code here

Cheers,
Hamad Ali Shaikh

Saturday, 28 September 2013

Camera lauch on button click

Capture Images using Intent (Android)

Show Captured image

Capture Images using Intent (Android)

Create new Android Project
Project Name: Take picture from Camera
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Take_Pic_Camera
Package Name: com.shaikhhamadali.blogspot.take_pic_camera
Create layout file: activity_launch_camera 



  1. create layout: 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LaunchCamera"
    android:background="#000000" >

    <ImageView
        android:id="@+id/imVCature_pic"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:contentDescription="catured picture"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="400dp"
        android:text="Take Picture" />

</RelativeLayout>

2.code od activity:


package com.shaikhhamadali.blogspot.take_pic_camera;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class LaunchCamera extends Activity {
 ImageView imVCature_pic;
 Button btnCapture;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_launch_camera);
  initializeControls();
 }

 private void initializeControls() {
  imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
  btnCapture=(Button)findViewById(R.id.btnCapture);
  btnCapture.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    /* create an instance of intent
     * pass action android.media.action.IMAGE_CAPTURE 
     * as argument to launch camera
     */
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    /*create instance of File with name img.jpg*/
    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
    /*put uri as extra in intent object*/
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    /*start activity for result pass intent as argument and request code */
    startActivityForResult(intent, 1);
   }
  });

 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  //if request code is same we pass as argument in startActivityForResult
  if(requestCode==1){
   //create instance of File with same name we created before to get image from storage
   File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
   //get bitmap from path with size of
   imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 600, 450));
   }
 }
 public static Bitmap decodeSampledBitmapFromFile(String path,
   int reqWidth, int reqHeight) { 
  // First decode with inJustDecodeBounds=true to check dimensions
  final BitmapFactory.Options options = new BitmapFactory.Options();
  //Query bitmap without allocating memory
  options.inJustDecodeBounds = true;
  //decode file from path
  BitmapFactory.decodeFile(path, options);
  // Calculate inSampleSize
  // Raw height and width of image
  final int height = options.outHeight;
  final int width = options.outWidth;
  //decode according to configuration or according best match
  options.inPreferredConfig = Bitmap.Config.RGB_565;
  int inSampleSize = 1;
  if (height > reqHeight) {
   inSampleSize = Math.round((float)height / (float)reqHeight);
  }
  int expectedWidth = width / inSampleSize;
  if (expectedWidth > reqWidth) {
   //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
   inSampleSize = Math.round((float)width / (float)reqWidth);
  }
  //if value is greater than 1,sub sample the original image
  options.inSampleSize = inSampleSize;
  // Decode bitmap with inSampleSize set
  options.inJustDecodeBounds = false;
  return BitmapFactory.decodeFile(path, options);
 }
}

3. note that:

  • you can use this on button onclick,on action_down,on the fly etc.
  • you can capture image by multiple ways one of them i have described above.

4. conclusion:

  • Some information about how to launch camera using intent.
  • Know how to decode from storage to bitmap.
  • know what is Bitmap factory options and how to use them.

5. About the post:

  • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!
6. Source Code:
        you can download the source code here

Cheers,
Hamad Ali Shaikh