我有一个带有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;
}
任何帮助???
答案 0 :(得分:1)
我怀疑问题可能出在 SRC属性中。确保 images / client-logo1.png 是图像的实际路径。如果它存储在本地,请记住使用前缀file:
。
例如,如果图像以 C:\ images \ client-logo1.png 的路径存储在Windows上,则img标记为:
<img src="file:C:\images\client-logo1.png" />