• Java端使用Batik将SVG转为PNG图片


    pom依赖:

    <!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-all -->
    <dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-all</artifactId>
    <version>1.10</version>
    <type>pom</type>
    </dependency>

    <dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-transcoder</artifactId>
    <version>1.14</version>
    </dependency>

    <dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-codec</artifactId>
    <version>1.14</version>
    </dependency>

    代码;


    import org.apache.batik.transcoder.TranscoderException;
    import org.apache.batik.transcoder.TranscoderInput;
    import org.apache.batik.transcoder.TranscoderOutput;
    import org.apache.batik.transcoder.image.ImageTranscoder;
    import org.apache.batik.transcoder.image.PNGTranscoder;

    import java.io.*;

    /**
    * @Author y
    * @Date
    * @Version 1.0
    */



    public class SVG2PNGUtils {


    /**
    * svg字符串转换为png
    *
    * @param svgCode
    * svg
    * @param pngFilePath
    * 保存的路
    * @throws TranscoderException
    * svg码异
    * @throws IOException
    * io错误
    */
    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文件,直接出到流中
    *
    * @param svgCode
    * svg
    * @param outputStream
    * 出流
    * @throws TranscoderException
    *
    * @throws IOException
    * io
    */
    public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException, IOException {
    try {
    byte[] bytes = svgCode.getBytes("utf-8");
    PNGTranscoder t = new PNGTranscoder();
    TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
    TranscoderOutput output = new TranscoderOutput(outputStream);
    // 增加片的(位是像素)---下面是死了,实际应该是根据SVG的大小动态设置,默认宽高都是400
    t.addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(400));
    t.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(300));
    t.transcode(input, output);
    outputStream.flush();
    } finally {
    if (outputStream != null) {
    try {
    outputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }


    public static void main(String[] args) throws IOException, TranscoderException {
    String svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n" +
    " <path d=\"M150 0 L75 200 L225 200 Z\" />\n" +
    "</svg>";
    convertToPng(svg,"src/main/resources/image/two.png");
    }
    }
  • 相关阅读:
    51-maven私有库神坑之:“Downloading: http://repo.maven.apache.org/maven2/”
    50-maven 仓库配置
    49-2017年第八届蓝桥杯国赛试题及详解(Java本科B组)
    21-matlab 迷宫题
    20-matlab全排列-函数调用
    52-python 画图二维
    51-python3 pandas读写excel
    48-设置tomcat虚拟路径的两种方法(Eclipse、tomcat、IDEA)
    19-matlab知识点复习二
    【剑指offer】二叉搜索树的后序遍历序列
  • 原文地址:https://www.cnblogs.com/yangsanluo/p/16422320.html
Copyright © 2020-2023  润新知