Thumbnailator處理圖片

顶风少年發表於2020-10-18

讀取源圖

of(String... files)

of(File... files)

of(InputStream... inputStreams)

of(URL... urls)

輸出檔案

toFile(String outFilepath)

toFile(File outFile)

toOutputStream(OutputStream os)

縮放

size(int width,int height)

width(int width)

height(int height)

forceSize(int width, int height)

scale(double scaleWidth, double scaleHeight)

scale(double scale)

裁剪

sourceRegion(int x, int y, int width, int height)

sourceRegion(Position position, int width, int height)

sourceRegion(Position position, Size size)

sourceRegion(Region sourceRegion)

sourceRegion(Rectangle region) crop(Position position)

crop(Position position)

覆蓋

allowOverwrite(boolean allowOverwrite)

輸出格式

outputFormat(String format)

輸出質量

outputQuality(float quality)

水印

watermark(BufferedImage image)

watermark(BufferedImage image, float opacity)

watermark(Position position, BufferedImage image, float opacity)

watermark(Watermark w)

旋轉

rotate(double angle)

讀取源圖

of(String... files)

本地的字串路徑,寫的可以傳遞多個但是我試了多個會報錯,最好只用一個。

Thumbnails.of("C:\\Users\\86152\\Desktop\\1\\原圖.png").scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

of(File... files)

將字串抽象成File也可以。

Thumbnails.of(new File("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

of(InputStream... inputStreams)

從輸入流讀取原始檔。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

of(URL... urls)

從網路流讀取原始檔。

Thumbnails.of(new URL("https://images0.cnblogs.com/blog/651487/201410/191023445446341.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

輸出檔案

toFile(String outFilepath)

將圖片輸出到本地,接收字串路徑。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

toFile(File outFile)

將圖片輸出到檔案。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).toFile(new File("C:\\Users\\86152\\Desktop\\1\\1.jpg"));

toOutputStream(OutputStream os)

將圖片輸出到輸出流。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).toOutputStream(new FileOutputStream("C:\\Users\\86152\\Desktop\\1\\1.jpg"));

縮放

size(int width,int height)

size(int width,int height)會對圖片進行縮放,會改變圖片的解析度隨之圖片的大小也會改變,但它並不會嚴格按照我們輸入的width,height進行縮放,因為這樣圖片的比例可能不對稱,最終的結果會稍微的微調。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).size(500, 500).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

width(int width)

width(int width)和size(int width,int height)的區別是它允許你單獨指定width然後它會自動根據width自適應的對height進行縮放。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).width(100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

height(int height)

height(int height)和width(int width)的區別很明顯,它指定height,width會自動根據height自適應的縮放。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).height(100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

forceSize(int width, int height)

forceSize(int width, int height)跟size(int width,int height)的區別是它會嚴格按照引數width,height對圖片進行縮放。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).forceSize(111,222).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

scale(double scaleWidth, double scaleHeight)

scale(double scaleWidth, double scaleHeight)根據比例對圖片進行縮放,它是嚴格按照引數比例的,如果是1則是原圖,如果大於1則解析度也會增大。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(0.1d, 0.5).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

scale(double scale)

scale(double scale)和scale(double scaleWidth, double scaleHeight)的別是,指定一個引數就可以代表width和height。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(0.1d).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

裁剪

sourceRegion(int x, int y, int width, int height)

sourceRegion(int x, int y, int width, int height)四個引數分別是指定從原圖片的X軸,Y軸,向右取width的寬,height的高開始裁剪,裁剪的區域就是新的圖片。

