图片压缩工具Thumbnailator的使用
生活随笔
收集整理的這篇文章主要介紹了
图片压缩工具Thumbnailator的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? Thumbnailator 是一個為Java界面更流暢的縮略圖生成庫。從API提供現有的圖像文件和圖像對象的縮略圖中簡化了縮略過程,兩三行代碼就能夠從現有圖片生成縮略圖,且允許微調縮略圖生成,同時保持了需要寫入到最低限度的代碼量。同時還支持根據一個目錄批量生成縮略圖。
http://code.google.com/p/thumbnailator/
版本:thumbnailator-0.4.8.jar
package com.zspr.utils.img;import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Positions;/*** 圖片工具* @author jichao**/ public class Pic {/*** 指定大小進行縮放* @param srcUrl 源圖片地址* @param targetUrl 目標圖片地址* @param width 寬* @param height 高* @throws IOException*/public static void resize(String srcUrl,String targetUrl,int width,int height) throws IOException {/** size(width,height) 若圖片橫比200小,高比300小,不變* 若圖片橫比200小,高比300大,高縮小到300,圖片比例不變 若圖片橫比200大,高比300小,橫縮小到200,圖片比例不變* 若圖片橫比200大,高比300大,圖片按比例縮小,橫為200或高為300*/Thumbnails.of(srcUrl).size(width, height).toFile(targetUrl);}/*** 按照比例進行縮放* @param srcUrl 源圖片地址* @param targetUrl 目標圖片地址* @param num 質量比例如 0.8* @throws IOException*/public static void scale(String srcUrl,String targetUrl,double num) throws IOException {Thumbnails.of(srcUrl).scale(num).toFile(targetUrl );}/*** 水印* @param srcUrl 源圖片地址* @param targetUrl 目標圖片地址* @param width 寬* @param height 高* @param num 質量比例如 0.8* @param pos 顯示位置: Positions.BOTTOM_RIGHT * @throws IOException*/public static void watermark(String srcUrl,String targetUrl,int width,int height,float num,Positions pos) throws IOException {Thumbnails.of(srcUrl).size(width,height).watermark(pos,ImageIO.read(new File(targetUrl)), num).outputQuality(num).toFile(targetUrl);}/*** 裁剪* @param srcUrl 源圖片地址* @param targetUrl 目標圖片地址* @param width 寬* @param height 高* @param pos 顯示位置: Positions.BOTTOM_RIGHT * @param x 區(qū)域寬度 * @param y 區(qū)域高度* @throws IOException*/public static void cut(String srcUrl,String targetUrl,int width,int height,Positions pos,int x,int y)throws IOException {Thumbnails.of(srcUrl).sourceRegion(pos,x,y).size(width, height).keepAspectRatio(false).toFile(targetUrl);}/*** 裁剪--指定坐標/大小* @param srcUrl 源圖片地址* @param targetUrl 目標圖片地址* @param width 寬* @param height 高* @param pointA_1 坐標A1 * @param pointA_2坐標A2 * @param pointB_1坐標B1* @param pointB_2坐標B2* @throws IOException*/public static void cut(String srcUrl,String targetUrl,int width,int height,int pointA_1,int pointA_2,int pointB_1,int pointB_2) throws IOException {Thumbnails.of(srcUrl).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height).keepAspectRatio(false).toFile(targetUrl);}/*** 轉化圖像格式* @param srcUrl 源圖片地址* @param targetUrl 目標圖片地址* @param width 寬* @param height 高* @param format 格式 如png/gif/jpg* @throws IOException*/public static void format(String srcUrl,String targetUrl,int width,int height,String format) throws IOException {Thumbnails.of(srcUrl).size(width, height).outputFormat(format).toFile(targetUrl);} }
總結
以上是生活随笔為你收集整理的图片压缩工具Thumbnailator的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 驱动力
- 下一篇: vim插件——auto-pairs