4 May 2012

Get image from Camera And gallery and display in Image view | Decode camera image and display in Image view | set image with orientation in android.

Hi Guys Today we are going to see very useful tutorial.
Capture or Select Image from gallery or camera and display in image view.



main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_shareImage_Gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Gallery" />

    <Button
        android:id="@+id/btn_shareImage_capture"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Capture" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

</LinearLayout>

CaptureImages.java :
public class CaptureImages extends Activity { Button btnGallery,btnCamera; ImageView imageView; static final int IMAGE_REQUEST_GALLERY = 0; static final int IMAGE_REQUEST_CAMERA = 1; private String selectedImagePath = ""; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.share_images); imageView=(ImageView) findViewById(R.id.imageView1); btnGallery=(Button) findViewById(R.id.btn_shareImage_Gallery); btnCamera=(Button) findViewById(R.id.btn_shareImage_capture); btnGallery.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { pickImage(IMAGE_REQUEST_GALLERY); } }); btnCamera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { pickImage(IMAGE_REQUEST_CAMERA); } }); } protected void pickImage(int code) { if(code==IMAGE_REQUEST_GALLERY) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, ""), code); } else { final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); startActivityForResult(intent, code); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == IMAGE_REQUEST_GALLERY) { selectedImagePath = getAbsolutePath(data.getData()); imageView.setImageBitmap(decodeFile(selectedImagePath)); } else if (requestCode == IMAGE_REQUEST_CAMERA) { selectedImagePath = getImagePath(); //imageView.setImageBitmap(decodeFile(selectedImagePath)); imageView.setImageBitmap(SetImageOrientaion(selectedImagePath)); } else { super.onActivityResult(requestCode, resultCode, data); } } } public Uri setImageUri() { // Store image in dcim File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png"); Uri imgUri = Uri.fromFile(file); this.selectedImagePath = file.getAbsolutePath(); return imgUri; } public String getImagePath() { return selectedImagePath; }
//Below Method is used for decode large image
 public Bitmap decodeFile(String path) 
 {
  try 
  {
   // Decode image size
   BitmapFactory.Options o = new BitmapFactory.Options();
   o.inJustDecodeBounds = true;
   BitmapFactory.decodeFile(path, o);
   // The new size we want to scale to
   final int REQUIRED_SIZE = 512;

   // Find the correct scale value. It should be the power of 2.
   int scale = 1;
   while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
    scale *= 2;

   // Decode with inSampleSize
   BitmapFactory.Options o2 = new BitmapFactory.Options();
   o2.inSampleSize = scale;
   return BitmapFactory.decodeFile(path, o2);
  } 
  catch (Throwable e) 
  {
   e.printStackTrace();
  }
  return null;

 }

 public String getAbsolutePath(Uri uri) 
 {
  String[] projection = { MediaColumns.DATA };
  @SuppressWarnings("deprecation")
  Cursor cursor = managedQuery(uri, projection, null, null, null);
  if (cursor != null) {
   int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
   cursor.moveToFirst();
   return cursor.getString(column_index);
  } else
   return null;
 }
 //Below Method is used for Set orientation of Image
 public Bitmap SetImageOrientaion(String path) 
 {
  Bitmap correctBmp = null;
  try
  {
   File f = new File(path);
   ExifInterface exif = new ExifInterface(f.getPath());
   int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

   int angle = 0;

   if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
    angle = 90;
   }
   else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
    angle = 180;
   }
   else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
    angle = 270;
   }

   Matrix mat = new Matrix();
   mat.postRotate(angle);

   Bitmap bmp1 = decodeFile(path);
   correctBmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);                
  }
  catch (IOException e) {
   Log.w("TAG", "-- Error in setting image");
  }  
  catch(OutOfMemoryError oom) {
   Log.w("TAG", "-- OOM Error in setting image");
  }
  return correctBmp;

 }
}

    
Download full source code from here CameraSampleCode

More Ref Link:
Link 2:
Link 3-Decode Image:
Link 4: Display with crop

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)

No comments:

Post a Comment