Camera lauch on button click
Show Captured image
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.
- about Camera.
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.
- 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
- 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