简体   繁体   English

在Jeditorpane的html文本内渲染图像的问题

[英]issue with rendering image inside html text in Jeditorpane

I have a string with html contents and i am setting it to JeditorPane. 我有一个包含html内容的字符串,并将其设置为JeditorPane。 The string contains an image source. 该字符串包含图像源。 I am facing a lot of issues rendering it. 我在渲染它时遇到很多问题。

I need to send the image to a printer. 我需要将图像发送到打印机。 Everything looks good but the logo which is always a broken image. 一切看起来不错,但徽标始终是残破的图像。

this is the html code 这是HTML代码

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

and this is how i am utilizing it after reading the html into a string name html 这就是我将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;
}

any help??? 任何帮助???

I suspect the problem might be in the SRC attribute . 我怀疑问题可能出在SRC属性中 Make sure images/client-logo1.png is the actual path of the image. 确保images / client-logo1.png是图像的实际路径。 If it's stored locally, remember to use the prefix file: . 如果存储在本地,请记住使用前缀file:

For example, if the image is stored on Windows under the path C:\\images\\client-logo1.png , the img tag would be: 例如,如果图像存储在Windows下的C:\\ images \\ client-logo1.png路径下,则img标签为:

<img src="file:C:\\images\\client-logo1.png" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM