百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

JAVA 验证码生成——SimpleCaptcha

bigegpt 2024-08-10 12:16 8 浏览

去官方网站下载Jar包:

http://simplecaptcha.sourceforge.net/

Javadocs:

http://simplecaptcha.sourceforge.net/javadocs/index.html

自己书写工具类:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.sino.gxh.util;

import java.awt.Color;

import java.awt.Font;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import nl.captcha.Captcha;

import nl.captcha.gimpy.FishEyeGimpyRenderer;

import nl.captcha.gimpy.RippleGimpyRenderer;

import nl.captcha.noise.CurvedLineNoiseProducer;

import nl.captcha.servlet.CaptchaServletUtil;

import nl.captcha.text.producer.DefaultTextProducer;

import nl.captcha.text.renderer.ColoredEdgesWordRenderer;

import nl.captcha.text.renderer.WordRenderer;

/**

*

* @author Administrator

*/

public class CodeMaker {

//验证码内容

private char[] numberChar = new char[]{'a', 'b', 'c', 'd',

'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

//验证码数量

private int _CodeCount = 4;

//验证码宽度

private int _width = 110;

//验证码高度

private int _height = 50;

//验证码颜色

private Color _CodeColor = Color.BLACK;

//使用字体名字

private String _FontName = "System";

//使用字体类型

private int _FontType = Font.BOLD;

//使用字体大小

private int _FontSize = 40;

//干扰线颜色

private Color _NoiseColor = Color.BLACK;

//干扰线大小

private int _NoiseSize = 2;

//干扰线条数

private int _NoiseCount = 1;

//验证图形码时是否开启大小写

private boolean whetherOpenBigOrSmall = false;

//验证码储存

private String CodeMemory;

//获取验证码

public void getCode(HttpServletResponse resp) {

Captcha.Builder captcha = new Captcha.Builder(_width, _height);

List<Font> fontList = new ArrayList<Font>();

List<Color> colorList = new ArrayList<Color>();

colorList.add(_CodeColor);

fontList.add(new Font(_FontName, _FontType, _FontSize));

WordRenderer dwr = new ColoredEdgesWordRenderer(colorList, fontList);

captcha.addText(new DefaultTextProducer(_CodeCount, numberChar), dwr);

for (int i = 0; i < _NoiseCount; i++) {

captcha.addNoise(new CurvedLineNoiseProducer(_NoiseColor, _NoiseSize));

}

captcha.gimp(new FishEyeGimpyRenderer(new Color(0, 0, 0, 0), new Color(0, 0, 0, 0)));

captcha.gimp(new RippleGimpyRenderer());

captcha.build();

Captcha captchas = captcha.build();

CaptchaServletUtil.writeImage(resp, captchas.getImage());

CodeMemory = captchas.getAnswer();

}

//比较验证码

public boolean compareCode(String Code) {

if (null == Code || "".equals(Code)) {

return false;

} else {

boolean bz;

System.out.println(whetherOpenBigOrSmall);

if (whetherOpenBigOrSmall) {

bz = CodeMemory.equals(Code);

} else {

bz = CodeMemory.equalsIgnoreCase(Code);

}

return bz;

}

}

public boolean isWhetherOpenBigOrSmall() {

return whetherOpenBigOrSmall;

}

public void setWhetherOpenBigOrSmall(boolean whetherOpenBigOrSmall) {

this.whetherOpenBigOrSmall = whetherOpenBigOrSmall;

}

public char[] getNumberChar() {

return numberChar;

}

public void setNumberChar(char[] numberChar) {

this.numberChar = numberChar;

}

public int getCodeCount() {

return _CodeCount;

}

public void setCodeCount(int _CodeCount) {

this._CodeCount = _CodeCount;

}

public int getWidth() {

return _width;

}

public void setWidth(int _width) {

this._width = _width;

}

public int getHeight() {

return _height;

}

public void setHeight(int _height) {

this._height = _height;

}

public Color getCodeColor() {

return _CodeColor;

}

public void setCodeColor(Color _CodeColor) {

this._CodeColor = _CodeColor;

}

public String getFontName() {

return _FontName;

}

public void setFontName(String _FontName) {

this._FontName = _FontName;

}

public int getFontType() {

return _FontType;

}

public void setFontType(int _FontType) {

this._FontType = _FontType;

}

public int getFontSize() {

return _FontSize;

}

public void setFontSize(int _FontSize) {

this._FontSize = _FontSize;

}

public Color getNoiseColor() {

return _NoiseColor;

}

public void setNoiseColor(Color _NoiseColor) {

this._NoiseColor = _NoiseColor;

}

public int getNoiseSize() {

return _NoiseSize;

}

public void setNoiseSize(int _NoiseSize) {

this._NoiseSize = _NoiseSize;

}

public int getNoiseCount() {

return _NoiseCount;

}

public void setNoiseCount(int _NoiseCount) {

this._NoiseCount = _NoiseCount;

}

}

调用和比较:

@RequestMapping(value = "/imagesanpeng", method = RequestMethod.GET)

protected void imagesanpeng(HttpServletRequest req, HttpServletResponse resp)

throws Exception {

CodeMaker c = new CodeMaker();

c.getCode(resp);

req.getSession().setAttribute("code", c);

}

@RequestMapping(value = "/txmbj", method = RequestMethod.POST)

protected void txmbj(HttpServletRequest req, HttpServletResponse resp,

@RequestParam(value = "txyzm", required = true) String txyzm)

throws Exception {

CodeMaker c = (CodeMaker) req.getSession().getAttribute("code");

c.setWhetherOpenBigOrSmall(true);

resp.getWriter().print(c.compareCode(txyzm));

resp.getWriter().flush();

resp.getWriter().close();

// CodeMaker c = new CodeMaker();

// c.setWhetherOpenBigOrSmall(true);

// resp.getWriter().print(c.compareCode(req, resp, txyzm));

// resp.getWriter().flush();

// resp.getWriter().close();

}

相关推荐

C#.NET Autofac 详解(c# autoit)

简介Autofac是一个成熟的、功能丰富的.NET依赖注入(DI)容器。相比于内置容器,它额外提供:模块化注册、装饰器(Decorator)、拦截器(Interceptor)、强o的属性/方法注...

webapi 全流程(webapi怎么部署)

C#中的WebAPIMinimalApi没有控制器,普通api有控制器,MinimalApi是直达型,精简了很多中间代码,广泛适用于微服务架构MinimalApi一切都在组控制台应用程序类【Progr...

.NET外挂系列:3. 了解 harmony 中灵活的纯手工注入方式

一:背景1.讲故事上一篇我们讲到了注解特性,harmony在内部提供了20个HarmonyPatch重载方法尽可能的让大家满足业务开发,那时候我也说了,特性虽然简单粗暴,但只能解决95%...

C# 使用SemanticKernel调用本地大模型deepseek

一、先使用ollama部署好deepseek大模型。具体部署请看前面的头条使用ollama进行本地化部署deepseek大模型二、创建一个空的控制台dotnetnewconsole//添加依赖...

C#.NET 中间件详解(.net core中间件use和run)

简介中间件(Middleware)是ASP.NETCore的核心组件,用于处理HTTP请求和响应的管道机制。它是基于管道模型的轻量级、模块化设计,允许开发者在请求处理过程中插入自定义逻辑。...

IoC 自动注入:让依赖注册不再重复劳动

在ASP.NETCore中,IoC(控制反转)功能通过依赖注入(DI)实现。ASP.NETCore有一个内置的依赖注入容器,可以自动完成依赖注入。我们可以结合反射、特性或程序集扫描来实现自动...

C#.NET 依赖注入详解(c#依赖注入的三种方式)

简介在C#.NET中,依赖注入(DependencyInjection,简称DI)是一种设计模式,用于实现控制反转(InversionofControl,IoC),以降低代码耦合、提高可...

C#从零开始实现一个特性的自动注入功能

在现代软件开发中,依赖注入(DependencyInjection,DI)是实现松耦合、模块化和可测试代码的一个重要实践。C#提供了优秀的DI容器,如ASP.NETCore中自带的Micr...

C#.NET 仓储模式详解(c#仓库货物管理系统)

简介仓储模式(RepositoryPattern)是一种数据访问抽象模式,它在领域模型和数据访问层之间创建了一个隔离层,使得领域模型无需直接与数据访问逻辑交互。仓储模式的核心思想是将数据访问逻辑封装...

C#.NET 泛型详解(c# 泛型 滥用)

简介泛型(Generics)是指在类型或方法定义时使用类型参数,以实现类型安全、可重用和高性能的数据结构与算法为什么需要泛型类型安全防止“装箱/拆箱”带来的性能损耗,并在编译时检测类型错误。可重用同一...

数据分析-相关性分析(相关性 分析)

相关性分析是一种统计方法,用于衡量两个或多个变量之间的关系强度和方向。它通过计算相关系数来量化变量间的线性关系,从而帮助理解变量之间的相互影响。相关性分析常用于数据探索和假设检验,是数据分析和统计建模...

geom_smooth()函数-R语言ggplot2快速入门18

在每节,先运行以下这几行程序。library(ggplot2)library(ggpubr)library(ggtext)#用于个性化图表library(dplyr)#用于数据处理p...

规范申报易错要素解析(规范申报易错要素解析)

为什么要规范申报?规范申报是以满足海关监管、征税、统计等工作为目的,纳税义务人及其代理人依法向海关如实申报的行为,也是海关审接单环节依法监管的重要工作。企业申报的内容须符合《中华人民共和国海关进出口货...

「Eurora」海关编码归类 全球海关编码查询 关务服务

  海关编码是什么?  海关编码即HS编码,为编码协调制度的简称。  其全称为《商品名称及编码协调制度的国际公约》(InternationalConventionforHarmonizedCo...

9月1日起,河南省税务部门对豆制品加工业试行新政7类豆制品均适用投入产出法

全媒体记者杨晓川报道9月2日,记者从税务部门获悉,为减轻纳税人税收负担,完善农产品增值税进项税额抵扣机制,根据相关规定,结合我省实际情况,经广泛调查研究和征求意见,从9月1日起,我省税务部门对豆制品...