Allrounder
Allrounder

Reputation: 695

How to change the orientation of image taken by camera intent?

in my app i am taking a picture from the camera intent, and then in a different class it creates a thumbnail of that image and returns that thumbnail, However if i take the picture in portrait it returns as landscape. And when googling this, i found that in samsung devices this is a problem? is there a way of resolving this?

here is my code for creating the thumbnail:

public class GetImageThumbnail {

private static int getPowerOfTwoForSampleRatio(double ratio) {
int k = Integer.highestOneBit((int) Math.floor(ratio));
if (k == 0)
    return 1;
else
    return k;
}

public Bitmap getThumbnail(Uri uri, Test test)
    throws FileNotFoundException, IOException {
InputStream input = ((Context) test).getContentResolver().openInputStream(uri);

BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -2)
        || (onlyBoundsOptions.outHeight == -2))
    return null;

int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
        : onlyBoundsOptions.outWidth;

double ratio = (originalSize > 200) ? (originalSize / 175) : 0.5;

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither = true;
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
input = ((Context) test).getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
return bitmap;
}
}

Could some one please help?

Upvotes: 0

Views: 775

Answers (3)

J.K
J.K

Reputation: 2393

use this method in your Activity & when you got the bitmap in onActivityResult you can call this method

public Bitmap changeOrientation(Uri imageUri, String imagePath, Bitmap source) {
        // TODO Auto-generated constructor stub
        int rotate = 0;
        int orientation = 0;
        try {
            getContentResolver().notifyChange(imageUri, null);
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            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;
            }
            // Log.v(Common.TAG, "Exif orientation: " + orientation);
        } catch (Exception e) {
            e.printStackTrace();
        }

        /****** Image rotation ****/
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);
        Bitmap cropped = Bitmap.createBitmap(source, x, y, width, height, matrix,
                true);
        return cropped;
        /*
         * 
         * 
         * Bitmap android.graphics.Bitmap.createBitmap(Bitmap source, int x, int
         * y, int width, int height, Matrix m, boolean filter) public static
         * Bitmap createBitmap (Bitmap source, int x, int y, int width, int
         * height, Matrix m, boolean filter) Added in API level 1 Returns an
         * immutable bitmap from subset of the source bitmap, transformed by the
         * optional matrix. The new bitmap may be the same object as source, or
         * a copy may have been made. It is initialized with the same density as
         * the original bitmap. If the source bitmap is immutable and the
         * requested subset is the same as the source bitmap itself, then the
         * source bitmap is returned and no new bitmap is created.
         * 
         * Parameters source The bitmap we are subsetting x The x coordinate of
         * the first pixel in source y The y coordinate of the first pixel in
         * source width The number of pixels in each row height The number of
         * rows m Optional matrix to be applied to the pixels filter true if the
         * source should be filtered. Only applies if the matrix contains more
         * than just translation.
         * 
         * Returns A bitmap that represents the specified subset of source
         * Throws IllegalArgumentException if the x, y, width, height values are
         * outside of the dimensions of the source bitmap, or width is <= 0, or
         * height is <= 0
         */
    }

Upvotes: 1

Shubham
Shubham

Reputation: 1442

You Can use ExifInterface for that

First get the captured orientation on the image by following method

public 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;
    }

and then use

Matrix matrix = new Matrix();
                matrix.postRotate(getImageOrientation(path));
                photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(),
                        photo.getHeight(), matrix, true);

to redraw the bitmap.

Upvotes: 1

ray
ray

Reputation: 148

You can check the Orientation via the Exif. Than you can rotate the picture if it has a wrong orientation: Android rotate bitmap around center without resizing

Upvotes: 0

Related Questions