在java中调整图像大小

我有一个png图像,我想调整它。我怎么能这样做? 虽然我已经完成了这个我无法理解的片段。

如果您有Image ,则重新设置它不需要任何其他库。 做就是了:

 Image newImage = yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT); 

实际上, newWidth和newHeight 替换为指定图像的尺寸。
注意最后一个参数:它告诉运行时要用于resize的算法

有些算法会产生非常精确的结果,但这些算法需要很长时间才能完成。
您可以使用以下任何算法:

  • Image.SCALE_DEFAULT :使用默认的图像缩放算法。
  • Image.SCALE_FAST :选择一种图像缩放算法,该算法比缩放图像的平滑度具有更高的缩放速度优先级。
  • Image.SCALE_SMOOTH :选择图像缩放算法,使图像平滑度优先于缩放速度。
  • Image.SCALE_AREA_AVERAGING :使用Area Averaging图像缩放算法。
  • Image.SCALE_REPLICATE :使用ReplicateScaleFilter类中包含的图像缩放算法。

有关详细信息,请参阅Javadoc 。

我们这样做是为了创建图像的缩略图:

  BufferedImage tThumbImage = new BufferedImage( tThumbWidth, tThumbHeight, BufferedImage.TYPE_INT_RGB ); Graphics2D tGraphics2D = tThumbImage.createGraphics(); //create a graphics object to paint to tGraphics2D.setBackground( Color.WHITE ); tGraphics2D.setPaint( Color.WHITE ); tGraphics2D.fillRect( 0, 0, tThumbWidth, tThumbHeight ); tGraphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR ); tGraphics2D.drawImage( tOriginalImage, 0, 0, tThumbWidth, tThumbHeight, null ); //draw the image scaled ImageIO.write( tThumbImage, "JPG", tThumbnailTarget ); //write the image to a file 

尝试这个:

 ImageIcon icon = new ImageIcon(UrlToPngFile); Image scaleImage = icon.getImage().getScaledInstance(28, 28,Image.SCALE_DEFAULT);