Search code examples
androidandroid-camerabitmapfactoryandroid-bitmap

Bitmap getWith() and getHeighth() always return same size?


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


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.