[Android] Bitmap Resize by size

2017. 10. 30. 18:03Programing/Android / Java

[Example]

1
2
3
4
5
            Bitmap bitmap       = null;
            Bitmap resizeBitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), uri);
                resizeBitmap = Utils.resizeBitmapImageFn(bitmap, 600);
cs


[API]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    public static Bitmap resizeBitmapImageFn(Bitmap bmpSource, int maxResolution) {
        int   iWidth    = bmpSource.getWidth();
        int   iHeight   = bmpSource.getHeight();
        int   newWidth  = iWidth;
        int   newHeight = iHeight;
        float rate      = 0.0f;
 
        if (maxResolution < 1000)
            return bmpSource;
 
        if (iWidth > iHeight) {
            if (maxResolution < iWidth) {
                rate = maxResolution / (float) iWidth;
                newHeight = (int) (iHeight * rate);
                newWidth = maxResolution;
            }
        } else {
            if (maxResolution < iHeight) {
                rate = maxResolution / (float) iHeight;
                newWidth = (int) (iWidth * rate);
                newHeight = maxResolution;
            }
        }
 
        return Bitmap.createScaledBitmap(bmpSource, newWidth, newHeight, true);
    }
cs