java - getting the desired aspect ratio for BufferedImage -


i using following codes resize image, can same aspect ratio original image using getscaledinstance function setting 1 parameter negative, automatically maintains other paramter aspect ratio. problem facing bufferedimage, cannot set desired aspect ratio because don't know beforehand values of height generate(width have set 500).i have included image can getting result of running following code. bufferedimage have set 800x600 because have no other option.my previous question resizing image without compromising aspect ratio

public class resizeimage {     public static void main(string[] args) throws ioexception {         bufferedimage img1 = new bufferedimage(800, 600,                 bufferedimage.type_int_rgb);         img1.creategraphics()                 .drawimage(                         imageio.read(                                 new file(                                         "c:/users/public/pictures/sample pictures/desert.jpg"))                                 .getscaledinstance(500, -1, image.scale_smooth),                         0, 0, null);         imageio.write(img1, "jpg", new file(                 "c:/users/public/pictures/sample pictures/test/desert.jpg"));        } } 

bufferedimage creating blank background

you can math original image aspect ratio

double ratio = img1.getwidth() / img1.getheigth(); 

and invoke getscaledinstance ratio

blabla.getscaledinstance(500, (int)500 / ratio, image.scale_smooth); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -