因为工作的需要,所以用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=你申请的key
input=E:/tinify/input
output=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.png
Disconnected 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