Question

I want to rotate bitmap when the picture taken as potrait . I want to understand image is potrait or lanscape? I use this code :

   Bitmap  photo   = BitmapFactory.decodeFile(path_img,options); 
   int imageHeight = photo.getHeight();
   int imageWidth  = photo.getWidth();

When I take image as portroit and lanscape it does not matter it is always like this : imageHeight =390; imageWidth =520;

How can I understand a picture is taken lanscape or portrait.

Thanks

Was it helpful?

Solution

Code taken from nigels link and altered it for the provided code snippet

Bitmap  photo   = BitmapFactory.decodeFile(path_img,options);
ExifInterface exif = new ExifInterface(path_img);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
int rotationInDegrees = exifToDegrees(rotation);

Matrix matrix = new Matrix();
if (rotation != 0f) {
    matrix.preRotate(rotationInDegrees);
}

Bitmap adjustedBitmap = Bitmap.createBitmap(photo, 0, 0, width, height, matrix, true);

private static int exifToDegrees(int exifOrientation) {        
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } 
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; } 
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }            
    return 0;    
}

from what i understood, this code snippet and the 2 methods should do all the work by themselfs.

OTHER TIPS

How can I understand a picture is taken lanscape or portrait?

assuming the image is currently stored on file, you can get it orientation from the Exif metadata object:

File f = new File(capturedImageFilePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

note that this information not necessarily available - it's up to the application which captured the image to store this metadata on the file.

in case this information not exists, getAttributeInt would return ExifInterface.ORIENTATION_NORMAL

the orientation can can be one of the following: ExifInterface.ORIENTATION_ROTATE_90 / ExifInterface.ORIENTATION_ROTATE_180 / ExifInterface.ORIENTATION_ROTATE_270

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top