使用Tiny作为工具进行图片压缩
来源:互联网 发布:mac装win7鼠标不能动 编辑:程序博客网 时间:2024/06/14 06:46
在前端的页面开发过程,图片的压缩一直是相当重要的一件事。之前使用grunt的图片压缩插件,感觉没啥实际的效果。最后,根据从网上找的资料,发现https://tinypng.com/这个网站的图片压缩效果是非常好的。如果是几张图片,那么通过网页操作就可以进行了。
这里介绍的是如果基于Java API来进行批量图片压缩。
在调用SDK前需要获得一个appKey,用邮箱注册就好了。
获取appKey地址:https://tinypng.com/developers
具体使用步骤:
1、引入TinyPng的jar包,我使用maven导入
<dependency> <groupId>com.tinify</groupId> <artifactId>tinify</artifactId> <version>RELEASE</version></dependency>
2、注册APP Key
public class Example { public static void main(String[] args) { Tinify.setKey("YOUR_API_KEY"); // Tiny官网注册一个就好了 }}
3、压缩图片
Source source = Tinify.fromFile("unoptimized.jpg");source.toFile("optimized.jpg");
4、压缩并调整图片大小
Source source = Tinify.fromFile("large.jpg");Options options = new Options() .with("method", "fit") .with("width", 150) .with("height", 100);Source resized = source.resize(options);resized.toFile("thumbnail.jpg");
method总共有scale、fit以及cover三种方式:
scale:按比例缩放图片,必须提供高度或者宽度,不需要同时提供两者。
fit:让图片在指定的大小内,需要同时提供高度和宽度,会有一边存在留白的内容。
cover:剪切图片到指定大小,需要同时提供高度和宽度,系统根据图片自动剪切出有价值的部分。
5、批量压缩图片
这个类是用来批量压缩图片资源的,能够对一个目录下的图片进行批量压缩。
public class TinyPngMain { public static void main(String[] args) throws IOException { String appKey = ""; // Tiny官网注册一个就好了 Tinify.setKey(appKey); float scale = 1.0f; scale = 1.5f; String src = "D:/imgs/normal"; String dest = "D:/imgs/x1.5"; tinyFold(src, dest, Boolean.FALSE, Boolean.FALSE, scale); } public static void tinyFold(String srcFoldPath, String destFoldPath, boolean recursion, boolean keepStructure, float scale) throws IOException { File srcFold = new File(srcFoldPath); if (srcFold.isFile()) { System.out.println(String.format("%s===>%s", srcFoldPath, destFoldPath)); tinyOneFile(srcFoldPath, destFoldPath, scale); return; } File destFold = new File(destFoldPath); if (!destFold.exists()) { destFold.mkdir(); } File[] subFiles = srcFold.listFiles(); if (subFiles == null || subFiles.length == 0) { return; } for (File subFile: subFiles) { if (subFile.isDirectory()) { if (recursion) { // 需要迭代 String recurisonDestFoldPath = destFoldPath; if (keepStructure) { // 保持结构不变 String foldName = subFile.getName(); recurisonDestFoldPath = String.format("%s/%s", destFoldPath, foldName); } tinyFold(subFile.getAbsolutePath(), recurisonDestFoldPath, recursion, keepStructure, scale); } } else { // 如果是文件那么dest带上文件名 String name = subFile.getName(); String destFilePath = String.format("%s/%s", destFoldPath, name); tinyFold(subFile.getAbsolutePath(), destFilePath, recursion, keepStructure, scale); } } } public static void tinyOneFile(String filePath, String outputPath, float scale) throws IOException { if (scale == 1.0f) { Source source = Tinify.fromFile(filePath); source.toFile(outputPath); } else { Source source = Tinify.fromFile(filePath); Image image = ImageIO.read(new File(filePath)); int width = image.getWidth(null); int realWidth = new BigDecimal(((double) width / scale)).setScale(0, BigDecimal.ROUND_HALF_UP).toBigInteger().intValue(); // System.out.println(new File(filePath).getName() + ": " + realWidth); Options options = new Options() .with("method", "scale") .with("width", realWidth); Source resized = source.resize(options); resized.toFile(outputPath); } }}
官网Java API
https://tinypng.com/developers/reference/java
0 0
- 使用Tiny作为工具进行图片压缩
- Android图片压缩框架-Tiny
- 使用Thumbnailator工具压缩图片
- 【图片压缩】使用canvas,html5进行图片压缩
- 使用 Image.SCALE_SMOOTH算法进行图片压缩
- 使用 Image.SCALE_SMOOTH算法进行图片压缩
- 使用ImageMagick对图片进行全面压缩
- 对图片进行压缩
- 对图片进行压缩
- 对图片进行压缩
- 对图片进行压缩
- 将图片进行压缩
- 使用ps工具进行图片分析
- 使用ImageMagick如何对图片进行全面压缩
- 使用ImageMagick如何对图片进行全面压缩
- 使用ZipEntry进行图片压缩下载,文件夹中文乱码问题
- 使用ImageMagick如何对图片进行全面压缩
- 使用ImageMagick如何对图片进行全面压缩
- 第十一周实践项目1(2)--二叉树的构造
- 深入浅出Tabhost+简单入门Demo
- HDU-2003 求绝对值
- 第12周项目1——图基本算法库
- 值类型和引用类型的区别
- 使用Tiny作为工具进行图片压缩
- 硅谷之谜——互动出版网
- 第十一周项目1-中序线索化二叉树的算法验证
- 第十二周项目一 图基本算法库
- 15.c/c++程序员面试宝典-内存分配
- 点击listview时对其中的图片进行改变
- 第十一周项目1-验证算法(2)二叉树构造算法的验证
- elasticsearch的Search Type类型分析
- makefile练习1