前言
本文提供将图片按照自定义尺寸进行裁剪的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坐标
* @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<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方法参数分别为图片路径、输出临时目录、起始坐标x值、起始坐标y值、裁剪宽度、裁剪高度。
2、采用uuid作为临时输出唯一id,避免重复。
3、对文件后缀格式做了校验,可以按照需求自行调整。
4、裁剪尺寸不能超出图片限制,按照需求自行调整。
验证一下
准备的图片如下
执行结果
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
结果图如下
OK,没什么问题。
总结
没啥好总结的,还是提醒一下,需要注意图片的尺寸,裁剪的时候不能超出尺寸范围。
分享:
我们赤着脚在自己的生命里行走,沿途无数的荆棘在等着你向前冲,它们在你的身上划出一道道伤口,让你痛,让你懂,教你怎样继续走。——《夏至未至》
如果本文对你有帮助的话,点个赞吧,谢谢!
本人CSDN主页地址:剑客阿良_ALiang的主页
一起学习,一起进步。