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

Javax.swing.text、Lowagie、HTMLWriter添加图像(非来自文件),java,itext,htmlwriter,javax.swing.text,Java,Itext,Htmlwriter,Javax.swing.text,我正在尝试用Java中com.lowagie.text中的htmlWriter创建一个文档。 我所做的是创建一个图像(来自二维码),并尝试将其添加到文档中。 该文档连接到ByteArrayOutputStream,然后我将其写入ServletOutputStream 当我从bitmatrix创建图像时,什么都没有发生。 我想知道这是否是因为html需要一个图像URL。因此,如果我从url获取图像,它会显示。但是当我用java创建一个图像时,它不会在html中显示它?!? 有人能帮我吗 publ

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

当我从bitmatrix创建图像时,什么都没有发生。 我想知道这是否是因为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;
}
public void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//设置一些响应头
setHeader(“Expires”,Expires);
//设置内容类型
response.setContentType(内容类型);
ServletOutputStream out=null;
ByteArrayOutputStream baos=null;
试一试{
baos=getHtmlTicket();
//将ByteArrayOutputStream写入ServletOutputStream
out=response.getOutputStream();
资产负债表(注销);
}
捕获(例外e){
log.error(e.getMessage(),e);
response.setContentType(“text/html”);
//setHeader(“内容处置”,“文件名=\”“+文件名+”\”);
response.getWriter().write(“Det har oppstèè½tt en feil!

”; response.getWriter().write(“”+新日期().toString()+”

”); response.getWriter().write(“”); response.flushBuffer(); } public ByteArrayOutputStream getHtmlTicket(){ ByteArrayOutputStream bas=新的ByteArrayOutputStream(); 文档=新文档(); 字符串mycdetext=“YO YOU”; int size=128; 试一试{ getInstance(文档,baos); document.open(); 文件.添加(新段落(“Hello World”); 添加(新段落(新日期().toString()); Hashtable hintMap=新的Hashtable(); hintMap.put(EncodeHintType.ERROR\u CORRECTION,ErrorCorrectionLevel.L); QRCodeWriter QRCodeWriter=新的QRCodeWriter(); BitMatrix byteMatrix=qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_代码,大小,大小,hintMap); int pictureWidth=byteMatrix.getWidth(); BuffereImage双图像=新的BuffereImage(pictureWidth,pictureWidth, BuffereImage.TYPE_INT_RGB); bimage.createGraphics(); Graphics2D graphics=(Graphics2D)bimage.getGraphics(); 图形.设置颜色(颜色.白色); graphics.fillRect(0,0,pictureWidth,pictureWidth); 图形.设置颜色(颜色.黑色); 对于(int i=0;i
创建HtmlWriter是为了在开发过程中测试Itext库。这就是为什么图像只显示为正方形而没有内容。这也是为什么Itext的创建者在以后的版本中删除了HtmlWriter

如果希望响应以HTML格式显示图像(必须是BuffereImage),可以通过将图像转换为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'/>";
  }
专用字符串addImageToHTML(BuffereImage bf){
字符串base64String=“”;
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
试一试{
ImageIO.write(bf,“png”,baos);
base64String=DatatypeConverter.printBase64Binary(baos.toByteArray());
}
捕获(IOE异常){
e、 printStackTrace();
} 
返回“”;
}