图片压缩,阿里巴巴SimpleImage 相关jar文件。其中包含依赖jar:
commons-io-2.4.jar;commons-lang.jar;commons-logging-1.1.1.jar
jai_codec-1.1.3.jar;jai_core-1.1.3.jar;
包含jar:aliSimpleImage.jar。aliSimpleImage.jar是对SimpleImage中simpleimage.core\src\main\java文件打包。
SimpleImage github地址:https://github.com/alibaba/simpleimage
测试代码:
public static void main(String[] args) {
File in = new File("d:/desktop/c/1.jpg"); //原图片
File out = new File("d:/desktop/d/ali_bak_400.jpg"); //目的图片
File out1 = new File("d:/desktop/d/ali_bak_50.jpg"); //目的图片
ScaleParameter scaleParam = new ScaleParameter(400, 400); //将图像缩略到1024x1024以内,不足1024x1024则不做任何处理
FileInputStream inStream = null;
FileOutputStream outStream = null;
WriteRender wr = null;
try {
inStream = new FileInputStream(in);
outStream = new FileOutputStream(out);
ImageRender rr = new ReadRender(inStream);
ImageRender sr = new ScaleRender(rr, scaleParam);
wr = new WriteRender(sr, outStream);
wr.render(); //触发图像处理
} catch(Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inStream); //图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
if (wr != null) {
try {
wr.dispose(); //释放simpleImage的内部资源
} catch (SimpleImageException ignore) {
// skip ...
}
}
}
}
1