java代码实现图片处理功能。对图片质量进行压缩。
java图片处理有点头疼,找了很多资料。在这里进行一个汇总,记录下个人的体验,也希望对大家有所帮助。
需求:浏览的图片需要在1M一下。
1、真正对图片的质量进行压缩的(不是通过修改图片的高,宽进行缩小图片。就单单缩小图片质量)
优点:不修改图片大小,简便。
缺点:对jpg格式能处理很好,对于gif,png其他格式不适合。
compressPic(图片路径,处理格式);
/**
*
* 修改图片大小
* <p>描述</p>
* @date 2014-7-10 下午4:27:51
* @version
* @param srcFilePath
* @param fileExtName
* @return
* @throws IOException
*/
public static boolean compressPic(String srcFilePath,String fileExtName) throws IOException {
File file = null;
BufferedImage src = null;
FileOutputStream out = null;
ImageWriter imgWrier;
ImageWriteParam imgWriteParams; long start1 = System.currentTimeMillis();
// 指定写图片的方式为 jpg
imgWrier = ImageIO.getImageWritersByFormatName(fileExtName).next();
imgWriteParams = imgWrier.getDefaultWriteParam();
// imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
// null);
// imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
// null); imgWriteParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// 这里指定压缩的程度,参数qality是取值0~1范围内,
imgWriteParams.setCompressionQuality((float) 0.2);
//imgWriteParams.setProgressiveMode(ImageWriteParam.MODE_DISABLED);//
ColorModel colorModel =ImageIO.read(new File(srcFilePath)).getColorModel();// ColorModel.getRGBdefault();
// 指定压缩时使用的色彩模式 imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
colorModel, colorModel.createCompatibleSampleModel(16, 16))); long end1 = System.currentTimeMillis();
System.out.println("11111消耗时间:"+((double)end1-(double)start1)/1000+"秒");
try {
if (isBlank(srcFilePath)) {
return false;
} else {
file = new File(srcFilePath);
src = ImageIO.read(file);
out = new FileOutputStream(srcFilePath); System.out.println("22222");
imgWrier.reset(); // 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
// OutputStream构造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
System.out.println("3333333");
// 调用write方法,就可以向输入流写图片 long start4 = System.currentTimeMillis();
imgWrier.write(null, new IIOImage(src, null, null),
imgWriteParams);
long end4 = System.currentTimeMillis(); System.out.println("4444消耗时间:"+((double)end4-(double)start4)/1000+"秒"); src.flush();
out.flush();
out.close();
imgWrier.dispose();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
} /**
*
* 修改单独图片大小
* <p>描述</p>
* @date 2014-7-10 下午4:26:30
* @version
* @param filePath
* @param createType
* @throws Exception
*/
public static void changeNoneImgSize(String filePath,String createType) throws Exception
{
File tempFile = new File(filePath);
if(tempFile.length()>ImagesUtils.IMAGEMAXSIZE){ long start = System.currentTimeMillis(); compressPic(filePath,createType);
long end = System.currentTimeMillis(); System.out.println(filePath+"消耗时间:"+((double)end-(double)start)/1000+"秒");
} } /**
*
* 修改多个图片大小
* <p>描述</p>
* @date 2014-7-10 下午4:26:52
* @version
* @param file
* @throws Exception
*/
public static void changeManyImgSize(File file) throws Exception
{
try {
// 判断文件是否是文件,如果是文件,获取路径,并计数
if (file.isFile()) {
String fileExtName = file.getName().substring(
(file.getName().lastIndexOf(".") + 1), file.getName().length());
if(ImagesUtils.isImageFile(fileExtName))
changeNoneImgSize(file.getAbsolutePath(), fileExtName);
//ImagesUtils.changeImgSize(file.getAbsolutePath(), ImagesUtils.CREATENEWIMAGETYPE_6);
} else {
// 如果是文件夹,声明一个数组放文件夹和他的子文件
File[] f = file.listFiles();
// 遍历文件件下的文件,并获取路径
for (File file2 : f) {
changeManyImgSize(file2);
}
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
2,这种需要用到一个java-image-scaling-0.8.5.jar包。这种需要设定宽高(我是按照原来比例走的。宽是按照两个A4的宽度走),jar我这里提供出来:http://pan.baidu.com/s/1c0pekVm
优点:简单,格式支持还行。我测试了gif,png都可以用、
缺点:宽高需要设定。
public static boolean compressPic(String srcFilePath,String fileExtName) throws IOException {
try {
BufferedImage sourceImage = ImageIO.read(new File(srcFilePath));
int newwidth = 1700;
double newheight = ((double) sourceImage.getHeight() / (double) sourceImage.getWidth()) * 1700;
ResampleOp resizeOp = new ResampleOp(newwidth, (int) newheight);
resizeOp.setFilter(ResampleFilters.getTriangleFilter());
BufferedImage resizedImage = resizeOp.filter(sourceImage, null);
ImageIO.write(resizedImage, "jpg", new File(srcFilePath));
} catch (Exception e) {
log.error("compressPic error", e);
return false;
}
return true;
}
有点忙,先写这了..................后续还有,不只这点资料。
java代码实现图片处理功能。对图片质量进行压缩。的更多相关文章
- java代码-----计算器,界面+功能+boolean
总结:还是那个不懂代码放在哪里好?不知道怎么定义一些关键性变量.比如boolean 型的. package com.sads; import java.awt.BorderLayout; import ...
- 通过Java代码实现图片的放大和缩小
本文介绍的例子在Android安卓手机上测试通过. 先看看效果吧.可以看到这个开发好的安卓应用有三个按钮:Zoom In缩小图片,Zoom Out放大图片和Save保存. 初始页面: 可以在左边边框自 ...
- 如何使用Java代码给图片增加倒影效果
效果 倒影率为90%时的效果: 倒影率10%时的效果: 实现原理 倒影率作为参数rate 传入Reflection button的事件处理函数: CreateImageWithReflection这个 ...
- Eclipse中实现JAVA代码的自动提示功能
1.打开Eclipse,在.出现时进行代码提示换成任意字母+.出现时的代码提示了(.abcdefghijklmnopqrstuvwxyz):
- java 代码判断图片格式后缀名称
/** * 图片判断 */ private static String getFormatName(Object o) { try { // Create an image input stream ...
- java代码判断图片文件格式, 不是根据文件后缀来判断。
public static final String TYPE_JPG = "jpg"; public static final String TYPE_GIF = "g ...
- java代码实现自动登录功能
通常我们登录某网站,会有选择保存几天,或者是几个星期不用登录,之后输入该网站地址无需登录直接进入主页面,那么这就叫做自动登录,怎么实现呢,下面我以一个小例子来演示一下 登录页面:login.jsp & ...
- JAVA实现根据图片生成缩略图、裁剪、压缩图片
依赖(用来复制文件,可以根据自己的来) <dependency> <groupId>commons-io</groupId> <artifactId>c ...
- java代码实现图片内容转文字
前言 现在的手机已经可以实现拍照转文字了.作为一名程序员,得使用java代码实现这一功能,虽然可能没啥用!!! pom.xml 添加依赖 <dependency> <groupId& ...
随机推荐
- OpenGL:使用顶点数组法绘制正六面体
在今天的opengl的课程以及实验中,我们学习了如何使用顶点数组的方法来绘制图形,但相信还有很多同学对它的实际使用方法不太了解,我们就用我们今天实验课上的实例来简单讲解一下 题目及要求 绘制一个正六面 ...
- 强化学习算法DQN
1 DQN的引入 由于q_learning算法是一直更新一张q_table,在场景复杂的情况下,q_table就会大到内存处理的极限,而且在当时深度学习的火热,有人就会想到能不能将从深度学习中借鉴方法 ...
- 基于skip-gram做推荐系统的想法
一.人工智能之自然语言处理 自然语言处理(Natural Language Processing, NLP),是人工智能的分支科学,意图是使计算机具备处理人类语言的能力. “处理人类语言的能力”要达到 ...
- Alpha阶段个人贡献分及转会人员确定
请各个团队协商确定个人贡献分,评分根据之前个团队确定的规则进行.每个团队的个人贡献分总数为50*N,N为团队的人数. 个人贡献分要求:必须是一个自然数,每个人分数互不相同,并且和为50*N. 请各个团 ...
- 课堂讨论——Alpha版总结会议
我们在课堂上针对第一阶段冲刺过程中存在的问题,展开了激烈的讨论,并投票选出需要改进的最主要三个问题.
- c# dataGridView cell添加下拉框
应用场景: dataGridView需要某一个cell变成下拉框样式. 思路详解: dataGridVie添加固定格式的row. 代码: DataGridViewRow row = new DataG ...
- 基于SSH框架的学生选课质量属性分析
系统:学生选课系统 框架:SSH(Struts2+Spring+Hibernate) 我做的是基于SSH框架的学生选课系统.学生选课系统的特性:①系统响应时间短,能够快速调出课程数据供学生选课提交.② ...
- NESTED最终与外部事务合并在一起提交
NESTED最终与外部事务合并在一起提交
- LOJ#6118 鬼牌
\(\rm upd\):是我假了...这题没有爆精...大家要记得这道题是相对误差\(10^{-6}\)...感谢@foreverlasting的指正. 题是好题,可是标算爆精是怎么回事...要写的和 ...
- 阿里Java编码规范
详细,全面 很不错 阿里 Java编码规范