java web中图片验证码功能实现

来源:互联网 发布:目前游戏的编程语言 编辑:程序博客网 时间:2024/06/06 23:51

用户在注册网站信息的时候基本上都要数据验证码验证。那么图片验证码功能该如何实现呢?

大概步骤是:

1.在内存中创建缓存图片

2.设置背景色

3.画边框

4.写字母

5.绘制干扰信息

6.图片输出

废话不多说,直接上代码

[java] view plain copy
  1. package com.lsgjzhuwei.servlet.response;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.IOException;  
  8. import java.util.Random;  
  9.   
  10. import javax.imageio.ImageIO;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.annotation.WebServlet;  
  13. import javax.servlet.http.HttpServlet;  
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16.   
  17. /** 
  18.  * Servlet implementation class VerificationCode 
  19.  */  
  20. @WebServlet(asyncSupported = true, urlPatterns = { "/VerificationCode" })  
  21. public class VerificationCode extends HttpServlet {  
  22.     private static final long serialVersionUID = 1L;  
  23.          
  24.     /** 
  25.      * @see HttpServlet#HttpServlet() 
  26.      */  
  27.     public VerificationCode() {  
  28.         super();  
  29.         // TODO Auto-generated constructor stub  
  30.     }  
  31.   
  32.     /** 
  33.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  34.      */  
  35.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  36.         // TODO Auto-generated method stub  
  37.           
  38.         int width = 120;  
  39.         int height = 30;  
  40.           
  41.         //创建一张内存中的缓存图片  
  42.         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  43.           
  44.         ///背景色  
  45.         //通过graphics绘制图像  
  46.         Graphics graphics = bufferedImage.getGraphics();  
  47.         //设置颜色  
  48.         graphics.setColor(Color.yellow);  
  49.         //填充  
  50.         graphics.fillRect(00, width, height);  
  51.           
  52.         ///画边框  
  53.         graphics.setColor(Color.blue);  
  54.         graphics.drawRect(00, width-1, height-1);  
  55.           
  56.         //写字母  
  57.         String content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";  
  58.         Random random = new Random();  
  59.         //设置字体颜色  
  60.         graphics.setColor(Color.red);  
  61.         //设置字体及大小  
  62.         graphics.setFont(new Font("宋体", Font.BOLD, 20));  
  63.         int x=20;  
  64.         int y=20;  
  65.         for(int i = 0; i < 4; i++)  
  66.         {  
  67.             int index = random.nextInt(content.length());  
  68.             char letter = content.charAt(index);  
  69.             graphics.drawString(letter+" ", x, y);  
  70.             x = x+20;  
  71.         }  
  72.           
  73.         //绘制干扰线  
  74.         int x1;  
  75.         int x2;  
  76.         int y1;  
  77.         int y2;  
  78.         graphics.setColor(Color.LIGHT_GRAY);  
  79.         for(int i = 0;i <50;i++)  
  80.         {  
  81.             x1=random.nextInt(width);  
  82.             x2=random.nextInt(width);  
  83.             y1=random.nextInt(height);  
  84.             y2=random.nextInt(height);  
  85.             graphics.drawLine(x1, y1, x2, y2);  
  86.         }  
  87.           
  88.         //将图片输出到浏览器  
  89.         //将内存的图片通过浏览器输出流输出成jpg图片  
  90.         ImageIO.write(bufferedImage, "jpg", response.getOutputStream());  
  91.           
  92.           
  93.     }  
  94.   
  95.     /** 
  96.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  97.      */  
  98.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  99.         // TODO Auto-generated method stub  
  100.     }  
  101.   
  102. }  
0 0
原创粉丝点击