首页 > 代码库 > JAVA图片压缩

JAVA图片压缩

package com.lea.test;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CompreeImages {
	private File file = null; 			// 文件对象
	private String inputDir; 			// 输入图路径
	private String outputDir; 			// 输出图路径
	private String inputFileName; 		// 输入图文件名
	private String outputFileName;		// 输出图文件名
	private int outputWidth = 100; 		// 默认输出图片宽
	private int outputHeight = 100; 	// 默认输出图片高
	private boolean proportion = true; 	// 是否等比缩放标记(默认为等比缩放)

	public CompreeImages() { 
		inputDir = "";
		outputDir = "";
		inputFileName = "";
		outputFileName = "";
		outputWidth = 100;
		outputHeight = 100;
	}
	
	public static void main(String[] arg) {
		CompreeImages mypic = new CompreeImages();
		File filePath = new File("F:\\img_test");
		File[] files = filePath.listFiles();
		for (File file : files) {
			String name = file.getName();
			int index = name.lastIndexOf(".");
			StringBuffer sb = new StringBuffer();
			sb.append(name);
			sb.insert(index, "_s");
			mypic.compressPic("F:\\img_test\\", "F:\\img_test\\sc\\", file.getName(), sb.toString(), 120, 120, true);
		}
	}

	public String compressPic() {
		try {
			file = new File(inputDir + inputFileName);
			if (!file.exists()) {
				return "";
			}
			Image img = ImageIO.read(file);
			if (img.getWidth(null) == -1) {
				return "no";
			} else {
				int newWidth;
				int newHeight;
				if (this.proportion == true) { // 判断是否是等比缩放
					// 为等比缩放计算输出的图片宽度及高度
					double rate1 = ((double) img.getWidth(null))/ (double) outputWidth + 0.1;
					double rate2 = ((double) img.getHeight(null))/ (double) outputHeight + 0.1;
					
					// 根据缩放比率大的进行缩放控制
					double rate = rate1 > rate2 ? rate1 : rate2;
					newWidth = (int) (((double) img.getWidth(null)) / rate);
					newHeight = (int) (((double) img.getHeight(null)) / rate);
				} else {
					newWidth = outputWidth; // 输出的图片宽度
					newHeight = outputHeight; // 输出的图片高度
				}
				BufferedImage tag = new BufferedImage((int) newWidth,(int) newHeight, BufferedImage.TYPE_INT_RGB);

				tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight,Image.SCALE_SMOOTH), 0, 0, null);
				FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);// JPEGImageEncoder可适用于其他图片类型的转换
				encoder.encode(tag);
				out.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return "ok";
	}

	public String compressPic(String inputDir, String outputDir,String inputFileName, String outputFileName) {
		this.inputDir = inputDir;
		this.outputDir = outputDir;
		this.inputFileName = inputFileName;
		this.outputFileName = outputFileName;
		return compressPic();
	}

	/**
	 * 
	 * @param inputDir 	输入图路径
	 * @param outputDir	输出图路径
	 * @param inputFileName		输入图文件名
	 * @param outputFileName	输出图文件名
	 * @param width			设置图片宽
	 * @param height		设置图片长
	 * @param gp		是否是等比缩放 标记
	 * @return
	 */
	public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height,boolean gp) {
		this.inputDir = inputDir;
		this.outputDir = outputDir;
		this.inputFileName = inputFileName;
		this.outputFileName = outputFileName;
		setWidthAndHeight(width, height);
		this.proportion = gp;
		return compressPic();
	}


	public void setInputDir(String inputDir) {
		this.inputDir = inputDir;
	}

	public void setOutputDir(String outputDir) {
		this.outputDir = outputDir;
	}

	public void setInputFileName(String inputFileName) {
		this.inputFileName = inputFileName;
	}

	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}

	public void setOutputWidth(int outputWidth) {
		this.outputWidth = outputWidth;
	}

	public void setOutputHeight(int outputHeight) {
		this.outputHeight = outputHeight;
	}

	public void setWidthAndHeight(int width, int height) {
		this.outputWidth = width;
		this.outputHeight = height;
	}
}

JAVA图片压缩