【问题标题】:issue with rendering image inside html text in Jeditorpane在 Jeditorpane 中的 html 文本中呈现图像的问题
【发布时间】:2012-11-03 00:22:53
【问题描述】:

我有一个包含 html 内容的字符串,我将它设置为 JeditorPane。该字符串包含一个图像源。我在渲染它时面临很多问题。

我需要将图像发送到打印机。一切看起来都不错,但徽标总是损坏的图像。

这是html代码

<td style="width:20%; height: auto" colspan="1">
<img src = "images/client-logo1.png" />
</td>

这就是我在将html读入字符串名称html后使用它的方式

    protected byte[] createImage(String html, String imageName) {
    final String methodName = "createImage";
    if (LOG.isTraceEnabled()) {
        LOG.trace("enter\n\t{}",  new Object[] {html, imageName});
    }
    StringReader reader = new StringReader("");
    JEditorPane pane = new JEditorPane();
    // pane.setEditable(false);
    pane.setEditorKit(new HTMLEditorKit());
    pane.setContentType("text/html");
    pane.setText(html);
    pane.setSize(IMAGE_WIDTH, IMAGE_HEIGHT);
    pane.setBackground(Color.white);

    // Create a BufferedImage
    BufferedImage image = new BufferedImage(pane.getWidth(), pane
            .getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();

    // Have the image painted by SwingUtilities
    JPanel container = new JPanel();
    SwingUtilities.paintComponent(g, pane, container, 0, 0, image
            .getWidth(), image.getHeight());
    g.dispose();
    byte[] imageInByte = null;
    try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "PNG", baos);
    baos.flush();
    imageInByte = baos.toByteArray();
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new CVProxyApplicationException(
                "Not able to create image due to: "
                        + e1.getLocalizedMessage());
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("exit\n\t{}");
    }
    /*
     * // If printer supports bytes, no need to create an image.
     * ByteArrayOutputStream os = new ByteArrayOutputStream();
     * image.flush(); try { ImageIO.write(image, "png", os); os.flush(); }
     * catch (IOException e1) { e1.printStackTrace(); } return
     * os.toByteArray();
     */
    return imageInByte;
}

任何帮助???

【问题讨论】:

  • 那么窗格上显示的图像是否正确?只是打印部分出了问题吗?
  • 这是一个很大的 html ......显示了很多文本......一切都打印得很好,但是用标签定义的图像 ......只有图像不渲染
  • 应该显示图像的容器显示损坏的图像...

标签: java html image jeditorpane htmleditorkit


【解决方案1】:

我怀疑问题可能出在 SRC 属性。确保 images/client-logo1.png 是图像的实际路径。如果是本地存储的,记得使用前缀file:。

例如,如果图像存储在 Windows 上的路径 C:\images\client-logo1.png 下,则 img 标签将为:

&lt;img src="file:C:\images\client-logo1.png" /&gt;

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 2012-07-03
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2022-12-15
    • 2010-10-28
    相关资源
    最近更新 更多