0

i want to resize my bitmap image to 512px,512px. i use below code ,but when my image resized its very bad and aspect ratio affected. how i can resize my image without aspect ratio?2nd image resized with pc to 512px. enter image description here enter image description here

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;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }
Mahdi
  • 377
  • 2
  • 20
  • 2
    This is not Java code, it's JavaScript. But the formula is what you're looking for: http://dpoisn.com/demos/aspectratio.php Check the code on that page. – durbnpoisn Feb 08 '16 at 17:27

1 Answers1

1

you can resize your bitmap using this.

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

or:

resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);
Shabbir Dhangot
  • 8,089
  • 9
  • 55
  • 75