<a4j:mediaOutput> is not displying image
kosna Nov 21, 2012 10:26 PMHallo,
I am using gfv3.1.2.2, richfaces 3.3.3 final and jsf 1.2. I am trying to generate captcha image using mediaoutput component but i am not succeeded with that. it is not showing image at all and paint method is not being called. It is not throwing any error in server log.
jsp code:
<a4j:mediaOutput id="captcha" element="img" cacheable="false" session="false" createContent="#{mediaBean.paint}" mimeType="image/jpeg"
value="#{mediaBean.mediaData}" />
bean code is:
public void paint(OutputStream out, Object data) throws IOException {
MediaData paintData = (MediaData) data;
BufferedImage img = new BufferedImage(paintData.getImageWidth(), paintData.getImageHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
/* Font style for image */
Font font = new Font(Font.SANS_SERIF, Font.TRUETYPE_FONT, 32);
Random randChars = new Random();
String imageChar = "";
/* Possible random characters in the image */
imageChar = (Long.toString(Math.abs(randChars.nextLong()), 36)).substring(0,
paintData.getTotalCharacters());
int iCircle = 7;
//Prepare a rectangular shape
g2d.fillRect(0, 0, paintData.getImageWidth(),
paintData.getImageHeight());
//Logic to prepare circles inside rectangular image
for (int i = 0; i < iCircle; i++) {
g2d.setColor(new Color(randChars.nextInt(255),
randChars.nextInt(255),
randChars.nextInt(255)));
int imageRadius = (int) (Math.random() * paintData.getImageHeight()
/ 2.0);
int iX = (int) (Math.random() * paintData.getImageWidth()
- imageRadius);
int iY = (int) (Math.random() * paintData.getImageHeight()
- imageRadius);
g2d.fillRoundRect(iX, iY, imageRadius * 2, imageRadius * 2, 100, 100);
}
//Set the font to image
g2d.setFont(font);
//Disperse the characters inside the image
for (int i = 0; i < paintData.getTotalCharacters(); i++) {
g2d.setColor(new Color(randChars.nextInt(255),
randChars.nextInt(255),
randChars.nextInt(255)));
if (i % 2 == 0) {
g2d.drawString(imageChar.substring(i, i + 1), 25 * i, 24);
} else {
g2d.drawString(imageChar.substring(i, i + 1), 25 * i, 35);
}
}
//set the random character string to validate later.
setDisplayCode(imageChar);
ImageIO.write(img, "png", out);
}
Serialized bean class is
public class MediaData implements Serializable {
private static final long serialVersionUID = 1L;
Integer Width=110;
Integer Height=50;
//Corresponding setters and getters
}
did i do anything wrong here? please help me out!