Java實(shí)現(xiàn)圖片裁剪功能的示例詳解
前言
本文提供將圖片按照自定義尺寸進(jìn)行裁剪的Java工具類,一如既往的實(shí)用主義。
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 臨時(shí)目錄 * @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ù)分別為圖片路徑、輸出臨時(shí)目錄、起始坐標(biāo)x值、起始坐標(biāo)y值、裁剪寬度、裁剪高度。
2、采用uuid作為臨時(shí)輸出唯一id,避免重復(fù)。
3、對文件后綴格式做了校驗(yàn),可以按照需求自行調(diào)整。
4、裁剪尺寸不能超出圖片限制,按照需求自行調(diào)整。
驗(yàn)證一下
準(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實(shí)現(xiàn)圖片裁剪功能的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Java圖片裁剪的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)高效隨機(jī)數(shù)算法的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)高效隨機(jī)數(shù)算法的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02使用Eclipse開發(fā)工具如何解決Java Compiler中Annotation Processin不出現(xiàn)的問題
這篇文章主要介紹了使用Eclipse開發(fā)工具如何解決Java Compiler中Annotation Processin不出現(xiàn)的相關(guān)資料,需要的朋友可以參考下2015-11-114個(gè)Java8中你需要知道的函數(shù)式接口分享
Java?8?中提供了許多函數(shù)式接口,包括Function、Consumer、Supplier、Predicate?等等。本文主要來和大家介紹一下它們的具體使用,需要的可以參考一下2023-04-04使用java基于pushlet和bootstrap實(shí)現(xiàn)的簡單聊天室
這篇文章主要介紹了使用java基于pushlet和bootstrap實(shí)現(xiàn)的簡單聊天室的相關(guān)資料,需要的朋友可以參考下2015-03-03java實(shí)現(xiàn)圖片上傳至本地實(shí)例詳解
我們給大家分享了關(guān)于java實(shí)現(xiàn)圖片上傳至本地的實(shí)例以及相關(guān)代碼,有需要的朋友參考下。2018-08-08vscode搭建java開發(fā)環(huán)境的實(shí)現(xiàn)步驟
本文主要介紹了vscode搭建java開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Java+MySQL實(shí)現(xiàn)設(shè)計(jì)優(yōu)惠券系統(tǒng)
這篇文章主要介紹了Java+MySQL實(shí)現(xiàn)設(shè)計(jì)優(yōu)惠券系統(tǒng),文章基于Java與MySQL的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05