Java实现图片裁剪功能的示例详解

目录

  • 前言
  • Maven依赖
  • 代码
  • 验证一下

前言 本文提供将图片按照自定义尺寸进行裁剪的Java工具类,一如既往的实用主义。

Maven依赖
com.google.guavaguava30.1.1-jreorg.bytedecojavacv-platform1.5.5cn.hutoolhutool-all5.5.2


代码 不废话,上代码。
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坐标* @param startY 裁剪起始y坐标* @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 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)); }}

代码说明:
【Java实现图片裁剪功能的示例详解】1、cutOutImage方法参数分别为图片路径、输出临时目录、起始坐标x值、起始坐标y值、裁剪宽度、裁剪高度。
2、采用uuid作为临时输出唯一id,避免重复。
3、对文件后缀格式做了校验,可以按照需求自行调整。
4、裁剪尺寸不能超出图片限制,按照需求自行调整。

验证一下 准备的图片如下
Java实现图片裁剪功能的示例详解
文章图片

执行结果
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developersbuilt 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'libavutil56. 51.100 / 56. 51.100libavcodec58. 91.100 / 58. 91.100libavformat58. 45.100 / 58. 45.100libavdevice58. 10.100 / 58. 10.100libavfilter7. 85.100 /7. 85.100libswscale5.7.100 /5.7.100libswresample3.7.100 /3.7.100Input #0, image2, from 'C:\Users\yi\Desktop\2054011.jpg':Duration: 00:00:00.04, start: 0.000000, bitrate: 255438 kb/sStream #0:0: Video: mjpeg (Progressive), yuvj444p(pc, bt470bg/unknown/unknown), 3840x2160, 25 tbr, 25 tbn, 25 tbcStream mapping:Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))Press [q] to stop, [?] for helpOutput #0, image2, to 'C:\Users\yi\Desktop\\d1013fbee79e4380a01c574addf72afb.jpg':Metadata:encoder: Lavf58.45.100Stream #0:0: Video: mjpeg, yuvj444p(pc), 1920x2160, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbcMetadata:encoder: Lavc58.91.100 mjpegSide data:cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/Aframe=1 fps=0.0 q=10.4 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.201xvideo:234kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknownC:\Users\yi\Desktop\\d1013fbee79e4380a01c574addf72afb.jpgProcess finished with exit code 0

结果图如下
Java实现图片裁剪功能的示例详解
文章图片

以上就是Java实现图片裁剪功能的示例详解的详细内容,更多关于Java图片裁剪的资料请关注脚本之家其它相关文章!

    推荐阅读