一、Windows
下载ImageMagick:
中文官网:点击下载ImageMagickImageMagick 中文站
注意:windows下载时需要勾选
1. pom文件
<!-- 图片转换 -->
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
2. 转换代码
ConvertCmd cmd = new ConvertCmd();
// 设置imageMagick安装地址(linux系统下无需指定)
cmd.setSearchPath("D:\\Develop\\ImageMagick-7.1.1-Q16-HDRI");
IMOperation op = new IMOperation();
op.addImage("/usr/local/file/20230803.heic");
op.addImage("/usr/local/file/20230803.jpg");
try {
cmd.run(op);
} catch (IOException | InterruptedException | IM4JavaException e) {
e.printStackTrace();
}
二、Linux
我的java服务使用docker容器部署的,这里只记录docker安装部署步骤
1. 注释掉指定imageMagick安装目录的代码
ConvertCmd cmd = new ConvertCmd();
// 设置imageMagick安装地址(linux系统下无需指定)
//cmd.setSearchPath("D:\\Develop\\ImageMagick-7.1.1-Q16-HDRI");
IMOperation op = new IMOperation();
op.addImage("/usr/local/file/20230803.heic");
op.addImage("/usr/local/file/20230803.jpg");
try {
cmd.run(op);
} catch (IOException | InterruptedException | IM4JavaException e) {
e.printStackTrace();
}
1. DockerFile文件添加
#安装ImageMagick
RUN apt-get update -y && apt-get install imagemagick -y
注意:apt-get安装卡住的话需要切换一下镜像源,我这里用的是Debian11系统镜像
步骤:
1. 创建sources.list文件
1. vim sources.list
#更换镜像源用于Debian11下载资源
deb http://ftp.cn.debian.org/debian/ buster main contrib non-free
deb-src http://ftp.cn.debian.org/debian/ buster main contrib non-free
deb http://ftp.cn.debian.org/debian-security/ buster/updates main contrib non-free
deb-src http://ftp.cn.debian.org/debian-security/ buster/updates main contrib non-free
deb http://ftp.cn.debian.org/debian/ buster-updates main contrib non-free
deb-src http://ftp.cn.debian.org/debian/ buster-updates main contrib non-free
deb http://ftp.cn.debian.org/debian/ buster-backports main contrib non-free
deb-src http://ftp.cn.debian.org/debian/ buster-backports main contrib non-free
1. DockerFile
RUN chmod +777 /etc/apt/sources.list
COPY sources.list /etc/apt/
#安装ImageMagick
RUN apt-get update -y && apt-get install imagemagick -y