PS:原谅不知道怎么上传图片,看不到转换后的图片的效果。
svg 转 图片的开源类库:Apache batik-bin-1.10 版本
java代码如下:
public class Svg2ImageTest {
public static void main(String[] args) throws Exception {
TranscoderInput input = new TranscoderInput(Streams.fileIn("F:/svg.txt"));
FileOutputStream fos = new FileOutputStream("F:/demo.png");
TranscoderOutput output = new TranscoderOutput(fos);
Transcoder transcoder = new PNGTranscoder();
transcoder.transcode(input, output);
Streams.safeFlush(fos);
Streams.safeClose(fos);
}
}
svg代码如下:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="508px" height="54px" version="1.1" style="background-color: rgb(255, 255, 255);"><defs><linearGradient x1="0%" y1="0%" x2="0%" y2="100%" id="mx-gradient-3333ff-1-99ccff-1-s-0"><stop offset="0%" style="stop-color:#3333ff"></stop><stop offset="100%" style="stop-color:#99ccff"></stop></linearGradient></defs><g transform="translate(0.5,0.5)"><path d="M 34.99 25 L 68 25 L 93.63 25" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="none"></path><path d="M 98.88 25 L 91.88 28.5 L 93.63 25 L 91.88 21.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="none"></path><ellipse cx="18" cy="25" rx="17.5" ry="17.5" fill="#000000" stroke="#000000" transform="translate(2,3)" opacity="0.25"></ellipse><ellipse cx="18" cy="25" rx="17.5" ry="17.5" fill="#e6fff8" stroke="#4d9900" pointer-events="none"></ellipse><ellipse cx="18" cy="25" rx="13.5" ry="13.5" fill="none" stroke="#4d9900" pointer-events="none"></ellipse><path d="M 210 25 L 283.63 25" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="none"></path><path d="M 288.88 25 L 281.88 28.5 L 283.63 25 L 281.88 21.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="none"></path><rect x="100" y="0" width="110" height="50" rx="7.5" ry="7.5" fill="#000000" stroke="#000000" transform="translate(2,3)" opacity="0.25"></rect><rect x="100" y="0" width="110" height="50" rx="7.5" ry="7.5" fill="url(#mx-gradient-3333ff-1-99ccff-1-s-0)" stroke="#427bff" pointer-events="none"></rect><g transform="translate(142.5,18.5)"><switch><foreignObject style="overflow:visible;" pointer-events="all" width="24" height="12" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; font-size: 12px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; vertical-align: top; width: 25px; white-space: nowrap; word-wrap: normal; text-align: center;"><div xmlns="http://www.w3.org/1999/xhtml" style="display:inline-block;text-align:inherit;text-decoration:inherit;">申请</div></div></foreignObject><text x="12" y="12" fill="#000000" text-anchor="middle" font-size="12px" font-family="Helvetica">申请</text></switch></g><ellipse cx="488" cy="25" rx="13.5" ry="13.5" fill="#000000" stroke="#000000" transform="translate(2,3)" opacity="0.25"></ellipse><ellipse cx="488" cy="25" rx="13.5" ry="13.5" fill="#000000" stroke="#ff0000" pointer-events="none"></ellipse><ellipse cx="488" cy="25" rx="17.5" ry="17.5" fill="none" stroke="#ff0000" pointer-events="none"></ellipse><path d="M 400 25 L 435 25 L 463.64 25" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="none"></path><path d="M 468.89 25 L 461.89 28.5 L 463.64 25 L 461.89 21.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="none"></path><rect x="290" y="0" width="110" height="50" rx="7.5" ry="7.5" fill="#000000" stroke="#000000" transform="translate(2,3)" opacity="0.25"></rect><rect x="290" y="0" width="110" height="50" rx="7.5" ry="7.5" fill="url(#mx-gradient-3333ff-1-99ccff-1-s-0)" stroke="#427bff" pointer-events="none"></rect><g transform="translate(332.5,18.5)"><switch><foreignObject style="overflow:visible;" pointer-events="all" width="24" height="12" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; font-size: 12px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; vertical-align: top; width: 25px; white-space: nowrap; word-wrap: normal; text-align: center;"><div xmlns="http://www.w3.org/1999/xhtml" style="display:inline-block;text-align:inherit;text-decoration:inherit;">审批</div></div></foreignObject><text x="12" y="12" fill="#000000" text-anchor="middle" font-size="12px" font-family="Helvetica">审批</text></switch></g></g></svg>