How to return the image to the <xp: image> url of java without saving to disk

233 views Asked by At

little work on java, it was necessary to add a captcha to xpages. There is a code in java:

public static void getImage() {
                    String text = "Hello";
                    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g2d = img.createGraphics();
                    Font font = new Font("Arial", Font.PLAIN, 48);
                    g2d.setFont(font);
                    FontMetrics fm = g2d.getFontMetrics();
                    int width = fm.stringWidth(text);
                    int height = fm.getHeight();
                    g2d.dispose();

                    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                    g2d = img.createGraphics();
                    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
                    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
                    g2d.setFont(font);
                    fm = g2d.getFontMetrics();
                    g2d.setColor(Color.BLACK);
                    g2d.drawString(text, 0, fm.getAscent());
                    g2d.dispose();
                    try {
                        ImageIO.write(img, "png", new File("c:\\Text.png"));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

What should be done to the image inserted into the <xp:image>

1

There are 1 answers

4
Per Henrik Lausten On BEST ANSWER

You can create an XAgent that calls your getImage() method to return the URL to xp:image.

Here's an example of an XAgent for that:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xc="http://www.ibm.com/xsp/custom" rendered="false" viewState="nostate">

    <xp:this.beforeRenderResponse><![CDATA[#{javascript:
        com.company.getImage();
    }]]></xp:this.beforeRenderResponse>

</xp:view>

You need to modify your getImage() method to stream the image back. Here's an example (not tested):

public static void getImage() {
    try {
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        ServletOutputStream responseStream = response.getOutputStream();

        // Add your existing code to create your image here

        // Write the image to the responseStream
        ImageIO.write(img, "png", responseStream);

        FacesContext.getCurrentInstance().responseComplete();
        responseStream.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

You then use the XAgent in your xp:image like this (assuming it's called image.xsp):

<xp:image>
    <xp:this.url><![CDATA[#{javascript:"/image.xsp"}]]></xp:this.url>
</xp:image>