欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何在Java中调整图像大小?

程序员文章站 2022-03-10 17:15:02
...

我不是Java图像处理方面的专家,但是有机会在用户图像上传表单中实现此重新调整大小的功能,该功能要求将较大的图像重新调整为具有固定宽度和高度的较小尺寸。

Graphics2D提供了图像调整大小功能,如下所示:

BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();

根据某些文章,如果要提高调整大小后的图像质量,可以按如下所示添加RenderingHints:

BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

1.例子

这是一个网站快照,下面的程序会将这个图像调整为固定的宽度和高度图像(100 x 100)。

mkyong.jpg(640 x 310)– 46KB

如何在Java中调整图像大小?

2.源代码

ImageTest.java
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/*
 * @author mkyong
 *
 */
public class ImageTest {

	private static final int IMG_WIDTH = 100;
	private static final int IMG_HEIGHT = 100;
	
	public static void main(String [] args){
		
	try{
			
		BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mkyong.jpg"));
		int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
			
		BufferedImage resizeImageJpg = resizeImage(originalImage, type);
		ImageIO.write(resizeImageJpg, "jpg", new File("c:\\image\\mkyong_jpg.jpg")); 
			
		BufferedImage resizeImagePng = resizeImage(originalImage, type);
		ImageIO.write(resizeImagePng, "png", new File("c:\\image\\mkyong_png.jpg")); 
			
		BufferedImage resizeImageHintJpg = resizeImageWithHint(originalImage, type);
		ImageIO.write(resizeImageHintJpg, "jpg", new File("c:\\image\\mkyong_hint_jpg.jpg")); 
			
		BufferedImage resizeImageHintPng = resizeImageWithHint(originalImage, type);
		ImageIO.write(resizeImageHintPng, "png", new File("c:\\image\\mkyong_hint_png.jpg")); 
				
	}catch(IOException e){
		System.out.println(e.getMessage());
	}
		
    }
	
    private static BufferedImage resizeImage(BufferedImage originalImage, int type){
	BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
	Graphics2D g = resizedImage.createGraphics();
	g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
	g.dispose();
		
	return resizedImage;
    }
	
    private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type){
		
	BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
	Graphics2D g = resizedImage.createGraphics();
	g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
	g.dispose();	
	g.setComposite(AlphaComposite.Src);

	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
	RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	g.setRenderingHint(RenderingHints.KEY_RENDERING,
	RenderingHints.VALUE_RENDER_QUALITY);
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	RenderingHints.VALUE_ANTIALIAS_ON);
	
	return resizedImage;
    }	
}

查看结果:

JPG(4KB) PNG(9KB) JPG +提示(4KB) PNG +提示(9KB)
如何在Java中调整图像大小? 如何在Java中调整图像大小? 如何在Java中调整图像大小? 如何在Java中调整图像大小?

结论

如您所看到的,重新调整大小的PNG图像的文件大小比重新调整大小的JPEG图像大,但是重新调整大小后的PNG质量比JPG更好。 这些提示在JPG格式下不会产生任何效果,但是我也没有看到PNG和PNG +提示之间的任何区别,即使有人告诉添加渲染提示会提高图像质量?

PS:如果您知道一些????,请分享Java映像技巧

参考文献

  1. http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageIO.html
  2. http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferedImage.html
  3. http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html
  4. http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
  5. http://www.servletsuite.com/servlets/imagescale.htm
  6. http://www.componenthouse.com/article-20

From: https://mkyong.com/java/how-to-resize-an-image-in-java/