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代码实现图片处理功能。对图片质量进行压缩。的更多相关文章

  1. java代码-----计算器,界面+功能+boolean

    总结:还是那个不懂代码放在哪里好?不知道怎么定义一些关键性变量.比如boolean 型的. package com.sads; import java.awt.BorderLayout; import ...

  2. 通过Java代码实现图片的放大和缩小

    本文介绍的例子在Android安卓手机上测试通过. 先看看效果吧.可以看到这个开发好的安卓应用有三个按钮:Zoom In缩小图片,Zoom Out放大图片和Save保存. 初始页面: 可以在左边边框自 ...

  3. 如何使用Java代码给图片增加倒影效果

    效果 倒影率为90%时的效果: 倒影率10%时的效果: 实现原理 倒影率作为参数rate 传入Reflection button的事件处理函数: CreateImageWithReflection这个 ...

  4. Eclipse中实现JAVA代码的自动提示功能

    1.打开Eclipse,在.出现时进行代码提示换成任意字母+.出现时的代码提示了(.abcdefghijklmnopqrstuvwxyz):

  5. java 代码判断图片格式后缀名称

    /** * 图片判断 */ private static String getFormatName(Object o) { try { // Create an image input stream ...

  6. java代码判断图片文件格式, 不是根据文件后缀来判断。

    public static final String TYPE_JPG = "jpg"; public static final String TYPE_GIF = "g ...

  7. java代码实现自动登录功能

    通常我们登录某网站,会有选择保存几天,或者是几个星期不用登录,之后输入该网站地址无需登录直接进入主页面,那么这就叫做自动登录,怎么实现呢,下面我以一个小例子来演示一下 登录页面:login.jsp & ...

  8. JAVA实现根据图片生成缩略图、裁剪、压缩图片

    依赖(用来复制文件,可以根据自己的来) <dependency> <groupId>commons-io</groupId> <artifactId>c ...

  9. java代码实现图片内容转文字

    前言 现在的手机已经可以实现拍照转文字了.作为一名程序员,得使用java代码实现这一功能,虽然可能没啥用!!! pom.xml 添加依赖 <dependency> <groupId& ...

随机推荐

  1. torchvision 批量可视化图片

    1.1 简介 计算机视觉中,我们需要观察我们的神经网络输出是否合理.因此就需要进行可视化的操作. orchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详 ...

  2. Golang Context 详细介绍

    Golang context 本文包含对context实现上的分析和使用方式,分析部分源码讲解比价多,可能会比较枯燥,读者可以直接跳过去阅读使用部分. ps: 作者本着开源分享的精神撰写本篇文章,如果 ...

  3. C++ string 类详解

    字符串是存储在内存的连续字节中的一系列字符.C++ 处理字符串的方式有两种,一种来自 C 语言,常被称为 C-风格字符串,另一种是基于 string 类库的字符串处理方式.C 风格字符串的处理可以参考 ...

  4. mysql select 字段别名是否可以用在 select中或者where中

    select column1+10 as c1,c1+10 as c2 from table1;想实现上面的效果,结果在mysql里面报错了,提示找不到c1这个列; -- 不同的 数据库不一样 一般不 ...

  5. CSAPP lab2 二进制拆弹 binary bombs phase_2

    给出对应于7个阶段的7篇博客 phase_1  https://www.cnblogs.com/wkfvawl/p/10632044.htmlphase_2  https://www.cnblogs. ...

  6. 词频统计 SPEC 20160911

    本文档随时可能修改,并且没有另行通知. 请确保每一次在开始修改你的代码前,读标题中的日期,如果晚于你上次阅读, 请重读一次. 老五在寝室吹牛他熟读过<鲁滨逊漂流记>,在女生面前吹牛热爱&l ...

  7. linux 内核 第二周 操作系统是如何工作的

    姬梦馨 原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一:计算机的三个法宝 存储程序计算机工 ...

  8. C++ Makefile文件编写

    对现有的一个C++动态库文件和调用程序,分别编写Makefile文件,从零开始,这里把自己弄明白的一些东西分享给大家. 1.必须明确Linux下,C++的编译器是g++,C语言的是gcc.网上大多数又 ...

  9. Daily Scrum - 11/23

    今天更新blog时发现了老师对我们daily scrum提的要求,从明天起除了简要记录组会的主要内容之外,还会总结上一个工作日每个组员的工作进度.代码提交情况和燃尽图. 今天会议内容主要是人千.章玮同 ...

  10. freopen stdout 真的更快?

    freopen stdout 真的更快? 在一次数独作业中,我发现大部分同学提交的代码中都使用 freopen 来将 stdout 重新指向目标文件进行文件输出操作.我感到十分好奇,关于 freope ...