NutzCN Logo
问答 用 Apache batik 1.10 把svg代码转成png图片,文字丢失???
发布于 142天前 作者 wqmain 306 次浏览 复制 上一个帖子 下一个帖子
标签:

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>
4 回复

不支持所用的字体呢?

来自炫酷的 NutzCN

谢兽总,但我看batik默认的字体就包括Helvetica呀?见如下源码:

/**
 * This class may be the base class of all transcoders which take an
 * SVG document as input and which need to build a DOM tree. The
 * <code>SVGAbstractTranscoder</code> uses several different hints that
 * guide it's behaviour:<br>
 *
 * <ul>
 *   <li><code>KEY_WIDTH, KEY_HEIGHT</code> can be used to specify how to scale the
 *       SVG image</li>
 * </ul>
 *
 * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
 * @version $Id: SVGAbstractTranscoder.java 1805902 2017-08-23 14:54:08Z ssteiner $
 */
public abstract class SVGAbstractTranscoder extends XMLAbstractTranscoder {
    /**
     * Value used as a default for the default font-family hint
     */
    public static final String DEFAULT_DEFAULT_FONT_FAMILY
        = "Arial, Helvetica, sans-serif";

    /**
     * Current area of interest.
     */
    protected Rectangle2D curAOI;

如果真是不支持这个字体,那我该怎么做呢? 通过 transcoder.addTranscodingHint 默认的字体是微软雅黑等字体也都不行呀。。。

或者兽总有没有别的svg转png的开源项目推荐呢?谢过先~

得试试了,之前没用过

来自炫酷的 NutzCN

添加回复
请先登陆
回到顶部