Captcha生成及使用

复制代码
@Controller
@RequestMapping("/PictureCheckCode")
public class ServletController {

    @RequestMapping
    public void pictureGenerate( HttpServletRequest req, HttpServletResponse res ) {
        try {
             MakePicture mp=new MakePicture() ;  
             String str=mp.drawPicture(60, 20,res.getOutputStream() );  
             req.getSession().setAttribute("pic", str);  
             res.getOutputStream().print(str);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
复制代码
<form th:action="@{/login}" method="post">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><label> Verify: <img src="PictureCheckCode"  onclick="this.src='PictureCheckCode?d='+new Date().getTime()"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package hello;
 
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO; 
import java.util.Random;
import java.io.OutputStream;
import java.io.IOException; 
public class MakePicture  //产生识别验证图像
{  
    private char charTable[]={
        'a','A','b','B','c','C','d','D' ,'e','E' ,
        'f','F','g','G','h','H','i','I','j','J' ,
        '0','1','2','3','4','5','6','7','8','9'
    };
    public String drawPicture(int width,int height,OutputStream os)
    {
        if(width<=0)
            width=100 ;
        if(height<=0)
            height=60 ;
             
        BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB) ;
        Graphics g=image.getGraphics()  ;
        g.setColor(Color.LIGHT_GRAY) ;
        g.fillRect(0, 0, width, height)  ;
        g.setColor(new Color(0x5265fd)) ;
        g.drawRect(0, 0, width, height)  ; 
        String str ="" ;
        for(int x=0;x<4;x++)
        {
          str+=charTable[(int) (Math.random()*charTable.length)];
        }
         
        g.drawString(str.substring(0, 1), 0, 15);
        g.drawString(str.substring(1, 2), 15, 17);
        g.drawString(str.substring(2, 3), 35, 19);
        g.drawString(str.substring(3, 4), 50, 16);
        Random rand=new Random() ;
        for(int i=0;i<10;i++)
        {
          int x=rand.nextInt(width)  ;
          int y=rand.nextInt(height)  ;
          g.drawOval(x, y, 1, 1) ;
        }
        g.dispose()  ;
        try {
            ImageIO.write(image, "JPEG",os) ;
        } catch (IOException e) {
             
            e.printStackTrace();
            return "" ;
        }
         
       return str ;
    }
}

  

posted @   samu  阅读(928)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
点击右上角即可分享
微信分享提示