Java:将html转换为图像 - 质量差

时间:2017-02-24 09:49:55

标签: java html image

我按照此示例将html转换为图片

https://devcrawler.wordpress.com/2013/05/07/rendering-html-to-image-on-java-code-server-side/

但是生成的图像质量很差。我错过了什么吗?这是我的代码:

public static void main(String[] args) throws Exception {
    String htmlString = ...;

    Java2DRenderer renderer = new Java2DRenderer(createDocumentFromXMLString(htmlString), 600, -1);
    renderer.getSharedContext().setDPI(2048);

    BufferedImage img = renderer.getImage();

    FSImageWriter imagWriter = new FSImageWriter();
    imagWriter.write(img, "path");

}

public static Document createDocumentFromXMLString(String xml) throws Exception {
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    fac.setNamespaceAware(false);
    fac.setValidating(false);
    fac.setIgnoringComments(false);
    fac.setIgnoringElementContentWhitespace(false);
    fac.setExpandEntityReferences(false);
    fac.setFeature("http://xml.org/sax/features/namespaces", false);
    fac.setFeature("http://xml.org/sax/features/validation", false);
    fac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    fac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    DocumentBuilder builder = fac.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

我该怎样做才能提高质量?任何帮助表示赞赏。

感谢。

0 个答案:

没有答案