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

java高质量缩放图片的示例代码

程序员文章站 2022-06-24 21:58:09
可按照比例缩放,也可以指定宽高import com.sun.image.codec.jpeg.jpegimageencoder;import com.sun.image.codec.jpeg.jpeg...

可按照比例缩放,也可以指定宽高

import com.sun.image.codec.jpeg.jpegimageencoder;
import com.sun.image.codec.jpeg.jpegcodec;
import com.sun.image.codec.jpeg.jpegencodeparam;
import javax.swing.*;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.awt.*;
import java.awt.image.bufferedimage;
import java.awt.image.kernel;
import java.awt.image.convolveop;
 
public class imageutil {
   /**
   * 
   * @param originalfile 原文件
   * @param resizedfile 压缩目标文件
   * @param newwidth 压缩后的图片宽度
   * @param quality 压缩质量(0到1之间,越高质量越好)
   * @throws ioexception
   */

	public static void resize(file originalfile, file resizedfile,
			int newwidth, float quality) throws ioexception {
 
		if (quality > 1) {
			throw new illegalargumentexception(
					"quality has to be between 0 and 1");
		}
 
		imageicon ii = new imageicon(originalfile.getcanonicalpath());
		image i = ii.getimage();
		image resizedimage = null;
 
		int iwidth = i.getwidth(null);
		int iheight = i.getheight(null);
        //比例缩放
		if (iwidth > iheight) {
			resizedimage = i.getscaledinstance(newwidth, (newwidth * iheight)
					/ iwidth, image.scale_smooth);
		} else {
			resizedimage = i.getscaledinstance((newwidth * iwidth) / iheight,
					newwidth, image.scale_smooth);
		}
        //指定宽高
		image temp = new imageicon(resizedimage).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();
 
		// soften.
		float softenfactor = 0.05f;
		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);
		bufferedimage = cop.filter(bufferedimage, null);
 
		// write the jpeg to a file.
		fileoutputstream out = new fileoutputstream(resizedfile);
 
		// encodes image as a jpeg data stream
		jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
 
		jpegencodeparam param = encoder
				.getdefaultjpegencodeparam(bufferedimage);
 
		param.setquality(quality, true);
 
		encoder.setjpegencodeparam(param);
		encoder.encode(bufferedimage);
	} // example usage
 
	public static void main(string[] args) throws ioexception {
		 file originalimage = new file("c:p7.gif");
		 resize(originalimage, new file("c:p7-0.jpg"),150, 0.7f);
		 resize(originalimage, new file("c:p7-1.jpg"),150, 1f);
	}
}

以上就是java高质量缩放图片的示例代码的详细内容,更多关于java 缩放图片的资料请关注其它相关文章!