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

java压缩图片大小

程序员文章站 2022-05-28 17:00:34
...


package com.xxq.util;
import java.awt.Image;
//旧的jpeg处理类
//import com.sun.image.codec.jpeg.*; 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;

import com.sun.imageio.plugins.jpeg.JPEGImageWriter;



/**
 * 
 * @author Administrator
 *
 */
public class ImgCompressUtil {

	/**
	 * log4j
	 */
	private final static Logger logger = Logger.getLogger(ImgCompressUtil.class
			.getName());

	/**
	 * 图片压缩测试
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		// 图片url,压缩后的宽和高
		String url = "D:/111.jpg";
		int w = 700;
		int h = 500;
		//压缩后的图片路径
			String newName = MD5Util.MD5(url);
			String newDir = "D:/";
			String filePath = newDir + newName + "_"
					+ w + "_" + h + url.substring(url.indexOf("."));
		//压缩
		ImgCompress(url,url,w,h);//url,filePath,w,h
	}

	/**
	 * 外部调用方法
	 * @param url
	 * @param w
	 * @param h
	 */
	public static String ImgCompress(String url, String filePath, int w, int h) {
			// 压缩质量 百分比
			float JPEGcompression = 0.7f;

			// 本地图片存储路径
			String name = url.substring(url.lastIndexOf("/") + 1);
						
			// 截取url中最后一个“/”之后的字符串为name
			logger.info("url:===========" + url);
			logger.info("name:=========" + name);
			
			//压缩主方法
			 return ImgCompress(filePath, url, name, w, h, JPEGcompression);
		
	}
	
	/**
	 * 图片压缩主方法
	 * 
	 * @param newDir
	 *            图片所在的文件夹路径
	 * @param file
	 *            图片路径
	 * @param name
	 *            图片名
	 * @param w
	 *            目标宽
	 * @param h
	 *            目标高
	 * @param JPEGcompression
	 *            压缩质量/百分比
	 * @author zhouqz
	 */
	public static String ImgCompress(String filePath, String url, String name,
			int w, int h, float JPEGcompression) {
		File file = new File(url);
		if (!(file.exists() && file.canRead())) {
			filePath = "/var/upload/404.jpg";
		}else{
			try {
				BufferedImage bufferedImage =  ImageIO.read(file);

				int new_w = w;
				int new_h = h; 

				BufferedImage image_to_save = new BufferedImage(new_w, new_h,
						bufferedImage.getType());
				image_to_save.getGraphics().drawImage(
						bufferedImage.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0,
						0, null);
				FileOutputStream fos = new FileOutputStream(filePath); // 输出到文件流

				// 旧的使用 jpeg classes进行处理的方法
				// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
				// JPEGEncodeParam jep =
				// JPEGCodec.getDefaultJPEGEncodeParam(image_to_save);
				/* 压缩质量 */
				// jep.setQuality(per, true);
				// encoder.encode(image_to_save, jep);

				// 新的方法
				int dpi = 300;//分辨率
				saveAsJPEG(dpi, image_to_save, JPEGcompression, fos);
				//关闭输出流
				fos.close();
				//返回压缩后的图片地址
			} catch (IOException ex) {
				logger.log(Level.SEVERE, null, ex);
				filePath = "/var/upload/404.jpg";
			}
		}
		
		return filePath;
		
	}

	/**
	 * 以JPEG编码保存图片
	 * 
	 * @param dpi
	 *            分辨率
	 * @param image_to_save
	 *            要处理的图像图片
	 * @param JPEGcompression
	 *            压缩比
	 * @param fos
	 *            文件输出流
	 * @throws IOException
	 */
	public static void saveAsJPEG(Integer dpi, BufferedImage image_to_save,
			float JPEGcompression, FileOutputStream fos) throws IOException {

		// useful documentation at
		// http://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html
		// useful example program at
		// http://johnbokma.com/java/obtaining-image-metadata.html to output
		// JPEG data

		// old jpeg class
		// com.sun.image.codec.jpeg.JPEGImageEncoder jpegEncoder =
		// com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(fos);
		// com.sun.image.codec.jpeg.JPEGEncodeParam jpegEncodeParam =
		// jpegEncoder.getDefaultJPEGEncodeParam(image_to_save);

		// Image writer
		JPEGImageWriter imageWriter = (JPEGImageWriter) ImageIO
				.getImageWritersBySuffix("jpg").next();
		ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
		imageWriter.setOutput(ios);
		// and metadata
		IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(
				new ImageTypeSpecifier(image_to_save), null);

		// if(dpi != null && !dpi.equals("")){
		//
		// //old metadata
		// //jpegEncodeParam.setDensityUnit(com.sun.image.codec.jpeg.JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
		// //jpegEncodeParam.setXDensity(dpi);
		// //jpegEncodeParam.setYDensity(dpi);
		//
		// //new metadata
		// Element tree = (Element)
		// imageMetaData.getAsTree("javax_imageio_jpeg_image_1.0");
		// Element jfif =
		// (Element)tree.getElementsByTagName("app0JFIF").item(0);
		// jfif.setAttribute("Xdensity", Integer.toString(dpi) );
		// jfif.setAttribute("Ydensity", Integer.toString(dpi));
		//
		// }

		if (JPEGcompression >= 0 && JPEGcompression <= 1f) {

			// old compression
			// jpegEncodeParam.setQuality(JPEGcompression,false);

			// new Compression
			JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter
					.getDefaultWriteParam();
			jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
			jpegParams.setCompressionQuality(JPEGcompression);

		}

		// old write and clean
		// jpegEncoder.encode(image_to_save, jpegEncodeParam);

		// new Write and clean up
		imageWriter.write(imageMetaData,
				new IIOImage(image_to_save, null, null), null);
		ios.close();
		imageWriter.dispose();

	}

}