• 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");
    }
    }
  • 相关阅读:
    改变字段的值
    创建新的对象
    根据方法的名称来执行方法
    获取类的字段
    获取构造器的信息
    找出类的方法
    开始使用Reflection
    反射简介
    leetcode501
    leetcode235
  • 原文地址:https://www.cnblogs.com/yangsanluo/p/16422320.html
Copyright © 2020-2023  润新知