js代碼
var code;//在全局 定義驗證碼
function createCode(){
code = "";
var codeLength = 4;//驗證碼的長度
//所有候選組成驗證碼的字符,可以用中文
var selectChar = new Array(2,3,4,5,6,7,8,'A','B','C',
'D','E','F','G','H','I','J','K','L','M','N','P',
'Q','R','S','T','U','V','W','X','Y','a','b','c',
'd','e','f','h','i','j','k','m','n','p',
'q','r','s','t','u','v','w','x','y');
for(var i=0;i<codeLength;i++){
var charIndex = Math.floor(Math.random()*60);
code +=selectChar[charIndex];
}
return code;
}
function show(){
//顯示驗證碼
$("#imgVcode").prop("src","項目名字/生成圖片的action?code="+createCode());
}
java代碼
package com.gyj.util;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.patchca.background.BackgroundFactory;
import org.patchca.color.ColorFactory;
import org.patchca.color.RandomColorFactory;
import org.patchca.filter.ConfigurableFilterFactory;
import org.patchca.filter.library.AbstractImageOp;
import org.patchca.filter.library.WobbleImageOp;
import org.patchca.font.RandomFontFactory;
import org.patchca.service.Captcha;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.text.renderer.BestFitTextRenderer;
import org.patchca.text.renderer.TextRenderer;
import org.patchca.word.RandomWordFactory;
import com.opensymphony.xwork2.Action;
public class CodeJs implements Action {
private ConfigurableCaptchaService configurableCaptchaService = null;
private ColorFactory colorFactory = null;
private RandomFontFactory fontFactory = null;
private SetCode wordFactory = null;
private TextRenderer textRenderer = null;
public void init(){
configurableCaptchaService = new ConfigurableCaptchaService();
// 顏色創建工廠,使用一定範圍內的隨機色
colorFactory = new RandomColorFactory();
configurableCaptchaService.setColorFactory(colorFactory);
// 隨機字體生成器
fontFactory = new RandomFontFactory();
fontFactory.setMaxSize(32);
fontFactory.setMinSize(28);
configurableCaptchaService.setFontFactory(fontFactory);
HttpServletRequest request = ServletActionContext.getRequest();
// 隨機字符生成器,去除掉容易混淆的字母和數字,如o和0等
String code = request.getParameter("code");
wordFactory = new SetCode();
wordFactory.setCharacters(code);
configurableCaptchaService.setWordFactory(wordFactory);
// 自定義驗證碼圖片背景
MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();
configurableCaptchaService.setBackgroundFactory(backgroundFactory);
// 圖片濾鏡設置
ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory();
List<BufferedImageOp> filters = new ArrayList<BufferedImageOp>();
WobbleImageOp wobbleImageOp = new WobbleImageOp();
wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR);
wobbleImageOp.setxAmplitude(2.0);
wobbleImageOp.setyAmplitude(1.0);
filters.add(wobbleImageOp);
filterFactory.setFilters(filters);
configurableCaptchaService.setFilterFactory(filterFactory);
// 文字渲染器設置
textRenderer = new BestFitTextRenderer();
textRenderer.setBottomMargin(3);
textRenderer.setTopMargin(3);
configurableCaptchaService.setTextRenderer(textRenderer);
// 驗證碼圖片的大小
configurableCaptchaService.setWidth(82);
configurableCaptchaService.setHeight(32);
}
public CodeJs() {
super();
}
/**
* 銷燬方法,負責銷燬所使用資源. <br>
*/
public void destroy() {
wordFactory = null;
colorFactory = null;
fontFactory = null;
textRenderer = null;
configurableCaptchaService = null;
}
@Override
public String execute() throws Exception {
init();
HttpServletResponse response = ServletActionContext.getResponse();
// TODO Auto-generated method stub
response.setContentType("image/png");
response.setHeader("cache", "no-cache");
OutputStream outputStream = response.getOutputStream();
// 得到驗證碼對象,有驗證碼圖片和驗證碼字符串
Captcha captcha = configurableCaptchaService.getCaptcha();
// 取得驗證碼圖片並輸出
BufferedImage bufferedImage = captcha.getImage();
ImageIO.write(bufferedImage, "png", outputStream);
outputStream.flush();
outputStream.close();
destroy();
return null;
}
/**
* 自定義驗證碼圖片背景,主要畫一些噪點和干擾線
*/
private class MyCustomBackgroundFactory implements BackgroundFactory {
private Random random = new Random();
public void fillBackground(BufferedImage image) {
Graphics graphics = image.getGraphics();
// 驗證碼圖片的寬高
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// 填充爲白色背景
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imgWidth, imgHeight);
// 畫100個噪點(顏色及位置隨機)
for (int i = 0; i < 100; i++) {
// 隨機顏色
int rInt = random.nextInt(255);
int gInt = random.nextInt(255);
int bInt = random.nextInt(255);
graphics.setColor(new Color(rInt, gInt, bInt));
// 隨機位置
int xInt = random.nextInt(imgWidth - 3);
int yInt = random.nextInt(imgHeight - 2);
// 隨機旋轉角度
int sAngleInt = random.nextInt(360);
int eAngleInt = random.nextInt(360);
// 隨機大小
int wInt = random.nextInt(6);
int hInt = random.nextInt(6);
graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
// 畫5條幹擾線
if (i % 20 == 0) {
int xInt2 = random.nextInt(imgWidth);
int yInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}
private class SetCode extends RandomWordFactory{
@Override
public String getNextWord() {
// TODO Auto-generated method stub
return super.characters;
}
@Override
public void setCharacters(String characters) {
// TODO Auto-generated method stub
super.setCharacters(characters);
}
@Override
public void setMaxLength(int maxLength) {
super.setMaxLength(1);
}
@Override
public void setMinLength(int minLength) {
super.setMinLength(0);
}
}
}
mevan項目的依賴
<dependency>
<groupId>net.pusuo</groupId>
<artifactId>patchca</artifactId>
<version>0.5.0</version>
</dependency>