Java實現(xiàn)圖片裁剪功能的示例詳解
前言
本文提供將圖片按照自定義尺寸進(jìn)行裁剪的Java工具類,一如既往的實用主義。
Maven依賴
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
代碼
不廢話,上代碼。
package ai.guiji.csdn.tool;
import cn.hutool.core.util.IdUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import org.bytedeco.javacpp.Loader;
import java.io.File;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
/**
* @Program: csdn @ClassName: CutOutTool @Author: 劍客阿良_ALiang @Date: 2022-01-23 18:27 @Description:
* 裁剪工具 @Version: V1.0
*/
public class CutOutTool {
/**
* 圖片裁剪
*
* @param imagePath 圖片地址
* @param outputDir 臨時目錄
* @param startX 裁剪起始x坐標(biāo)
* @param startY 裁剪起始y坐標(biāo)
* @param weight 裁剪寬度
* @param height 裁剪高度
* @throws Exception 異常
*/
public static String cutOutImage(
String imagePath,
String outputDir,
Integer startX,
Integer startY,
Integer weight,
Integer height)
throws Exception {
List<String> paths = Splitter.on(".").splitToList(imagePath);
String ext = paths.get(paths.size() - 1);
if (!Arrays.asList("png", "jpg").contains(ext)) {
throw new Exception("format error");
}
String resultPath =
Joiner.on(File.separator).join(Arrays.asList(outputDir, IdUtil.simpleUUID() + "." + ext));
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
ProcessBuilder builder =
new ProcessBuilder(
ffmpeg,
"-i",
imagePath,
"-vf",
MessageFormat.format(
"crop={0}:{1}:{2}:{3}",
String.valueOf(weight),
String.valueOf(height),
String.valueOf(startX),
String.valueOf(startY)),
"-y",
resultPath);
builder.inheritIO().start().waitFor();
return resultPath;
}
public static void main(String[] args) throws Exception {
System.out.println(
cutOutImage(
"C:\\Users\\yi\\Desktop\\2054011.jpg", "C:\\Users\\yi\\Desktop\\", 0, 0, 1920, 2160));
}
}代碼說明:
1、cutOutImage方法參數(shù)分別為圖片路徑、輸出臨時目錄、起始坐標(biāo)x值、起始坐標(biāo)y值、裁剪寬度、裁剪高度。
2、采用uuid作為臨時輸出唯一id,避免重復(fù)。
3、對文件后綴格式做了校驗,可以按照需求自行調(diào)整。
4、裁剪尺寸不能超出圖片限制,按照需求自行調(diào)整。
驗證一下
準(zhǔn)備的圖片如下

執(zhí)行結(jié)果
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers ? built with gcc 10.2.0 (Rev5, Built by MSYS2 project) ? configuration: --prefix=.. --disable-iconv --disable-opencl --disable-sdl2 --disable-bzlib --disable-lzma --disable-linux-perf --enable-shared --enable-version3 --enable-runtime-cpudetect --enable-zlib --enable-libmp3lame --enable-libspeex --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-openssl --enable-libopenh264 --enable-libvpx --enable-libfreetype --enable-libopus --enable-cuda --enable-cuvid --enable-nvenc --enable-libmfx --enable-w32threads --enable-indev=dshow --target-os=mingw32 --cc='gcc -m64' --extra-cflags=-I../include/ --extra-ldflags=-L../lib/ --extra-libs='-static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lgcc_eh -lWs2_32 -lcrypt32 -lpthread -lz -lm -Wl,-Bdynamic -lole32 -luuid' ? libavutil ? ? ?56. 51.100 / 56. 51.100 ? libavcodec ? ? 58. 91.100 / 58. 91.100 ? libavformat ? ?58. 45.100 / 58. 45.100 ? libavdevice ? ?58. 10.100 / 58. 10.100 ? libavfilter ? ? 7. 85.100 / ?7. 85.100 ? libswscale ? ? ?5. ?7.100 / ?5. ?7.100 ? libswresample ? 3. ?7.100 / ?3. ?7.100 Input #0, image2, from 'C:\Users\yi\Desktop\2054011.jpg': ? Duration: 00:00:00.04, start: 0.000000, bitrate: 255438 kb/s ? ? Stream #0:0: Video: mjpeg (Progressive), yuvj444p(pc, bt470bg/unknown/unknown), 3840x2160, 25 tbr, 25 tbn, 25 tbc Stream mapping: ? Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native)) Press [q] to stop, [?] for help Output #0, image2, to 'C:\Users\yi\Desktop\\d1013fbee79e4380a01c574addf72afb.jpg': ? Metadata: ? ? encoder ? ? ? ? : Lavf58.45.100 ? ? Stream #0:0: Video: mjpeg, yuvj444p(pc), 1920x2160, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc ? ? Metadata: ? ? ? encoder ? ? ? ? : Lavc58.91.100 mjpeg ? ? Side data: ? ? ? cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A frame= ? ?1 fps=0.0 q=10.4 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.201x ? ? video:234kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown C:\Users\yi\Desktop\\d1013fbee79e4380a01c574addf72afb.jpg Process finished with exit code 0
結(jié)果圖如下

以上就是Java實現(xiàn)圖片裁剪功能的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Java圖片裁剪的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis 探秘之#{} 與 ${} 參傳差異解碼(數(shù)據(jù)庫連接池筑牢數(shù)據(jù)交互
本文詳細(xì)介紹了MyBatis中的`#{}`和`${}`的區(qū)別與使用場景,包括預(yù)編譯SQL和即時SQL的區(qū)別、安全性問題,以及如何正確使用數(shù)據(jù)庫連接池來提高性能,感興趣的朋友一起看看吧2024-12-12
Java并發(fā)程序刺客之假共享的原理及復(fù)現(xiàn)
前段時間在各種社交平臺“雪糕刺客”這個詞比較火,而在并發(fā)程序中也有一個刺客,那就是假共享。本文將通過示例詳細(xì)講解假共享的原理及復(fù)現(xiàn),需要的可以參考一下2022-08-08
Java日常練習(xí)題,每天進(jìn)步一點點(24)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例
這篇文章主要介紹了java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringData JPA基本/高級/多數(shù)據(jù)源的使用詳解
這篇文章主要介紹了SpringData JPA基本/高級/多數(shù)據(jù)源的使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
詳解Java中的println輸入和toString方法的重寫問題
這篇文章主要介紹了Java中的println輸入和toString方法的重寫,一個對象數(shù)組在調(diào)用Arrays.toString打印時,相當(dāng)于遍歷數(shù)組,然后打印里邊每個對象,這再打印對象就調(diào)用對象自己的toString了,需要的朋友可以參考下2022-04-04
Spring Boot集成MyBatis-Plus 自定義攔截器實現(xiàn)動態(tài)表名切換功能
本文介紹了如何在SpringBoot項目中集成MyBatis-Plus,并通過自定義攔截器實現(xiàn)動態(tài)表名切換,此外,還探討了MyBatis攔截器在其他場景中的應(yīng)用,如SQL日志記錄、多租戶數(shù)據(jù)隔離、數(shù)據(jù)權(quán)限控制等,感興趣的朋友跟隨小編一起看看吧2024-11-11
MyBatis注解方式之@Update/@Delete使用詳解
這篇文章主要介紹了MyBatis注解方式之@Update/@Delete使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

