Java 图片压缩

先上传压缩后的效果图一张:

Java 图片压缩

图片1是压缩前,2是压缩后,这是以最大压缩比压缩的,效果只有自己去看了。

package zhangao.main;

import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;

//这个类重写了setCompressionQuality方法,因为在压缩JPEG图片的时候可能会出现问题
class MyImageWriteParam extends JPEGImageWriteParam {

	public MyImageWriteParam() {
		super(Locale.getDefault());
	}

	public void setCompressionQuality(float quality) {
		if (quality < 0.0F || quality > 1.0F) {
			throw new IllegalArgumentException("Quality out-of-bounds!");
		}
		this.compressionQuality = 256 - (quality * 256);
	}

}

public class Main {

	public void compressJpegFile(File infile, File outfile, float compressionQuality) {
		try {
			// 检索要压缩的图片
			RenderedImage rendImage = ImageIO.read(infile);

			// 找到一个jpeg writer
			ImageWriter writer = null;
			Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
			if (iter.hasNext()) {
				writer = (ImageWriter) iter.next();
			}

			// 准备输出文件
			ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
			writer.setOutput(ios);

			// 设置压缩比
			ImageWriteParam iwparam = new MyImageWriteParam();
			iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
			iwparam.setCompressionQuality(compressionQuality);

			// 写图片
			writer.write(null, new IIOImage(rendImage, null, null), iwparam);

			// 最后清理
			ios.flush();
			writer.dispose();
			ios.close();
		} catch (IOException e) {
		}
	}

	public static void main(String[] args) {
		String initSrc = "c:\\1.jpg";
		String dest = "c:\\2.jpg";
		new Main().compressJpegFile(new File(initSrc), new File(dest), 1);
	}
}


但是出现了异常(在windows下不会),我的环境是JDK1.4+TOMCAT5.5,系统是AIX5.3,都是很旧的版本,但是没办法,出现的异常如下:

Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable. 

NoClassDefFoundError: sun/awt/X11GraphicsEnvironment.

解决方式:

修改CATALINA.SH文件,我把原JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=GBK"改为JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=GBK;-Djava.awt.headless=true"即可解决这个问题,我甚至还在程序中加入了System.setProperty("java.awt.headless", "true");,但是不知道为什么压缩速度没windows下快。



上一篇:create-react-app 创建的项目添加less支持


下一篇:html5压缩图片并上传