(RESOLVIDO) Alterar cor de fundo do BufferedImage

6 respostas
ShOtCeL

Boa tarde a todos,

Tenho um código onde ele gera uma imagem Captcha, o problema é que ele está gerando com o fundo preto, preciso alterar o fundo para azul, alguém sabe como posso fazer?

Segue o código:

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();

Obrigado!

6 Respostas

J

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

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();
	}
J
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

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.

J

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

Entendi … muito bom.

Mais uma vez, obrigado!

Criado 5 de agosto de 2010
Ultima resposta 5 de ago. de 2010
Respostas 6
Participantes 2