2

Java の com.lowagie.text で htmlWriter を使用してドキュメントを作成しようとしています。私がしていることは、(qr コードから) 画像を作成し、それをドキュメントに追加しようとすることです。ドキュメントは 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;
}
4

1 に答える 1