Take a photo in android camera

on Sunday, January 6, 2013 | 9:56 PM

Take a photo in android camera. Here is a simple tutorial using the cameras available in android device for use in applications that want to create and display a preview of the image.

Take a photo in android camera

Given that the camera is part of the existing hardware in the device, then we need to make permissions to access the camera. To add permissions in android we can add to the AndroidManifest.xml file.
<uses -permission android:name="android.permission.CAMERA"></uses>
In simple access to the camera via an intent. If you already have a button with the event being called quite like this:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
With intent as above, then you will directly use the camera, but just open the camera, no additional functions such as preview the result.

Well, the following will also preview the results.

Initially we will determine the layout. Because only simple, then this layout will only use the function keys to access the camera and ImageView to display the camera images.
< ?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<button android:id="@+id/capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take a Picture"></button>
<imageview android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerInside"></imageview>
</linearlayout>
Next we determine the content of play ativity like this:
public class MyActivity extends Activity {
   private static final int REQUEST_IMAGE = 100;
   Button captureButton;
   ImageView imageView;
 
   @Override
   public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          captureButton = (Button)findViewById(R.id.capture);
          captureButton.setOnClickListener(listener);
          imageView = (ImageView)findViewById(R.id.image);
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
       //Process and display the image
       Bitmap userImage = (Bitmap)data.getExtras().get("data");
       imageView.setImageBitmap(userImage);
   }
}
   private View.OnClickListener listener = new View.OnClickListener() {
 
   @Override
   public void onClick(View v) {
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       startActivityForResult(intent, REQUEST_IMAGE);
   }
 };
}
By coding as above the photo obtained directly displayed on imageView. This can be seen in the code:
      Bitmap userImage = (Bitmap)data.getExtras().get("data");
       imageView.setImageBitmap(userImage);
If you need to determine where the images are stored into the storage, then we need to add Uri destination file, so its code be like:
public class MyActivity extends Activity {
   private static final int REQUEST_IMAGE = 100;
   Button captureButton;
   ImageView imageView;
 
   @Override
   public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          captureButton = (Button)findViewById(R.id.capture);
          captureButton.setOnClickListener(listener);
          imageView = (ImageView)findViewById(R.id.image);
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
       try {
            FileInputStream in = new FileInputStream(destination);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 10; //Downsample by 10x
            Bitmap userImage = BitmapFactory.decodeStream(in, null, options);
            imageView.setImageBitmap(userImage);
       } catch (Exception e) {
            e.printStackTrace();
       }
    }
  }
   private View.OnClickListener listener = new View.OnClickListener() {
 
   @Override
   public void onClick(View v) {
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       //Add extra to save full-image somewhere
       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
       startActivityForResult(intent, REQUEST_IMAGE);
   }
 };
}
Completed.
Share this post :
Take a photo in android camera Published By Technoledge Terima Kasih Telah membaca Take a photo in android camera
Link Published on: 2013-01-06T21:56:00+07:00
Take a photo in android camera. Here is a simple tutorial using the cameras available in android device for use in applications that want to... Rating 5 ★★★★★ Reviews 1110
Index »

Android App

 
Copyright © 2013. Technoledge - All Rights Reserved
Proudly powered by Blogger