Kritias
Kritias

Reputation: 187

How to display Bitmap correctly?

I'm coping with two issues trying to display a photo from the Gallery in my Android application. What I want to do is simple: get a photo from the gallery and put it in a ImageView (which is 100dp*100dp) in my MainActivity.

The first issue is that on some phones, such as Sony Xperia, the photo is rotated when it is set on the ImageView. To address the problem, I found that piece of code in a SO answer:

public Bitmap decodeFile(String path) 
{
    int orientation;

    try {
        if (path == null) {
            return null;
        }
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 0;

        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }
        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bm = BitmapFactory.decodeFile(path, o2);
        Bitmap bitmap = bm;

        ExifInterface exif = new ExifInterface(path);

        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Log.e("ExifInteface .........", "rotation =" + orientation);

        // exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90);

        Log.e("orientation", "" + orientation);
        Matrix m = new Matrix();

        if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
            m.postRotate(180);
            // m.postScale((float) bm.getWidth(), (float) bm.getHeight());
            // if(m.preRotate(90)){
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            m.postRotate(90);
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            m.postRotate(270);
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        }
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}

That works perfectly, but I also want the image to be a square, which is not the case for now.

To do that, after I used the first method on my bitmap, I also called that one:

public static Bitmap cropToSquare(Bitmap bitmap)
{
    int width  = bitmap.getWidth();
    int height = bitmap.getHeight();
    int newWidth = (height > width) ? width : height;
    int newHeight = (height > width)? height - ( height - width) : height;
    int crop = (width - height) / 2;
    crop = (crop < 0)? 0: crop;
    Bitmap cropImg = Bitmap.createBitmap(bitmap, crop, 0, newWidth, newHeight);

    return cropImg;
}

It does turns the bitmap into a square, but the problem is it cuts the photo instead of rescaling it. (basically half of the image is lost)

I am pretty sure what I want to do is simple, how can I do that?

Upvotes: 1

Views: 145

Answers (2)

SilentKnight
SilentKnight

Reputation: 14021

Anyway, the official document is the best teacher, check here

Upvotes: 0

Saritha
Saritha

Reputation: 488

Instead of using,

Bitmap.createBitmap(bitmap, crop, 0, newWidth, newHeight);

Use this below line,

Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);

Upvotes: 2

Related Questions