Java端使用Batik將SVG轉為PNG


在上篇中,我們需要將Highcharts生成的圖通過后台保存到pdf文件中,就需要對SVG進行轉換。
這里就介紹一下使用Batik處理SVG代碼的方法。
首先是jar包的獲取地址,https://xmlgraphics.apache.org/batik/,Apache旗下的,用起來也比較放心。

需要導入項目的jar包有4個

batik-all-1.11.jar
xml-apis-1.3.04.jar
xml-apis-ext-1.3.04.jar
xmlgraphics-commons-2.3.jar

具體的轉換方法如以下代碼,直接復制就可以使用

package jp.co.token.emobile.common;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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;

/**
 * 將svg轉換為png格式的圖片
 *
 */
public abstract 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();
                }
            }
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM