Java web图片验证功能实现二

来源:互联网 发布:社交媒体网络安全问题 编辑:程序博客网 时间:2024/06/05 10:43

上一篇实现的是最简单的图片验证,本文要实现复杂一点的:图片是旋转的,并通过一个注册实例把图片验证嵌套在网页中

    实现图片旋转功能的代码为:

[java] view plain copy
  1. //写字母  
  2.         String content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";  
  3.         Random random = new Random();  
  4.         Graphics2D graphics2D = (Graphics2D)graphics;  
  5.         //设置字体颜色  
  6.         graphics2D.setColor(Color.red);  
  7.         //设置字体及大小  
  8.         graphics2D.setFont(new Font("宋体", Font.BOLD, 20));  
  9.         int x=20;  
  10.         int y=20;  
  11.         for(int i = 0; i < 4; i++)  
  12.         {  
  13.             int index = random.nextInt(content.length());  
  14.             char letter = content.charAt(index);  
  15.               
  16.             double jiaodu = random.nextInt(60)-30;  
  17.             double theta = jiaodu/360*2*Math.PI;  
  18.             graphics2D.rotate(theta, x, y);  
  19.             graphics2D.drawString(letter+" ", x, y);  
  20.             graphics2D.rotate(-theta, x, y);  
  21.               
  22.             x = x+20;         
  23.               
  24.         }  

实现带旋转图片验证功能的servlet代码为:

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

下面是新建HTML页面,并添加简单的注册按钮,源码如下:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>Insert title here</title>  
  6. </head>  
  7. <body>  
  8. <form>  
  9.     输入用户名<input type="text"/><br/>  
  10.     请输入密码<input type="password"/><br/>  
  11.     请输入验证码<input type="text" name ="checkcode">  
  12.     <img src="/day06/VerificationCode"/> <br/>  
  13.     <input type="submit" value="注册"/>  
  14. </form>  
  15. </body>  
  16. </html>  

系统运行界面如下:


下面实现复杂点的功能:单击图片验证码的时候验证码刷新

修改HTML页面代码如下:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <script type="text/javascript">  
  6. function change()  
  7. {  
  8.     //alert("1");  
  9.     //重新加载生成图片  
  10.     document.getElementById("myimg").src="/day06/VerificationCode?"+new Date().getTime();  
  11.     //alert(document.getElementById("myimg").src);  
  12. }  
  13. </script>  
  14. <title>Insert title here</title>  
  15. </head>  
  16. <body>  
  17. <form>  
  18.     输入用户名<input type="text"/><br/>  
  19.     请输入密码<input type="password"/><br/>  
  20.     请输入验证码<input type="text" name ="checkcode">  
  21.     <img src="/day06/VerificationCode" onclick="change()" id="myimg" style="cursor:pointer"/> <br/>  
  22.     <input type="submit" value="注册"/>  
  23. </form>  
  24. </body>  
  25. </html>  
0 0
原创粉丝点击