将使用poi导出来的wmf公式文件转为让图片png、jpg,svg后缀文件,可直接执行代码

maven依赖:

<!-- wmf转svg转png -->
	<dependency>
	      <groupId>net.arnx</groupId>
	      <artifactId>wmf2svg</artifactId>
	      <version>0.9.5</version>
	</dependency>

<!--    svg转png-->
    <dependency>
      <groupId>org.apache.xmlgraphics</groupId>
      <artifactId>batik-codec</artifactId>
      <version>1.7</version>
    </dependency>

实现思路

先将wmf文件转为svg
再将svg文件转为图片png、jpg

public static void main(String[] args) throws Exception {
    String path = "D:\\b\\1\\out\\aa\\image8.wmf";
    // 将wmf转svg
    String svgFile = path.substring(0,
      path.lastIndexOf(".wmf"))
      + ".svg";
    wmfToSvg(path, svgFile);
    String s = readToString("D:\\b\\1\\out\\aa\\image8.svg");
    convertToPng(s,"D:\\b\\1\\out\\aa\\image8.png");
  }

/**
   * 将wmf转换为svg
   *
   * @param src
   * @param dest
   */
  public static void wmfToSvg(String src, String dest){
    InputStream in = null;
    OutputStream out = null;
    try {
      in = new FileInputStream(src);
      WmfParser parser = new WmfParser();
      final SvgGdi gdi = new SvgGdi(false);
      parser.parse(in, gdi);
      Document doc = gdi.getDocument();
      out = new FileOutputStream(dest);
      if (dest.endsWith(".svgz")) {
        out = new GZIPOutputStream(out);
      }
      output(doc, out);

    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      assert out != null;
      try {
        out.close();
        in.close();
      }catch (Exception e){
        e.printStackTrace();
      }

    }
  }

  /**
   * 输出信息
   *
   * @param doc
   * @param out
   * @throws Exception
   */
  private static void output(Document doc, OutputStream out) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
      "-//W3C//DTD SVG 1.0//EN");
    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
      "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
    transformer.transform(new DOMSource((Node) doc), new StreamResult(out));
    if (out != null) {
      out.flush();
      out.close();
    }
  }

// 读取svg
  public static String readToString(String fileName) {
    String encoding = "UTF-8";
    File file = new File(fileName);
    Long filelength = file.length();
    byte[] filecontent = new byte[filelength.intValue()];
    try {
      FileInputStream in = new FileInputStream(file);
      in.read(filecontent);
      in.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      return new String(filecontent, encoding);
    } catch (UnsupportedEncodingException e) {
      System.err.println("The OS does not support " + encoding);
      e.printStackTrace();
      return null;
    }
  }

//    svg转为图片
  public static void convertToPng(String svgCode, String pngFilePath) throws IOException, TranscoderException {

    File file = new File(pngFilePath);

    FileOutputStream outputStream = null;
    try {
      file.createNewFile();
      outputStream = new FileOutputStream(file);
      convertToPng(svgCode, outputStream);
    } finally {
      if (outputStream != null) {
        try {
          outputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  //将svgCode转换成png文件,直接输出到流中
  public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException, IOException {
    try {
      byte[] bytes = svgCode.getBytes(StandardCharsets.UTF_8);
      PNGTranscoder t = new PNGTranscoder();
      TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
      TranscoderOutput output = new TranscoderOutput(outputStream);
      t.transcode(input, output);
      outputStream.flush();
    } finally {
      if (outputStream != null) {
        try {
          outputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值