博客
关于我
java图片压缩技术-不错
阅读量:575 次
发布时间:2019-03-11

本文共 4024 字,大约阅读时间需要 13 分钟。

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 CompressPic {	/*******************************************************************************	 * 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法	 * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))	 */	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 CompressPic() { // 初始化变量		inputDir = "";		outputDir = "";		inputFileName = "";		outputFileName = "";		outputWidth = 100;		outputHeight = 100;	}	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;	}	/*	 * 获得图片大小 传入参数 String path :图片路径	 */	public long getPicSize(String path) {		file = new File(path);		return file.length();	}	// 图片处理	public String compressPic() {		try {			// 获得源文件			file = new File(inputDir);			//System.out.println(inputDir + inputFileName);			if (!file.exists()) {				//throw new Exception("文件不存在");			}			Image img = ImageIO.read(file);			// 判断图片格式是否正确			if (img.getWidth(null) == -1) {				System.out.println(" can't read,retry!" + "
"); 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); /* * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 */ tag.getGraphics().drawImage( img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); File f=new File(outputDir); if (!f.exists()){ f.mkdirs(); } FileOutputStream out = new FileOutputStream(outputDir + outputFileName); // JPEGImageEncoder可适用于其他图片类型的转换 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } catch (IOException ex) { ex.printStackTrace(); } return "ok"; } public String compressPic(String outputDir, String inputFileName, String outputFileName) { // 输出图路径 this.outputDir = outputDir; // 输入图文件名 this.inputFileName = inputFileName; // 输出图文件名 this.outputFileName = outputFileName; return compressPic(); } 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(); }}

转载地址:http://ielvz.baihongyu.com/

你可能感兴趣的文章
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>