CarinaMj
CarinaMj

Reputation: 197

Android image rotation

I have chosen camera or gallery to take the image for upload. I have decode the image using the below code

 public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {

    path = new File(path).getAbsolutePath();

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

}

Now if this image took as horizontal view then I should change it as vertically. because of the receipt upload.

In my gallery image or taken image width > height I want to rotate the image to vertical view any suggestion for this??

Upvotes: 0

Views: 906

Answers (3)

AngelJanniee
AngelJanniee

Reputation: 623

This is the code I used it works for me,

 public Bitmap decodeFile(String filePath) {
    int imageOrientation = 10;
    ExifInterface ei;
    try {
        ei = new ExifInterface(filePath);
        imageOrientation = ei.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

    } catch (IOException e) {
        e.printStackTrace();
    }

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitMap = BitmapFactory.decodeFile(filePath, o2);

    switch (imageOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotateImage(bitMap, 90);
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotateImage(bitMap, 180);
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotateImage(bitMap, 270);
            break;

        default:
            break;
    }


    return bitMap;

}

This is the rotate method,

 public void rotateImage(Bitmap img, int degree) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    bitMap = Bitmap.createBitmap(img, 0, 0, img.getWidth(),
            img.getHeight(), matrix, true);
}

Upvotes: 1

Sonal Bharwani
Sonal Bharwani

Reputation: 61

You need to use matrix for this as follows, it worked for me

  Matrix matrix = getRotation(context, Uri.fromFile(f));
  b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);   

And here is the getRotation() method

  public static Matrix getRotation(Context context, Uri selectedImage) {

        Matrix matrix = new Matrix();
        ExifInterface exif;
        try {

            exif = new ExifInterface(selectedImage.getPath());

            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            Utility.log("orientation", orientation + "");
            if (orientation == 6) {
                matrix.postRotate(90);
            } else if (orientation == 3) {
                matrix.postRotate(180);
                matrix.postScale((float) b.getWidth(), (float) b.getHeight());
            } else if (orientation == 8) {
                matrix.postRotate(270);
            } else {
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return matrix;

    }   

here b is your bitmap.

Upvotes: 0

Nadav Tasher
Nadav Tasher

Reputation: 147

You can just set it in an ImageView and rotate the view 90 degrees to any direction you want it to be.

Upvotes: 0

Related Questions