juliocbq 5 de ago. de 2010
está difícil ver porque não dá para saber onde as variáveis estão sendo aplicadas somente com esse pedaço de código.
Essa cor está sendo usada onde?
Color c = new Color(0.6662f, 0.4569f, 0.3232f);
Pode usar o Graphics2D para pintar o bufferedImage com fillRect
ShOtCeL 5 de ago. de 2010
Julio,
o Color c = new Color(0.6662f, 0.4569f, 0.3232f); é usado para pintar as letras que aparecem no captcha.
Segue o código completo:
protected void doGet ( HttpServletRequest req
, HttpServletResponse response ) throws IOException , ServletException {
response . setHeader ( "Cache-Control" , "no-cache" );
response . setDateHeader ( "Expires" , 0 );
response . setHeader ( "Pragma" , "no-cache" );
response . setDateHeader ( "Max-Age" , 0 );
Random r = new Random ();
Hashtable < TextAttribute , Object > map = new Hashtable < TextAttribute , Object > ();
BufferedImage image = new BufferedImage ( width , height , BufferedImage . TYPE_INT_RGB );
Graphics2D graphics2D = image . createGraphics ();
String token = Long . toString ( Math . abs ( r . nextLong ()), 36 );
String ch = token . substring ( 0 , 6 );
Color c = new Color ( 0.6662f , 0.4569f , 0.3232f );
GradientPaint gp = new GradientPaint ( 30 , 30 , c , 15 , 25 , Color . white , true );
graphics2D . setPaint ( gp );
Font font = new Font ( "Verdana" , Font . CENTER_BASELINE , 26 );
graphics2D . setFont ( font );
graphics2D . drawString ( ch , 2 , 20 );
graphics2D . dispose ();
HttpSession session = req . getSession ( true );
session . setAttribute ( CAPTCHA_KEY , ch );
OutputStream outputStream = response . getOutputStream ();
ImageIO . write ( image , "jpeg" , outputStream );
outputStream . close ();
}
juliocbq 5 de ago. de 2010
protected void doGet(HttpServletRequest req
, HttpServletResponse response) throws IOException, ServletException {
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Max-Age", 0);
Random r = new Random();
Hashtable<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.Blue);
graphics2D.fillRect(0,0,image.getWidth(), image.getHeight());
String token = Long . toString ( Math . abs ( r . nextLong ()), 36 );
String ch = token . substring ( 0 , 6 );
Color c = new Color ( 0.6662f , 0.4569f , 0.3232f );
GradientPaint gp = new GradientPaint ( 30 , 30 , c , 15 , 25 , Color . white , true );
graphics2D . setPaint ( gp );
Font font = new Font ( "Verdana" , Font . CENTER_BASELINE , 26 );
graphics2D . setFont ( font );
graphics2D . drawString ( ch , 2 , 20 );
graphics2D . dispose ();
HttpSession session = req . getSession ( true );
session . setAttribute ( CAPTCHA_KEY , ch );
OutputStream outputStream = response . getOutputStream ();
ImageIO . write ( image , "jpeg" , outputStream );
outputStream . close ();
}
Presta atenção na sintaxe, porque posso ter digitado errado.
ShOtCeL 5 de ago. de 2010
Julio, muito obrigado!
Testei uma outra forma e funcionou, antes de criar o Graphics2D que gera o texto do Captcha, eu crei um outro só com a cor azul, veja:
protected void doGet ( HttpServletRequest req
, HttpServletResponse response ) throws IOException , ServletException {
response . setHeader ( "Cache-Control" , "no-cache" );
response . setDateHeader ( "Expires" , 0 );
response . setHeader ( "Pragma" , "no-cache" );
response . setDateHeader ( "Max-Age" , 0 );
Random r = new Random ();
Hashtable < TextAttribute , Object > map = new Hashtable < TextAttribute , Object > ();
BufferedImage image = new BufferedImage ( width , height , BufferedImage . TYPE_INT_RGB );
Graphics2D fundo = image . createGraphics ();
fundo . setColor ( Color . blue );
fundo . fillRect ( 0 , 0 , width , height );
fundo . dispose ();
Graphics2D graphics2D = image . createGraphics ();
String token = Long . toString ( Math . abs ( r . nextLong ()), 36 );
String ch = token . substring ( 0 , 6 );
Color c = new Color ( 0.6662f , 0.4569f , 0.3232f );
GradientPaint gp = new GradientPaint ( 30 , 30 , c , 15 , 25 , Color . white , true );
graphics2D . setPaint ( gp );
Font font = new Font ( "Verdana" , Font . CENTER_BASELINE , 26 );
graphics2D . setFont ( font );
graphics2D . drawString ( ch , 2 , 20 );
graphics2D . dispose ();
HttpSession session = req . getSession ( true );
session . setAttribute ( CAPTCHA_KEY , ch );
OutputStream outputStream = response . getOutputStream ();
ImageIO . write ( image , "jpeg" , outputStream );
outputStream . close ();
}
Isso funcionou!!!
Muito obrigado.
juliocbq 5 de ago. de 2010
ok, você pode simplificar usando somente uma superfície do Graphics2D para pintar, depois só precisa resetar a cor que será pintada novamente para o padrão anterior.
Graphics2D.setColor(Cor);
ShOtCeL 5 de ago. de 2010
Entendi … muito bom.
Mais uma vez, obrigado!