Java实现emf转jpg png 图片转换


前言

本文旨在帮助需要处理emf转png, jpg, 目前网上能搜到emf转jpg的文章, 博客基本上都是12年写的, 用的是freehep, 但是用freehep有个问题就是转出来的图片出现文字丢失, 或者颜色异常, 因此freehep无法满足满足我的需求.

本文主要使用GraphicsMagick + im4java 来实现emf转jpg, png.


一、GraphicsMagick的安装

windows安装包: https://www.aliyundrive.com/s/wjj94qCpVQq

GM官网下载地址: https://sourceforge.net/projects/graphicsmagick/files/

linux 安装请自行百度
windows安装方式: 一直next即可

验证GM是否能够转换自己想要的格式可以通过安装后通过命令控制台输入命令查看
gm convert 源文件名称 目标文件名称
例如: gm convert 1.emf 1.jpg

除了图片转换 还可以实现图片的压缩、裁剪、文字水印, 这些并不在本文本次的讨论范围, 有兴趣的小伙伴可以自行研究

二、结合im4java使用实现图片转换

1.引入maven依赖

       <dependency>
            <groupId>org.im4java</groupId>
            <artifactId>im4java</artifactId>
            <version>1.4.0</version>
        </dependency>

2.图片转换

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.im4java.core.ImageCommand;
import org.im4java.process.Pipe;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ClassName GraphicsMagicUtils
 * @Description emf转jpg/png工具类
 **/
public class GraphicsMagicUtils {

    // GraphicsMagic的安装路径
    private static final String INSTALL_PATH = "C:\\Program Files\\GraphicsMagick-1.3.36-Q16";

    /**
     * 图片格式转换
     *
     * @param src               源文件路径
     * @param toFormatImageType 转换后的图片格式
     * @param target            目标文件路径
     */
    public static void convertFormat(String src, String toFormatImageType, String target) {
        FileInputStream sourceInputStream = null;
        FileOutputStream targetOut = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            // 读取图片
            sourceInputStream = new FileInputStream(src);
            // 图片输出流
            targetOut = new FileOutputStream(target);
            // 获取图片操作对象
            IMOperation imOperation = buildIMOperation(toFormatImageType);
            // 将图片转成流
            byteArrayOutputStream = new ByteArrayOutputStream();
            Pipe pipeIn = new Pipe(sourceInputStream, null);
            Pipe pipeOut = new Pipe(null, byteArrayOutputStream);
            // 必须为true. 默认false
            ImageCommand convertCmd = new ConvertCmd(true);
            // 设置GM程序路径
            convertCmd.setSearchPath(INSTALL_PATH);
            convertCmd.setInputProvider(pipeIn);
            convertCmd.setOutputConsumer(pipeOut);
            // 图片转换处理
            convertCmd.run(imOperation);
            // 写出图片
            targetOut.write(byteArrayOutputStream.toByteArray());
        } catch (Exception e) {
            System.out.println("图片格式转换失败");
        } finally {
            try {
                if (targetOut != null) {
                    targetOut.flush();
                    targetOut.close();
                }
                if (byteArrayOutputStream != null) {
                    byteArrayOutputStream.flush();
                    byteArrayOutputStream.close();
                }
                if (sourceInputStream != null) {
                    sourceInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建 IMOperation
     *
     * @param imageType 图片类型,示例:jpg、png等
     * @return
     */
    private static IMOperation buildIMOperation(String imageType) {
        IMOperation imOperation = new IMOperation();
        // 表示图片为输入流
        imOperation.addImage("-");
        // 设置宽度像素最大值为1000, 高度像素最大值为1000, 优先满足宽,按比例缩放
        imOperation.resize(1000, 1000);
        // '^'表示宽高像素都必须大于等于值
//        imOperation.resize(1000, 1000, '^');
        // 设置输出图片的格式, 例如  jpg:-
        imOperation.addImage(imageType + ":-");

        return imOperation;
    }

}

3. 使用注意事项

  1. 需要修改工具类中的INSTALL_PATH的值改为自己GM安装的根目录
  2. 如果需要修改图片的像素大小, 在buildIMOperation方法里面进行操作

4.测试代码

public static void main(String[] args) throws Exception {
		// 参数一: 源图片路径
		// 参数二: 要转换的目标格式
		// 参数化三: 存在生成的图片路径 需指定文件名.拓展名
        GraphicsMagicUtils.convertFormat(SRC, "png", TARGET);
    }

参考链接

  1. https://blog.csdn.net/Sunshine_zjh/article/details/112988095
  2. https://www.cnblogs.com/xieegai/p/8438918.html
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值