我有一个包含 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;
}
有什么帮助吗???