Java 在窗格中呈现html文本内的图像时出现问题
我有一个包含html内容的字符串,我正在将其设置为JeditorPane。该字符串包含一个图像源。我面临着很多问题 我需要将图像发送到打印机。一切看起来都很好,但标志总是一个坏的形象 这是html代码Java 在窗格中呈现html文本内的图像时出现问题,java,html,image,jeditorpane,htmleditorkit,Java,Html,Image,Jeditorpane,Htmleditorkit,我有一个包含html内容的字符串,我正在将其设置为JeditorPane。该字符串包含一个图像源。我面临着很多问题 我需要将图像发送到打印机。一切看起来都很好,但标志总是一个坏的形象 这是html代码 <td style="width:20%; height: auto" colspan="1"> <img src = "images/client-logo1.png" /> </td> 有什么帮助吗?我怀疑问题可能出在SRC属性中。确保images/cli
<td style="width:20%; height: auto" colspan="1">
<img src = "images/client-logo1.png" />
</td>
有什么帮助吗?我怀疑问题可能出在SRC属性中。确保images/client-logo1.png是图像的实际路径。如果它存储在本地,请记住使用前缀
文件:
例如,如果图像存储在Windows上的路径C:\images\client-logo1.png下,img标记将是:
窗格上显示的图像是否正确?只是打印部分出错了吗?它是一个大的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;
}