Skip to content Skip to sidebar Skip to footer

How Rotate Image Taken From Camera Or Gallery.?

I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured. I want rotate image to straight if they are rotated. I

Solution 1:

profileImage = destination;

You took the thumbnail which is a Bitmap and wrote it to file.

Then after that you used that file to extract an exifinterface.

But bitmaps do not contain exif information. And hence your file does not either.

So your orientation is always null;

If you had used profileImage to launch the camera intent then leave it as is.

So remove above statement.

Solution 2:

publicintgetCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
 introtate=0;
try {
    context.getContentResolver().notifyChange(imageUri, null);
    FileimageFile=newFile(imagePath);

    ExifInterfaceexif=newExifInterface(imageFile.getAbsolutePath());
    intorientation= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotate = 270;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotate = 180;
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotate = 90;
        break;
    }

    Log.i("RotateImage", "Exif orientation: " + orientation);
    Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
    e.printStackTrace();
}
return rotate;
}

And put this code in Activity result method and get value to rotate image...

StringselectedImage= data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursorcursor= getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();

        introtateImage= getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath);

Rotate Bitmap using this

publicstatic Bitmap rotate(Bitmap bitmap, float degrees) {
   Matrixmatrix=newMatrix();
   matrix.postRotate(degrees);
   return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  }

Post a Comment for "How Rotate Image Taken From Camera Or Gallery.?"