Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to change the Image orientation in Android

    • 0
    • 3
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 8.34k
    Comment on it

    To change the orientation of an image write the following code:

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = 2; 
    // Get the original bitmap from the filepath to which you want to change orientation
    // fileName ist the filepath of the image
    Bitmap cachedImage=BitmapFactory.decodeFile(fileName, o2);
    
    int rotate = getCameraPhotoOrientation(imageViewer, fileName);
    Matrix matrix = new Matrix();
    matrix.postRotate(rotate);
    // Here you will get the image bitmap which has changed orientation
    cachedImage = Bitmap.createBitmap(cachedImage , 0, 0, cachedImage.getWidth(), cachedImage.getHeight(), matrix, true);

    Get the original photo orientation

    public static int getCameraPhotoOrientation(Context context, 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 (Exception e) {
                e.printStackTrace();
            }
            return rotate;
        }
        

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: