JAVA 验证码生成。SimpleCaptcha

去官方网站下载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();

}

时间: 2024-10-05 15:00:54

JAVA 验证码生成。SimpleCaptcha的相关文章

java验证码生成

第一种方式:纯代码 先写jsp: 在编写CaptServlet: 1 package cn.itcast.web.controller; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.image.BufferedImage; 7 /** 8 * 用户登陆 9 */ 10 import java.io.IOException; 11 import ja

用java Graphics生成验证码

以下下是API文档对Graphics的介绍! Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制. Graphics 对象封装了 Java 支持的基本呈现操作所需的状态信息.此状态信息包括以下属性: 要在其上绘制的 Component 对象. 呈现和剪贴坐标的转换原点. 当前剪贴区. 当前颜色. 当前字体. 当前逻辑像素操作函数(XOR 或 Paint). 当前 XOR 交替颜色(参见 setXORMode(java.awt.Colo

java实现生成简单图片验证码

原文:java实现生成简单图片验证码 源代码下载地址:http://www.zuidaima.com/share/1550463428840448.htm 项目载图: 该项目要转换为Dynamic web project http://www.zuidaima.com/blog/1618162161323008.htm 另外缺少jar包: http://www.zuidaima.com/jar/search/jstl-1.0.1.htm http://www.zuidaima.com/jar/s

java自动生成验证码

代码结构: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="

java web验证码生成总结(包括servlet、jsp和struts2实现)(转)

一.使用纯Servlet实现验证码 (1)在web.xml配置: [java] view plaincopy <servlet> <servlet-name>image</servlet-name> <servlet-class>org.test.web.AuthImage</servlet-class> </servlet> <servlet-mapping> <servlet-name>image</

浅谈JAVA验证码~

这两天在帮同学做个项目,项目中需要做个验证码,说实话那么多年竟然没注意过这东西,原理很简单,贴出来给大家做个参考. 1.简单介绍 一般稍微有些经验的程序员都不会再自己写原生验证码生成了,因为各种强大的开源组件,足以解决我们大部分的需求.但是,毕竟也是刚接触这东西,还需要从原理入手的. 项目效果图: 下面我就简单介绍下原生和使用开源项目kaptcha生成验证码的两种形式. 2.jdk原生生成验证码 效果: 2.1 验证码生成的流程 1.定义BufferedImage(图像数据缓冲区)对象 2.获得

利用谷歌 kaptcha 进行验证码生成

1 package main.com.smart.controller; 2 3 import com.google.code.kaptcha.Producer; 4 import main.com.smart.utils.CaptchaProducer; 5 import main.com.smart.utils.RedisClient; 6 import org.apache.log4j.Logger; 7 import org.apache.log4j.spi.LoggerFactory;

随机验证码生成代码 (转)

随机验证码生成代码 package com.zuidaima.core.util; import java.util.Random; public class RandomUtil { public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String LETTERCHAR = "abc

Web---图片验证码生成教程详解-从简单到复杂-从本地到前后台

首先,我们先来看本地如何生成图片验证码的,再来写输出到网页的验证码如何实现. 先来看最简单的-实现的功能是,将一个字符串变成图片写入到文件中 实现代码: package cn.hncu.img; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOE