Java ImageMagick实现各种图片格式转JPG

一、Windows

下载ImageMagick:

中文官网:点击下载ImageMagickImageMagick 中文站

下载站:Index of /archive

下载站:ImageMagick – Download

注意: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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Go语言中,你可以使用第三方库来实现HEIC格式图片批量换为JPG格式。一个常用的库是github.com/dsoprea/go-heic-convert。以下是一个简单的示例代码: ```go package main import ( "fmt" "io/ioutil" "os" "path/filepath" "github.com/dsoprea/go-heic-convert/convert" ) func main() { // 输入HEIC图片所在文件夹路径 inputDir := "/path/to/input/directory" // 输出JPG图片所在文件夹路径 outputDir := "/path/to/output/directory" // 获取输入文件夹下的所有HEIC文件 files, err := ioutil.ReadDir(inputDir) if err != nil { fmt.Printf("无法读取输入文件夹:%v\n", err) os.Exit(1) } // 遍历每个HEIC文件并进行换 for _, file := range files { if !file.IsDir() && filepath.Ext(file.Name()) == ".heic" { inputFile := filepath.Join(inputDir, file.Name()) outputFile := filepath.Join(outputDir, file.Name()+".jpg") err = convert.ConvertHEICFile(inputFile, outputFile) if err != nil { fmt.Printf("换失败:%s\n", inputFile) } else { fmt.Printf("换成功:%s\n", inputFile) } } } } ``` 请确保在运行代码之前,你已经安装了`github.com/dsoprea/go-heic-convert`库。可以使用以下命令进行安装: ```shell go get -u github.com/dsoprea/go-heic-convert/... ``` 注意:该库依赖于ImageMagick和libheif库。在使用之前,请确保这两个库已经正确安装并配置好环境变量。 运行上述代码时,将输入文件夹路径和输出文件夹路径替换为实际的路径。代码将遍历输入文件夹中的所有HEIC文件,并将其换为JPG格式并保存在输出文件夹中。换成功的文件将打印在控制台上,而换失败的文件将显示相应的错误消息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值