Tinify API批处理压缩PNG和JPG(Java版本)
来源:互联网 发布:mac电脑性能在线测试 编辑:程序博客网 时间:2024/06/11 17:07
因为工作的需要,所以用Tinify的Java API写了一个批处理小程序,主要是用来自动压缩UI和美术工具生成的新图片。在这里就分享出来,如果只是单纯的开发,其实真的是很简单,官方给的教程就很清晰了。稍微麻烦点的是搭建java的运行环境什么的以及下载相关的依赖包,可能会遇到一些问题。
这里只简单的说一下过程就贴代码了。
想先了解的,可以先看我之前翻译的官方 api文档:Tinify Java API中文参考文档
我是采用源码直接编译的,直接在Github上把Tinify Java的源代码给下载下来
https://github.com/tinify/tinify-java
IDE使用的是IntelliJ IDEA
一、工具用途需求
编写工具的主要目的有两个
1. 美术资源打包压缩处理,角色和ui图片资源的打包,这个是要嵌入到现在有的打包工具里面去。也就是说会被其他程序来调用。需要接受额外的参数。
2. 批处理来压缩某一个文件夹工具,直接运行jar就可以了。
二、设计思路
根据前面提到的两个使用需求,根据这些需求就可以整理出几个设计点了。
1. 由于每次压缩的图片数量不一致,所以必须支持指定文件夹和遍历文件夹里面的图片来压缩
2. 由于需要被其他程序调用,所以支持传参数过来
3. 支持单独运行,直接读取配置好的相关路径
4. 由于免费的key有500的限制,所以配置的key支持多个,以及自动检测是否失效,自动使用新的key
三、Java相关代码
这里我简单地使用一个Java类来解决。直接贴代码,有详细的注释。有可能会遇到没有下载到相关的jar包的问题,可以自己在pom.xml找到自己相应的jar版本,然后去下载。
这份代码实现了设计思路的前3点,最后多key的还没有实现。如果有人感兴趣的话在后面留言,我到时会把自己完善的代码再发出来。
配置文件:config.properties
key=你申请的keyinput=E:/tinify/inputoutput=E:/tinify/output
TinifyClient.java类源码
import com.tinify.Source;import com.tinify.Tinify;import java.io.*;import java.util.Iterator;import java.util.Properties;import java.util.Scanner;/** * Created by sodaChen on 2017/5/21. */public class TinifyClient{ /** 单个key的最大压缩图片数量 **/ private static final int MAX_IMG = 500; /** 属性配置 **/ private static Properties properties = new Properties(); /** key的索引值 **/ private static int keyIndex; /** 图片数量 **/ private static int imgCount; public static void main(String[] args) throws Exception { System.out.println(System.getProperty("user.dir")); if(args.length == 0) { System.out.println("根据配置来压缩图片"); //读取配置 readConfigHanle(); } else { System.out.println("根据输入参数来压缩图片"); properties.setProperty("key",args[0]); properties.setProperty("input",args[1]); if(args.length == 3) properties.setProperty("output",args[2]); else properties.setProperty("output",args[1]); } //转换换用户自定义的字符串 String str = properties.getProperty("input"); str = str.replaceAll("/","\\\\"); properties.setProperty("input",str); str = properties.getProperty("output"); str = str.replaceAll("/","\\\\"); properties.setProperty("output",str); //设置用户key Tinify.setKey(properties.getProperty("key")); File root = new File(properties.getProperty("input")); File[] files = root.listFiles(); //遍历输入文件夹 readDirectory(files); System.out.println("全部png和jpg压缩完成"); } /** * 读取配置文件来设置属性 */ private static void readConfigHanle() { String confPath = "config.properties"; try { InputStream in = new BufferedInputStream(new FileInputStream(confPath)); //加载属性列表 properties.load(in); in.close(); } catch (Exception e) { System.out.println("读取配置文件错误"); e.printStackTrace(); } } /** * 读取文件夹,并处理里面的png和jpg图片 * @param files */ private static void readDirectory(File[] files) { for (File file : files) { String fileName = file.getName(); if (file.isDirectory()) { //递归读文件夹 readDirectory(file.listFiles()); } else { int index = fileName.lastIndexOf("."); String prefix = fileName.substring(index + 1); if (prefix.equals("png") || prefix.equals("jpg")) //进行压缩 tinifyImg(file.getPath(), properties.getProperty("output")); } } } /** * 连接tinify的API进行压缩图片和保存回本地 * @param inPath 文件路径 * @param outPath 输出路径头 */ private static void tinifyImg(String inPath, String outPath) { try { System.out.println("上传:" + inPath); Source source = Tinify.fromFile(inPath); //确保输入和输出是一样的斜杠符号 inPath = inPath.replaceAll("/","\\\\"); //替换路径保存 String savePath = inPath.replace(properties.getProperty("input"),outPath); System.out.println("保存:" + savePath); File file = new File(savePath); //如果没有文件夹,则根据路径生成文件夹 if(!file.getParentFile().exists()) file.getParentFile().mkdirs(); source.toFile(savePath); System.out.println("完成!!!"); } catch (Exception e) { e.printStackTrace(); System.out.print("上传压缩失败!"); } }}
测试输出结果:
E:\workspaces\JavaProjects\tinifyJava根据配置来压缩图片上传:E:\tinify\input\mail\bg\bg.png保存:E:\tinify\output\mail\bg\bg.png完成!!!上传:E:\tinify\input\mail\mail.png保存:E:\tinify\output\mail\mail.pngDisconnected from the target VM, address: '127.0.0.1:2099', transport: 'socket'完成!!!全部png和jpg压缩完成
四、jar和bat
最后用IntelliJ IDEA生成tinify.jar,再写个bat小脚本,就可以运行使用啦(必须安装java的运行环境)
运行方式:java -jar tinify.jar设置key和路径有两种方式1. 通过设置config.properties里面的属性2. 通过执行jar的时候传奇参数(有参数则程序不会再读取config.properties了)java -jar tinify.jar key input output
- Tinify API批处理压缩PNG和JPG(Java版本)
- JAVA 批处理 调整、压缩png jpg图片大小
- Webp 和 png、jpg格式压缩
- 图片压缩 java 压缩 JPG JPEG PNG GIF
- Tinify Java API中文参考文档
- png转jpg java
- [Python模块学习]用tinify模块压缩和优化图片
- GIF、JPG和PNG
- Linux命令压缩/优化JPG/PNG图片
- Linux命令压缩/优化JPG/PNG图片
- Delphi7 压缩图片(BMP、JPG、PNG)
- 使用tinify批量压缩图片
- JPG和PNG的区别
- 相册图片选取,图片压缩和图片jpg、png格式的转换
- png压缩,jpg压缩不错的网站推荐
- Ubuntu下批处理转换jpg 2 png格式
- Ubuntu下批处理转换jpg 2 png格式
- png jpg
- 面向对象设计原则之里氏代换原则
- 【慕课网】分页页码制作学习笔记
- hdu5781 沈阳day6训练赛
- js判断是不是空对象
- 文章标题
- Tinify API批处理压缩PNG和JPG(Java版本)
- 树(数据结构)
- Android7.0中文API -- SearchView
- 整合Struts2与Spring以及spring的自动装配
- 关于eclipse导出Java项目形成的jar包中没有图片以及图片显示问题
- 三次握手
- Spring源码解析--Spring配置文件解析NamespaceHandler(五)
- jdbc链接数据库
- 计算机图形学——扫描椭圆