java后臺(tái)svg字符串轉(zhuǎn)成png

該篇文章只寫(xiě)了將SVG報(bào)文字符串轉(zhuǎn)PNG布隔,后續(xù)再更新SVG圖片轉(zhuǎn)PNG,如有問(wèn)題點(diǎn)澎迎,請(qǐng)大家留言庐杨,謝謝选调!

一夹供、引jar包

需要引入的jar包包含如下:(這里的jar包版本,大家可以根據(jù)自己的maven私服上的版本做對(duì)應(yīng)關(guān)系仁堪,可以是其他版本號(hào))


? <dependency>??????????

????????<groupId>org.apache.xmlgraphics</groupId>

????????<artifactId>batik-all</artifactId>

????????<version>1.7</version>

</dependency>

<dependency>

????????<groupId>xml-apis</groupId>

????????<artifactId>xml-apis-ext</artifactId>

????????<version>1.3.04</version>

????????</dependency>

<dependency>

<groupId>org.apache.xmlgraphics</groupId>

????????<artifactId>batik-codec</artifactId>

????????<version>1.7</version>

</dependency>


二哮洽、相關(guān)代碼


package com.mdp.backend.frame.impl.utils;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import org.apache.batik.transcoder.TranscoderInput;

import org.apache.batik.transcoder.TranscoderOutput;

import org.apache.batik.transcoder.image.PNGTranscoder;

/**

* 流程圖片操作

* @author lixiaolong

*

*/

public class ProcessPictureUtils {

/**

* 將svg字符串轉(zhuǎn)換為png

? ? *

? ? * @param svgCode svg代碼

? ? * @param pngFilePath 保存的路徑

? ? * @throws TranscoderException svg代碼異常

? ? * @throws IOException io錯(cuò)誤

? ? */

? ? public static String convertToPng(String svgCode, String pngFilePath){

? ? String errorMessage = "";

? ? ? ? File file = new File(pngFilePath);

? ? ? ? FileOutputStream outputStream = null;

? ? ? ? try {

? ? ? ? ? ? file.createNewFile();

? ? ? ? ? ? outputStream = new FileOutputStream(file);

? ? ? ? ? ? convertToPng(svgCode, outputStream);

? ? ? ? }catch(Exception e) {

? ? ? ? e.printStackTrace();

? ? ? ? errorMessage = "將svg字符串轉(zhuǎn)換為png出錯(cuò)!";

? ? ? ? Log4jUtils.info(String.format("將svg字符串轉(zhuǎn)換為png出錯(cuò)O夷簟: Excepion:=%s",e));

? ? ? ? }finally {

? ? ? ? ? ? if (outputStream != null) {

? ? ? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ? ? outputStream.close();

? ? ? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return errorMessage;

? ? }

? ? /**

? ? * 將svgCode轉(zhuǎn)換成png文件鸟辅,直接輸出到流中

? ? *

? ? * @param svgCode svg代碼

? ? * @param outputStream 輸出流

? ? * @throws TranscoderException 異常

? ? * @throws IOException io異常

? ? */

? ? public static void convertToPng(String svgCode, OutputStream outputStream){

? ? try {

byte[] bytes = svgCode.getBytes("utf-8");

PNGTranscoder t = new PNGTranscoder();

TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));

TranscoderOutput output = new TranscoderOutput(outputStream);

t.transcode(input, output);

outputStream.flush();

} catch (Exception e) {

e.printStackTrace();

Log4jUtils.info(String.format("將svg轉(zhuǎn)換成png文件,直接輸出到流中出錯(cuò)]汉: Excepion:=%s",e));

}

? ? }

}


三匪凉、測(cè)試報(bào)文字符串樣例

