Java image resize, maintain aspect ratio

前端 未结 11 1479
盖世英雄少女心
盖世英雄少女心 2020-11-28 19:38

I have an image which I resize:

if((width != null) || (height != null))
{
    try{
        // scale image on disk
        BufferedImage originalImage = ImageI         


        
11条回答
  •  死守一世寂寞
    2020-11-28 20:02

    try this

    float rateX =  (float)jpDisplayImagen.getWidth()/(float)img.getWidth();
    float rateY = (float)jpDisplayImagen.getHeight()/(float)img.getHeight();
    if (rateX>rateY){
        int W=(int)(img.getWidth()*rateY);
        int H=(int)(img.getHeight()*rateY);
        jpDisplayImagen.getGraphics().drawImage(img, 0, 0,W,H, null);
    }
    else{
        int W=(int)(img.getWidth()*rateX);
        int H=(int)(img.getHeight()*rateX);
        jpDisplayImagen.getGraphics().drawImage(img, 0, 0,W,H, null);
    }
    

提交回复
热议问题