本文介绍了Java的2D和调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些旧的Java 2D code我想重复使用,但不知道,这是获得最高质量的图像的最佳方法是什么?

 公共静态的BufferedImage getScaled(BufferedImage的imgSrc,DIM维){

//这个code确保加载图像中所有的像素。
图像缩放= imgSrc.getScaledInstance(
dim.width,dim.height,Image.SCALE_SMOOTH);

//这个code确保加载图像中所有的像素。
图片临时=新的ImageIcon(缩放).getImage();

//创建一个缓冲的图像。
BufferedImage中的BufferedImage =新的BufferedImage(temp.getWidth(空)
temp.getHeight(空),BufferedImage.TYPE_INT_RGB);

//复制图像缓冲的图像。
图形G = bufferedImage.createGraphics();
//清晰的背景和油漆的形象。
g.setColor(Color.white);
g.fillRect(0,0,temp.getWidth(空),temp.getHeight(空));
g.drawImage(温度,0,0,NULL);
g.dispose();


// j2d的图像缩放质量比较差,尤其是当
//比例缩小的图像,以一小得多的尺寸。我们会后过滤器
//使用了一招我们的图片发现,在
// http://blogs.cocoondev.org/mpo/archives/003584.html
//增加了感知质量....
浮origArea = imgSrc.getWidth()* imgSrc.getHeight();
浮newArea = dim.width * dim.height;
如果(newArea&其中; =(origArea / 2)){
的BufferedImage = blurImg(BufferedImage的);
}

返回的BufferedImage;
}

公共静态的BufferedImage blurImg(BufferedImage的SRC){
//软化因子 - 增加,增加模糊强度
浮softenFactor = 0.010f;
//卷积​​核(模糊)
浮动[] softenArray = {
0,softenFactor,0,
softenFactor,1-(softenFactor * 4),softenFactor,
0,softenFactor,0};

内核内核=新的内核(3,3,softenArray);
缔约方会议的ConvolveOp =新的ConvolveOp(内核,ConvolveOp.EDGE_NO_OP,NULL);
返回cOp.filter(SRC,NULL);
}
 

解决方案

克里斯·坎贝尔对图像缩放,一个优秀的和详细的写了 - 看

切特·哈泽和罗曼盖伊也有详细的内容非常丰富写了图像缩放在他们的著作, 巨富客户

I have some old Java 2D code I want to reuse, but was wondering, is this the best way to get the highest quality images?

    public static BufferedImage getScaled(BufferedImage imgSrc, Dimension dim) {

	//	This code ensures that all the pixels in the image are loaded.
	Image scaled = imgSrc.getScaledInstance(
			dim.width, dim.height, Image.SCALE_SMOOTH);

	// This code ensures that all the pixels in the image are loaded.
	Image temp = new ImageIcon(scaled).getImage();

	// Create the buffered image.
	BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
			temp.getHeight(null), BufferedImage.TYPE_INT_RGB);

	// Copy image to buffered image.
	Graphics g = bufferedImage.createGraphics();
	// Clear background and paint the image.
	g.setColor(Color.white);
	g.fillRect(0, 0, temp.getWidth(null),temp.getHeight(null));
	g.drawImage(temp, 0, 0, null);
	g.dispose();


	// j2d's image scaling quality is rather poor, especially when
	// scaling down an image to a much smaller size. We'll post filter
	// our images using a trick found at
	// http://blogs.cocoondev.org/mpo/archives/003584.html
	// to increase the perceived quality....
	float origArea = imgSrc.getWidth() * imgSrc.getHeight();
	float newArea = dim.width * dim.height;
	if (newArea <= (origArea / 2.)) {
		bufferedImage = blurImg(bufferedImage);
	}

	return bufferedImage;
}

public static BufferedImage blurImg(BufferedImage src) {
	// soften factor - increase to increase blur strength
	float softenFactor = 0.010f;
	// convolution kernel (blur)
	float[] softenArray = {
			0, 				softenFactor, 		0,
			softenFactor, 1-(softenFactor*4), softenFactor,
			0, 				softenFactor, 		0};

	Kernel kernel = new Kernel(3, 3, softenArray);
	ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
	return cOp.filter(src, null);
}
解决方案

Chris Campbell has an excellent and detailed write-up on scaling images - see this article.

Chet Haase and Romain Guy also have a detailed and very informative write-up of image scaling in their book, Filthy Rich Clients.

这篇关于Java的2D和调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:28