java服务器图片压缩的几种方式及效率比较
以下是测试了三种图片压缩方式,通过测试发现使用jdk的ImageIO压缩时间更短,使用Google的thumbnailator更简单,但是thumbnailator在GitHub上的源码已经停止维护了。
1、Google的thumbnailator
pom.xml中引入依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
测试源码:
/**
* @Title: ThumbnailatorUtil
* @Package: com.test.image
* @Description: google Thumbnailator 测试
* @Author: monkjavaer
* @Data: 2019/2/22 14:07
* @Version: V1.0
*/
public class ThumbnailatorUtil { public static void main(String[] args) {
try {
long start = System.nanoTime();
compressPic();
long end = System.nanoTime();
System.out.println("压缩时间:"+(end-start)*0.000000001+"s");
} catch (IOException e) {
e.printStackTrace();
}
} /**
* scale: 按比例
* outputQuality:输出的图片质量,范围:0.0~1.0,1为最高质量。注意使用该方法时输出的图片格式必须为jpg
* @throws IOException
*/
public static void compressPic() throws IOException {
// Thumbnails.of("E:\\3.png")
Thumbnails.of("E:\\154.jpg")
.scale(1f)
.outputQuality(0.25f)
.toFile("E:\\1.jpg");
} }
输出:压缩时间:0.5615769580000001s
2、Java原生ImageIO实现:
/**
* @Title: ImageIOUtil
* @Package: com.test.image
* @Description: ImageIO test
* @Author: monkjavaer
* @Data: 2019/2/22 16:37
* @Version: V1.0
*/
public class ImageIOUtil { public static void main(String[] args) throws IOException {
long start = System.nanoTime();
compressPic("E:\\154.jpg", "E:\\1.jpg", 0.25f);
long end = System.nanoTime();
System.out.println("压缩时间:" + (end - start) * 0.000000001 + "s");
} public static void compressPic(String srcFilePath, String descFilePath, Float quality) throws IOException {
File input = new File(srcFilePath);
BufferedImage image = ImageIO.read(input); // 指定写图片的方式为 jpg
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); // 先指定Output,才能调用writer.write方法
File output = new File(descFilePath);
OutputStream out = new FileOutputStream(output);
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios); ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()){
// 指定压缩方式为MODE_EXPLICIT
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// 压缩程度,参数qality是取值0~1范围内
param.setCompressionQuality(quality);
}
// 调用write方法,向输入流写图片
writer.write(null, new IIOImage(image, null, null), param); out.close();
ios.close();
writer.dispose();
}
}
输出:压缩时间:0.44548557000000005s
3、com.sun.image.codec.jpeg.JPEGCodec实现
测试代码:
public class JPEGCodecUtil { public static void main(String[] args) throws IOException {
long start = System.nanoTime();
compressPic(new File("E:\\154.jpg"), new File("E:\\1.jpg"), 1920, 0.25f);
long end = System.nanoTime();
System.out.println("压缩时间:" + (end - start) * 0.000000001 + "s");
} /**
* 缩放图片(压缩图片质量,改变图片尺寸)
* 若原图宽度小于新宽度,则宽度不变!
* @param newWidth 新的宽度
* @param quality 图片质量参数 0.7f 相当于70%质量
* 2015年12月11日
*/
public static void compressPic(File originalFile, File resizedFile,
int newWidth, float quality) throws IOException { if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
} ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null; int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null); if(iWidth < newWidth){
newWidth = iWidth;
}
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
} // This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage(); // Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB); // Copy image to buffered image.
Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose(); // Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null); // Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile); // Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage); param.setQuality(quality, true); encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
} // Example usage }
输出:压缩时间:0.52596054s
java服务器图片压缩的几种方式及效率比较的更多相关文章
- Java中图片压缩处理
原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- JAVA中集合输出的四种方式
在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- java中数组复制的两种方式
在java中数组复制有两种方式: 一:System.arraycopy(原数组,开始copy的下标,存放copy内容的数组,开始存放的下标,需要copy的长度); 这个方法需要先创建一个空的存放cop ...
- java实现图片压缩
java实现图片压缩 package Test; import java.awt.Image; import java.awt.image.BufferedImage; import java.io. ...
- java动态获取WebService的两种方式(复杂参数类型)
java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...
- java 实现md5加密的三种方式与解密
java 实现md5加密的三种方式 CreateTime--2018年5月31日15点04分 Author:Marydon 一.解密 说明:截止文章发布,Java没有实现解密,但是已有网站可以免费 ...
- Python:实现图片裁剪的两种方式——Pillow和OpenCV
原文:https://blog.csdn.net/hfutdog/article/details/82351549 在这篇文章里我们聊一下Python实现图片裁剪的两种方式,一种利用了Pillow,还 ...
随机推荐
- webpack3.0版本的一些改动
npm install --save / npm install -S 项目发布上线之后还会依赖用到的插件,没有这些插件,项目不能运行 npm install --save-dev / npm ins ...
- vue2.0 静态prop和动态prop
动态prop: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- spring 获取ApplicationContext
第一种:获取根目录下的文件名 ApplicationContext ac = new ClassPathXmlApplicationContext("../mvc-dispatcher-se ...
- K近邻法(K-Nearest Neighbor,KNN)
KNN是一种基本分类与回归方法,本篇只总结分类问题中的KNN. 输入:样本的特征向量,对应于特征空间中的点 输出:样本的类别,可取多类 算法思想:给定一个样本类别已知的训练数据集,对于新样本,根据其K ...
- 开源一个一个NodeJS 代理服务器扫描工具,可以用来***
鉴于我朝很多网站访问不了,google等就是大悲剧,之前一直在用VPN,但是公司内网VPN被封,诸多工具也惨遭毒手..我辈怎能容忍. 目前只有代理没有被封,于是搞了个代理扫描工具并开源: https: ...
- Python_高阶函数、装饰器(decorator)
一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...
- Android(java)学习笔记187:多媒体之SurfaceView
1. SurfaceView: 完成单位时间内界面的快速切换(游戏界面流畅感). 我们之前知道一般的View,只能在主线程里面显示,主线程中更新UI.但是SurfaceView可以在子线程中里 ...
- swift 使用计算属性+结构管理内存
class GooClass { deinit { print("aaaaaaaa") } var str = "gooClass" } struct GooS ...
- caffe编译
用make -j带一个参数,可以把项目在进行并行编译,比如在一台双核的机器上,完全可以用make -j4,让make最多允许4个编译命令同时执行,这样可以更有效的利用CPU资源 也就是说make -j ...
- vue工程化之公有CSS、JS文件
1.关于公共的css 在src下面新建public.css,然后在main.js中引入进来 import '@/public.css',这样所有页面中都会使用这个样式了,如果只是部分页面需要,那还是不 ...