gpt4 book ai didi

Javax.swing.text、Lowagie、HTMLWriter 添加图像(不是来自文件)

转载 作者:行者123 更新时间:2023-11-30 11:10:09 26 4
gpt4 key购买 nike

我正在尝试在 Java 的 com.lowagie.text 中使用 htmlWriter 创建一个文档。我所做的是创建一个图像(来自二维码)并尝试将其添加到文档中。文档连接到 ByteArrayOutputStream,然后我将其写出到 ServletOutputStream。

当我从位矩阵创建图像时,没有任何反应。我想知道这是否是因为 html 需要图像 URL。因此,如果我从 url 获取图像,它就会显示。但是当我只是在 java 中创建一个图像时,它不会在 html 中显示这个?!?谁能帮帮我?

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// setting some response headers
response.setHeader("Expires", EXPIRES);

// setting the content type
response.setContentType(CONTENT_TYPE);

ServletOutputStream out = null;
ByteArrayOutputStream baos = null;
try {
baos = getHtmlTicket();

// write ByteArrayOutputStream to the ServletOutputStream
out = response.getOutputStream();
baos.writeTo(out);
}
catch (Exception e) {

log.error(e.getMessage(), e);
response.setContentType("text/html");
// response.setHeader("Content-Disposition", "filename=\"" + filename + "\"");
response.getWriter().write("<p>Det har oppst�tt en feil!</p>");
response.getWriter().write("<p>" + new Date().toString() + "</p>");
response.getWriter().write("<!-- " + e.getMessage() + " -->");
response.flushBuffer();
}


public ByteArrayOutputStream getHtmlTicket() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

Document document = new Document();

String myCodeText = "YO YOU";
int size = 128;
try {
HtmlWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("Hello World"));
document.add(new Paragraph(new Date().toString()));

Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
int pictureWidth = byteMatrix.getWidth();
BufferedImage bimage = new BufferedImage(pictureWidth, pictureWidth,
BufferedImage.TYPE_INT_RGB);
bimage.createGraphics();

Graphics2D graphics = (Graphics2D) bimage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, pictureWidth, pictureWidth);
graphics.setColor(Color.BLACK);

for (int i = 0; i < pictureWidth; i++) {
for (int j = 0; j < pictureWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}

com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(bimage , null);
document.add(image);


}
catch (DocumentException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

document.close();

return baos;
}

最佳答案

创建 HtmlWriter 是为了在开发过程中测试 Itext 库。这就是为什么图像只显示为没有内容的正方形的原因。这也是 Itext 的创建者在以后的版本中删除 htmlWriter 的原因。

如果您希望响应以 HTML 格式显示图像(必须是 bufferedImage),您可以像这样将图像转换为 Base64:

 private String addImageToHTML(BufferedImage bf) {
String base64String = "";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bf, "png", baos);
base64String = DatatypeConverter.printBase64Binary(baos.toByteArray());
}
catch (IOException e) {
e.printStackTrace();
}
return "<img style='max-width:100%' src='data:image/png;base64,"+ base64String + "' alt='IMG DESC'/>";
}

关于Javax.swing.text、Lowagie、HTMLWriter 添加图像(不是来自文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27821223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com