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 ...
随机推荐
- Android做法说明(3)---Fragment使用app袋或v4包解析
Android做法说明(3)---Fragment使用app袋或v4包解析 1)问题简述 相信非常多的朋友在调用Fragment都会遇到以下的情况: watermark/2/text/aHR0cDov ...
- .net与Java的WebService互调
本文记录一下.net与Java是如何进行Web Service的互相调用的. 1.准备工作 MyEclipse 10 JDK 1.6.0_13 Visual Studio 2012 .net fram ...
- Grant的时候报错的解决:Access denied for user 'root'@'localhost' (using password: YES)
mysql> grant all on *.* to 'root'@'192.168.1.1' identified by 'password'; ERROR 1045 (28000): Acc ...
- iOS开发无第三方控件的援助达到的效果侧边栏
最近的研究iOS程序侧边栏.渐渐的发现iOS该方案还开始采取风侧边栏格该,QQ,今日头条,Path(Path运营商最早的侧边栏app该,效果说成是Path效果),所以就研究了下. 然后发现Git Hu ...
- Maven工程引入jar包(转)
Maven项目引入jar包的方法,希望能帮助有需要的朋友们 法一.手动导入:项目右键—>Build Path—>Configure Build Path—>选中Libraries—& ...
- LNK快捷方式漏洞利用方式 exp制作教程
前言windows的shell32在处理控制面板程序的快捷方式文件时,存在一个漏洞,能够载入硬盘上的随意DLL文件,就可以运行随意代码. 漏洞文件的生成到“控制面板”以下,右键点“显示”,点“创建快捷 ...
- 数组名取地址所算数运算应注意的"trap"
数组名取地址所算数运算应注意的"trap" 直接看代码: #include <stdio.h> int main() { int array[5]; printf(&q ...
- C#中实现并发
C#中实现并发的几种方法的性能测试 0x00 起因 去年写的一个程序因为需要在局域网发送消息支持一些命令和简单数据的传输,所以写了一个C/S的通信模块.当时的做法很简单,服务端等待链接,有用户接入后开 ...
- WP8加入自己定义铃声
1.下载千千静听绿色版,下载地址:http://pan.baidu.com/s/1ntuSNOH 2.将目标铃声加入到千千静听,选中铃声,右键选文件属性. 3.改动文件属性,将流派改为"Ri ...
- jQuery.reveal弹出层使用
最近用到弹出层,还得自定义UI,原本用的artDialog太庞大,不合适了,于是就找到了这个东西,又小又好用,基础的弹出遮罩都有了,想要什么还不是Coder自己说了算. 这个插件是基于Jquery实现 ...