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();
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
JAVA 代码生成。SimpleCaptcha的更多相关文章
- java代码生成二维码
java代码生成二维码一般步骤 常用的是Google的Zxing来生成二维码,生成的一般步骤如下: 一.下载zxing-core的jar包: 二.需要创建一个MatrixToImageWriter类, ...
- java代码生成二维码以及解析二维码
package com.test; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedI ...
- Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解
废话不多说,本文将会层层深入给大家讲解如何动态的生成一个完整的界面. 本文内容: Java代码中动态生成View Java代码中动态设置View的位置,以及其他的属性 LayoutParams详解 一 ...
- 如何将java代码生成一个bat文件
java -cp classes;lib/* beans.FileUpload 列出所要带的参数,用空格分开Pause
- java代码生成Excel文件3000条自定义属性的的域账户名
一个项目为了测试需要模拟3000条域用户,将数据保存在Excel表格,然后导入到与服务器里. 我们今天要做的是自动生成3000条数据,并将这些数据保存在excel表格里面. 需要jar包:poi-3. ...
- 使用原生Java代码生成可执行Jar包
最近想做一个功能,就是把我们编译后的字节码及其资源文件打包成一个可执行的jar包,在装有jre的机器上双击就能运行. 首先是我们需要选择哪些字节码和文件需要打包到文件中,这个我们用JFileChoos ...
- 利用java代码生成keyStore
在前面的章节中介绍了如何利用KeyTool工具生成keyStore:传送门. 但是很多时候,在javaWeb项目中,比如给每个用户加上独特的数字签名,那么我们需要在创建用户的时候,给其生成独一无二的k ...
- 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl
目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...
- 基于mybatis的java代码生成存储过程
问题: 项目中目前使用mybatis操作数据库,使用插件(mybatis-generator)自动生成代码,对于增改查,使用存储过程实现了一版本,方便使用. insert代码生成器用法: insert ...
随机推荐
- ProducerConsumerDemo
package algorithm; public class ProducerConsumer { public static void main(String[] args) { SyncStac ...
- hdoj1010Starship Troopers (树dp,依赖背包)
称号:hdoj1010Starship Troopers 题意:有一个军队n个人要占据m个城市,每一个城市有cap的驻扎兵力和val的珠宝,并且这m个城市的占率先后具有依赖关系,军队的每一个人能够打败 ...
- C++生产和使用的临时对象
所谓暂时对象就是一种无名对象. 它的出现假设不在程序猿的预期之下(比如不论什么pass by value操作都会引发copy操作,于是形成一个暂时对象),往往照成效率上的负担. 但有时候能够制造 ...
- 采用WindowManager添加您自己的自定义视图
原文地址:使用WindowManager加入自己定义视图 在写手机卫士的时候,用户拨打|接听电话须要显示号码归属地,然后出现了一些异常.在此留下记号,希望对麻友们有帮助: BUG教程 在使用 view ...
- 【蓝桥杯】 PREV-1 核桃数
主题链接:http://lx.lanqiao.org/problem.page?gpid=T24 历届试题 核桃的数量 时间限制:1.0s 内存限制:256.0MB 问题描写 ...
- 【2014】【】辛星【php】【秋】【1】php构建开发环境
**************************什么是开发环境*********************** 1.我们学习PHP,是使用它来做web用的,通俗理解,就是做站点. 2.站点的执行须要 ...
- SQL入门学习5-函数、为此、CASE表达式
6-1. 各种各样的函数 函数的种类 算数函数 字符串函数 日期函数 转换函数 聚合函数 1.1算术函数 数据类型:NUMERIC 是大多数DBMS都支持的一种数据类型. 通过NUMBERIC(全体位 ...
- jekyll博客安装
摘要: 一直用Mac,换了新公司使用的电脑是windows,网上粗略的看了一下Jekyll的安装.简略的实现了一遍 首先安装Ruby "Ruby安装文件下载地址" 下载对应版本,我 ...
- 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法(转)
对象的synchronized方法不能进入了,但它的其他非synchronized方法还是可以访问的 对每一个class只有一个thread可以执行synchronized static method ...
- 微博API怎么爬取其它未授权用户的微博/怎么爬取指定用户公布的微博
获取某个用户最新发表的微博列表:http://open.weibo.com/wiki/2/statuses/user_timeline 原接口已经被封.很多人都在问怎么获取指定用户的微博,于是写这篇B ...