Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

camera intent auto rotate to 90 degree

Tags:

android

In my code below, I am trying to take photo using native camera and upload to server but when I take it as portrait and view it in gallery as landscape which means, its rotated to 90 degree. Pls help :-

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {

            handleCameraPhoto();
}
private void handleCameraPhoto() {
    Intent mediaScanIntent = new Intent(
            "android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);

    mediaScanIntent.setData(contentUri);
    getActivity().sendBroadcast(mediaScanIntent);
}

How can I rotate the image before saving to SD card?

like image 256
micky Avatar asked Oct 22 '13 07:10

micky


2 Answers

I also faced this kind of problem while showing the images in listview. But using the EXIF data I was able to get a work around to set the images in proper orientation.

This is were the bitmap object for display is prepared :

  Matrix matrix = new Matrix();
  matrix.postRotate(getImageOrientation(url));
  Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
  bitmap.getHeight(), matrix, true);

This is the method used, in the 2nd line of above code, to rotate the images orientation.

 public static int getImageOrientation(String imagePath){
     int rotate = 0;
     try {

         File imageFile = new File(imagePath);
         ExifInterface exif = new ExifInterface(
                 imageFile.getAbsolutePath());
         int orientation = 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;
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
    return rotate;
 }

This may not be the precise answer to your question, it worked for me and hope it will be useful for you.

like image 179
Shail Adi Avatar answered Oct 21 '22 07:10

Shail Adi


Matrix matrix=new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);



This method does not require creating a new bitmap each time.. Hope this works.
like image 27
AKSH Avatar answered Oct 21 '22 05:10

AKSH