Camera lauch on button click(Capture/Crop App)
Crop Captured image(Capture/Crop App)
Show Croped image(Capture/Crop App)
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.
- 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
- 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!
you can download the source code here