出現以下錯誤,缺少size,也就是說當指定了需要裁剪的圖片區域後,要先變成一個新的圖片必須找到一個容器存放,可以選擇縮放裡的幾個函式。 

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(0, 0, 1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

從下圖看縮圖原圖和這個圖片其實差不多的包括的它們的解析度,但它的大小其實是改變了。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(0, 0, 1920, 1080).size(1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

這次我們將輸出的width,height改成100,100。圖片的解析度和大小都改變了。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(0, 0, 1920, 1080).size(100, 100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

 

換成forceSize後整體的效果就出來了,因為他嚴格按照指定的width和height改變輸出圖片。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(0, 0, 1920, 1080).forceSize(100, 100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

sourceRegion(Position position, int width, int height)

sourceRegion(Position position, int width, int height)是sourceRegion(int x, int y, int width, int height)過載,Position將X,Y封裝成了物件,Position是一個介面它的唯一實現類是Positions。TOP_LEFT代表的是X(0),Y(0)。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(Positions.TOP_LEFT, 1920, 1080).forceSize(1920,1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

sourceRegion(Position position, Size size)

sourceRegion(Position position, Size size)是sourceRegion(int x, int y, int width, int height)過載它將X,Y封裝成了Position,將width,height封裝成了Size,Size是一個介面它的唯一實現類是RelativeSize這個類的構造器接收一個引數,沒錯一個引數就是百分比,而且是width,height的百分比就像scale(double scale)。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(Positions.TOP_LEFT, new RelativeSize(1)).forceSize(1920, 1080)
.toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

sourceRegion(Region sourceRegion)

sourceRegion(Region sourceRegion)其實就是上邊三個過載形式最終組成的物件,Region的構造器是Position和Size。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(new Region(Positions.TOP_LEFT, new RelativeSize(1))).forceSize(1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

sourceRegion(Rectangle region)

sourceRegion(Rectangle region)引數Rectangle跟Region差不多,不過它有無參構造,如果使用無參構造Rectangle()相當於sourceRegion(0, 0, 0, 0)如果用這個會報錯,因為width,height為0。同樣你也可以直接指定它的四個引數。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).sourceRegion(new Rectangle(0, 0, 1920, 1080)).forceSize(1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

crop(Position position)

crop(Position position)這個方法就有點意思了,相比sourceRegion(int x, int y, int width, int height)少了width,height那它是如何指定X,Y的橫向縱向距離呢?謎底是它使用size(int width, int height)。也就是輸出的width,height既是裁剪的width,height。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).crop(Positions.TOP_LEFT).size(500, 500).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

覆蓋

allowOverwrite(boolean allowOverwrite)

是否允許覆蓋原始檔,如果要輸出的圖片已經存在(路徑和檔名相同),預設是覆蓋掉原始檔,使用此方法傳值false則可以避免檔案覆蓋。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).crop(Positions.TOP_LEFT).size(500, 400).allowOverwrite(false).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

輸出格式

outputFormat(String format)

outputFormat(String format)這個方法用於更改輸出檔案的型別,如果指定了輸出型別,那輸出檔名就不要帶檔案型別了。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).outputFormat("png").toFile("C:\\Users\\86152\\Desktop\\1\\1");

輸出質量

outputQuality(float quality)

outputQuality(float quality)設定輸出圖片的質量,0.1-1,1表示最大質量。下面看三種情況下的輸出對比。

1 縮略模式,width,height保持原圖1:1。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

2 縮略模式,width,height保持原圖1:1,outputQuality(1f)輸出全量質量。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).outputQuality(1f).toFile("C:\\Users\\86152\\Desktop\\1\\2.jpg");

3 縮略模式,width,height保持原圖1:1,outputQuality(0.5f)輸出一半質量。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).outputQuality(0.5f).toFile("C:\\Users\\86152\\Desktop\\1\\3.jpg");

水印

watermark(BufferedImage image)

watermark(BufferedImage image)給圖片打水印,BufferedImage是水印圖片的抽象表示。使用ImageIO.read(InputStream in)構造一個水印的圖片的表示類。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg"))).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

watermark(BufferedImage image, float opacity)

watermark(BufferedImage image, float opacity)和watermark(BufferedImage image)的區別是第二個引數它表示水印的不透明度從0.1-1,不指定則是0.5,下圖設定不透明度為0.1

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 0.1f).toFile("C:\\Users\\86152\\Desktop\\1\\2.jpg");

下圖設定不透明度為0.1。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).toFile("C:\\Users\\86152\\Desktop\\1\\3.jpg");

watermark(Position position, BufferedImage image, float opacity)

watermark(Position position, BufferedImage image, float opacity)是上邊兩個的完整體,預設的水印位置在輸出圖片的中間,使用Position指定水印輸出位置。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

如果需要打上多個水印,也可以連續呼叫watermark。下圖在輸出圖底部中間和底部右邊打上水印。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).watermark(Positions.BOTTOM_CENTER,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

watermark(Watermark w)

watermark(Watermark w)其實就是把Position,BufferedImage,opacity封裝成了Watermark物件,可以使水印共同使用。

Watermark watermark1 = new Watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f);

Watermark watermark2 = new Watermark(Positions.BOTTOM_LEFT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f);

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).watermark(watermark1).watermark(watermark2).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

旋轉

rotate(double angle)

rotate(double angle)對輸出圖片做旋轉操作。

Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原圖.png")).scale(1f).rotate(180).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");

 

相關文章