"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- created with bpmn-js / http://bpmn.io -->\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"288\" height=\"92\" viewBox=\"216 114 288 92\" version=\"1.1\"><defs><marker id=\"sequenceflow-end-white-black-369213csyyrintp0747bwivx0\" viewBox=\"0 0 20 20\" refX=\"11\" refY=\"10\" markerWidth=\"10\" markerHeight=\"10\" orient=\"auto\"><path d=\"M 1 5 L 11 10 L 1 15 Z\" style=\"fill: black; stroke-width: 1px; stroke-linecap: round; stroke-dasharray: 10000, 1; stroke: black;\"/></marker></defs><g class=\"djs-group\"><g class=\"djs-element djs-connection\" data-element-id=\"SequenceFlow_1tmmq3b\" style=\"display: block;\"><g class=\"djs-visual\"><path d=\"m 390,160L462,160 \" style=\"fill: none; stroke-width: 2px; stroke: black; stroke-linejoin: round; marker-end: url('#sequenceflow-end-white-black-369213csyyrintp0747bwivx0');\"/></g><polyline points=\"390,160 462,160 \" class=\"djs-hit\" style=\"fill: none; stroke-opacity: 0; stroke: white; stroke-width: 15px;\"/><rect x=\"384\" y=\"154\" width=\"84\" height=\"12\" class=\"djs-outline\" style=\"fill: none;\"/></g></g><g class=\"djs-group\"><g class=\"djs-element djs-connection djs-dragging\" data-element-id=\"SequenceFlow_1ky1ex5\" style=\"display: block;\"><g class=\"djs-visual\"><path d=\"m 258,160L290,160 \" style=\"fill: none; stroke-width: 2px; stroke: black; stroke-linejoin: round; marker-end: url('#sequenceflow-end-white-black-369213csyyrintp0747bwivx0');\"/></g><polyline points=\"258,160 290,160 \" class=\"djs-hit\" style=\"fill: none; stroke-opacity: 0; stroke: white; stroke-width: 15px;\"/><rect x=\"252\" y=\"154\" width=\"44\" height=\"12\" class=\"djs-outline\" style=\"fill: none;\"/></g></g><g class=\"djs-group\"><g class=\"djs-element djs-shape\" data-element-id=\"Task_0zg07r9\" style=\"display: block;\" transform=\"matrix(1 0 0 1 290 120)\"><g class=\"djs-visual\"><rect x=\"0\" y=\"0\" width=\"100\" height=\"80\" rx=\"10\" ry=\"10\" style=\"stroke: black; stroke-width: 2px; fill: white; fill-opacity: 0.95;\"/><text lineHeight=\"1.2\" class=\"djs-label\" style=\"font-family: Arial, sans-serif; font-size: 12px; font-weight: normal; fill: black;\"><tspan x=\"46.6640625\" y=\"43.599999999999994\">1</tspan></text></g><rect x=\"0\" y=\"0\" width=\"100\" height=\"80\" class=\"djs-hit\" style=\"fill: none; stroke-opacity: 0; stroke: white; stroke-width: 15px;\"/><rect x=\"-6\" y=\"-6\" width=\"112\" height=\"92\" class=\"djs-outline\" style=\"fill: none;\"/></g></g><g class=\"djs-group\"><g class=\"djs-element djs-shape\" data-element-id=\"EndEvent_0v2lvki\" style=\"display: block;\" transform=\"matrix(1 0 0 1 462 142)\"><g class=\"djs-visual\"><circle cx=\"18\" cy=\"18\" r=\"18\" style=\"stroke: black; stroke-width: 4px; fill: white; fill-opacity: 0.95;\"/></g><rect x=\"0\" y=\"0\" width=\"36\" height=\"36\" class=\"djs-hit\" style=\"fill: none; stroke-opacity: 0; stroke: white; stroke-width: 15px;\"/><rect x=\"-6\" y=\"-6\" width=\"48\" height=\"48\" class=\"djs-outline\" style=\"fill: none;\"/></g></g><g class=\"djs-group\"><g class=\"djs-element djs-shape djs-dragging\" data-element-id=\"StartEvent_01dnj5i\" style=\"display: block;\" transform=\"matrix(1 0 0 1 222 142)\"><g class=\"djs-visual\"><circle cx=\"18\" cy=\"18\" r=\"18\" style=\"stroke: black; stroke-width: 2px; fill: white; fill-opacity: 0.95;\"/></g><rect x=\"0\" y=\"0\" width=\"36\" height=\"36\" class=\"djs-hit\" style=\"fill: none; stroke-opacity: 0; stroke: white; stroke-width: 15px;\"/><rect x=\"-6\" y=\"-6\" width=\"48\" height=\"48\" class=\"djs-outline\" style=\"fill: none;\"/></g></g><g class=\"djs-drag-group\" style=\"pointer-events: none;\" transform=\"matrix(1 0 0 1 20 0)\"><g class=\"djs-dragger\" data-element-id=\"StartEvent_01dnj5i\" style=\"display: block;\" transform=\"matrix(1 0 0 1 202 142)\" x=\"303\" y=\"409\"><g class=\"djs-visual\"><circle cx=\"18\" cy=\"18\" r=\"18\" style=\"stroke: black; stroke-width: 2px; fill: white; fill-opacity: 0.95;\"/></g><rect x=\"0\" y=\"0\" width=\"36\" height=\"36\" class=\"djs-hit\" style=\"fill: none; stroke-opacity: 0; stroke: white; stroke-width: 15px;\"/><rect x=\"-6\" y=\"-6\" width=\"48\" height=\"48\" class=\"djs-outline\" style=\"fill: none;\"/></g></g></svg>"

四、效果圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末捺檬,一起剝皮案震驚了整個(gè)濱河市再层,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖聂受,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蒿秦,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蛋济,警方通過(guò)查閱死者的電腦和手機(jī)棍鳖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)碗旅,“玉大人渡处,你說(shuō)我怎么就攤上這事】秆浚” “怎么了骂蓖?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)川尖。 經(jīng)常有香客問(wèn)我登下,道長(zhǎng),這世上最難降的妖魔是什么叮喳? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任被芳,我火速辦了婚禮,結(jié)果婚禮上馍悟,老公的妹妹穿的比我還像新娘畔濒。我一直安慰自己,他們只是感情好锣咒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布侵状。 她就那樣靜靜地躺著,像睡著了一般毅整。 火紅的嫁衣襯著肌膚如雪趣兄。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,679評(píng)論 1 305
  • 那天悼嫉,我揣著相機(jī)與錄音艇潭,去河邊找鬼。 笑死戏蔑,一個(gè)胖子當(dāng)著我的面吹牛蹋凝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播总棵,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼鳍寂,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了情龄?” 一聲冷哼從身側(cè)響起迄汛,我...
    開(kāi)封第一講書(shū)人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤候味,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后隔心,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體白群,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年硬霍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了帜慢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡唯卖,死狀恐怖粱玲,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情拜轨,我是刑警寧澤抽减,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站橄碾,受9級(jí)特大地震影響卵沉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜法牲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一史汗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拒垃,春花似錦停撞、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至横堡,卻和暖如春埋市,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背翅萤。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工恐疲, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留腊满,地道東北人套么。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像碳蛋,于是被迫代替她去往敵國(guó)和親胚泌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容