麟の随笔

  • 游戏
  • 影视
  • Tech
  1. 首页
  2. 科技
  3. Java
  4. 正文

Java 下SVG转PNG图标

2023年6月2日 1435点热度 0人点赞 0条评论

POM引入

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-all</artifactId>
    <version>1.13</version>
    <type>pom</type>
</dependency>

Java代码

 public static void svg2Png(String folderPath, String outPath) {
        try {
            //开始
            File folder = new File(folderPath);
            if (!folder.exists()) {
                return;
            }
            File outFolder = new File(outPath);
            if (!outFolder.exists()) {
                outFolder.mkdirs();
            }
            for (File file : folder.listFiles()) {
                if (!file.getName().endsWith(".svg")) {
                    continue;
                }
                File png = new File(outPath + File.separator + file.getName().replace(".svg", ".png"));
                OutputStream out = Files.newOutputStream(png.toPath());
                try {
                    out = new BufferedOutputStream(out);
                    Transcoder transcoder = new PNGTranscoder();
                    TranscoderInput input = new TranscoderInput(file.toURI().toString());
                    TranscoderOutput output = new TranscoderOutput(out);
                    transcoder.transcode(input, output);
                } catch (Exception e) {

                } finally {
                    out.close();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
标签: 暂无
最后更新:2023年6月2日

晓虎

必须有人浴血奋战,世上才有自由可言!

点赞
< 上一篇
下一篇 >

COPYRIGHT © 2024 kirinz.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

闽ICP备19013981号