How to resize bitmap to maximum available size?

2019-05-11 03:54发布

I have very large bitmap image. My source

BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_WIDTH = 1000;
            final int REQUIRED_HIGHT = 500;
            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                    && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

i want to resize image correctly, i need resize image to maximum available size

for example

i downloaded image size 4000x4000 px and my phone supported 2000x1500 px size i need anderstend how size suported my phone? then i resize image to 2000x1500 (for example)

1条回答
神经病院院长
2楼-- · 2019-05-11 04:40

Here you have good to resize bitmap to maximum avaliabe size :

public void onClick() //for example 
{
/*
Getting screen diemesions
*/
WindowManager w = getWindowManager();
        Display d = w.getDefaultDisplay();
        int width = d.getWidth();
        int height = d.getHeight(); 
// "bigging" bitmap
Bitmap nowa = getResizedBitmap(yourbitmap, width, height);
}

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

I hope I helped

查看更多
登录 后发表回答