简单的图像验证码

@WebServlet("/captche")
public class CaptcheController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletOutputStream sos= resp.getOutputStream();//输出二进制信息
/*        resp.getWriter();输出文本信息*/
        ImageIO.write(generatePic(100,80),"JPEG",sos);
        sos.flush();
        sos.close();
    }
    private BufferedImage generatePic(int width,int height){
        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
        Graphics g= image.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0,0,width,height);
        g.setColor(Color.black);
        //设置字体大小
        g.setFont(new Font("宋体",Font.BOLD,26));
        //设置验证码的内容,因为我也不是很会,所以就用最低级的静态验证码了
        g.drawString("1234",50,20);
        Random rnd=new Random();//产生随机数
        rnd.nextInt(100);//产生随机数

        Graphics2D g2 = image.createGraphics();
        Random random = new Random();
        g2.setColor(Color.blue);// 设置线条的颜色
       //开始增加干扰项,连续做了三个不一样的颜色
        for (int i = 0; i < 20; i++) {
            int x = random.nextInt(width- 1);
            int y = random.nextInt(height- 1);
            int xl = random.nextInt(6) + 1;
            int yl = random.nextInt(12) + 1;
            g2.drawLine(x, y, x + xl + 40, y + yl + 20);
        }
        g2.setColor(Color.cyan);
        for (int i = 0; i < 20; i++) {
            int x = random.nextInt(width- 3);
            int y = random.nextInt(height- 3);
            int xl = random.nextInt(4) + 2;
            int yl = random.nextInt(10) + 2;
            g2.drawLine(x, y, x + xl + 30, y + yl + 10);
        }
        g2.setColor(Color.red);
        for (int i = 0; i < 20; i++) {
            int x = random.nextInt(width- 6);
            int y = random.nextInt(height- 6);
            int xl = random.nextInt(1) + 1;
            int yl = random.nextInt(2) + 1;
            g2.drawLine(x, y, x + xl + 50, y + yl + 40);
        }
        g.setFont(new Font("宋体",Font.BOLD,26));//设置字体
        g.dispose();
        return image;
    }
}

成品就是这样的了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于PyTorch的图像验证码识别模型及测试代码的示例: ```python import torch import torchvision.transforms as transforms from PIL import Image # 定义模型结构 class CaptchaModel(torch.nn.Module): def __init__(self): super(CaptchaModel, self).__init__() self.conv1 = torch.nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1) self.pool1 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.conv2 = torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1) self.pool2 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.conv3 = torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1) self.pool3 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.fc1 = torch.nn.Linear(128*4*13, 1024) self.fc2 = torch.nn.Linear(1024, 4*10) def forward(self, x): x = self.conv1(x) x = torch.nn.functional.relu(x) x = self.pool1(x) x = self.conv2(x) x = torch.nn.functional.relu(x) x = self.pool2(x) x = self.conv3(x) x = torch.nn.functional.relu(x) x = self.pool3(x) x = x.view(-1, 128*4*13) x = self.fc1(x) x = torch.nn.functional.relu(x) x = self.fc2(x) return x # 加载模型 model = CaptchaModel() model.load_state_dict(torch.load('captcha_model.pth')) # 定义验证码字符集 charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' # 定义预处理函数 preprocess = transforms.Compose([ transforms.Resize((50, 200)), transforms.ToTensor(), ]) # 定义测试函数 def test_captcha(image_path): # 加载图像 image = Image.open(image_path).convert('RGB') # 预处理图像 image = preprocess(image) # 扩展维度 image = image.unsqueeze(0) # 预测结果 output = model(image) # 获取预测结果的索引 output = torch.argmax(output, dim=1) # 将索引转换成字符 captcha = ''.join([charset[i] for i in output]) return captcha # 测试 captcha = test_captcha('captcha.png') print(captcha) ``` 需要注意的是,在运行代码之前,需要先准备好验证码图像,并将其保存为`captcha.png`文件。如果需要识别多张验证码图像,只需要在测试函数中添加一个循环即可。 此外,需要提醒的是,这只是一个简单验证码识别模型,针对不同的验证码类型,可能需要进行一些调整,比如修改模型结构、调整字符集等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值