Header Ads

Android Capture Image from Camera and Display it into ImageView Programmatically in 1 minutes

We can find nowadays many android application offering users to take photos and videos for different purpose. So in this tutorial, I am going to show how to capture/take image from camera and display it in android app programmatically. 

There are many ways to take photos from android camera and display it. And in this tutorial, I am going to show with one of the simplest way. You will take/capture photos by clicking a button i.e. Take Photo From Camera and display it in ImageView of the same activity.

Following is the example to capture/take image from camera and display in ImageView with full necessary code. 


How to Capture Image from Android Camera and Display it Programmatically


Following is the content of XML layout file and java activity file.

XML Layout File


res/layout/capture_image_from_camera_and_display.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_margin="16dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the below button to take photo from camera and display it in image view"/>
<Button
android:id="@+id/take_image_from_camera"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:onClick="takeImageFromCamera"
android:layout_height="wrap_content"
android:text="Take Photo From Camera"/>
<ImageView
android:id="@+id/image_from_camera"
android:layout_width="200dp"
android:src="@drawable/image"
android:layout_height="300dp"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="ViralAndroid.com"
android:autoLink="web"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center|bottom"/>
</LinearLayout>


Java Activity File


src/CamereCaptureAndDisplayImage.java
//How to Capture Image from Camera and Display in Android ImageView/ Activity
package com.viralandroid.androidtutorial;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class CamereCaptureAndDisplayImage extends AppCompatActivity {
private static final int CAMERA_REQUEST = 1888;
ImageView mimageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture_image_from_camera_and_display);
mimageView = (ImageView) this.findViewById(R.id.image_from_camera);
Button button = (Button) this.findViewById(R.id.take_image_from_camera);
}
public void takeImageFromCamera(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
mimageView.setImageBitmap(mphoto);
}
}
}

No comments:

Powered by Blogger.