web驗證碼

jsp頁面上:

<META HTTP-EQUIV="pragma" CONTENT="no-cache">

<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">

<meta http-equiv="expires" content="0">


<img id="vials" src="createPicture" style="height: 30px; width: 100px;">

//createPicture是control中的方法

function createPicture() {

var bigimg = document.getElementById("vials");

var url = "createPicture?m="+new Date();//new Date();防止ie緩存

bigimg.url;< /p>

};


java類產生數字與英文的驗證碼

package com.chinasofti.ljzs.cigaretteauthenticityquery.util;


import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.p_w_picpath.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.util.Random;


import javax.p_w_picpathio.ImageIO;

import javax.p_w_picpathio.stream.ImageOutputStream;


public class VerificationCodeUtil {

private ByteArrayInputStream p_w_picpath;// 圖像

private String str;// 驗證碼

private static final int WIDTH = 80;

private static final int HEIGHT = 20;

BufferedImage p_w_picpaths;

public BufferedImage getImages() {

return p_w_picpaths;

}


public void setImages(BufferedImage p_w_picpaths) {

this.p_w_picpaths = p_w_picpaths;

}


public static void main(String[] arg) {

VerificationCodeUtil vcu = VerificationCodeUtil.Instance();

System.err.println(vcu.getVerificationCodeValue());

}


public static VerificationCodeUtil Instance() {

return new VerificationCodeUtil();

}


private VerificationCodeUtil() {

p_w_picpaths = new BufferedImage(WIDTH, HEIGHT,

BufferedImage.TYPE_INT_RGB);

//int randomNum = new Random().nextInt(1);

//if (randomNum == 1) {

initLetterAndNumVerificationCode(p_w_picpaths);

//}

}


public void initLetterAndNumVerificationCode(BufferedImage p_w_picpath) {


Random random = new Random(); // 生成隨機類

Graphics g = initImage(p_w_picpath, random);

String[] letter = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",

"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",

"W", "X", "Y", "Z" };

String sRand = "";

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

String tempRand = "";

if (random.nextBoolean()) {

tempRand = String.valueOf(random.nextInt(10));

} else {

tempRand = letter[random.nextInt(25)];

if (random.nextBoolean()) {// 隨機將該字母變成小寫

tempRand = tempRand.toLowerCase();

}

}

sRand += tempRand;

g.setColor(new Color(20 + random.nextInt(10), 20 + random

.nextInt(110), 20 + random.nextInt(110)));

g.drawString(tempRand, 13 * i + 6, 16);

}

this.setStr(sRand);/* 賦值驗證碼 */

g.dispose(); // 圖象生效

this.setImage(drawImage(p_w_picpath));

}


public Graphics initImage(BufferedImage p_w_picpath, Random random) {

Graphics g = p_w_picpath.getGraphics(); // 獲取圖形上下文

g.setColor(getRandColor(200, 250));// 設定背景色

g.fillRect(0, 0, WIDTH, HEIGHT);

g.setFont(new Font("Times New Roman", Font.PLAIN, 20));// 設定字體

g.setColor(getRandColor(160, 200)); // 隨機產生165條幹擾線,使圖象中的認證碼不易被其它程序探測到

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

int x = random.nextInt(WIDTH);

int y = random.nextInt(HEIGHT);

int xl = random.nextInt(12);

int yl = random.nextInt(12);

g.drawLine(x, y, x + xl, y + yl);

}

return g;

}


public ByteArrayInputStream drawImage(BufferedImage p_w_picpath) {

ByteArrayInputStream input = null;

ByteArrayOutputStream output = new ByteArrayOutputStream();

try {

ImageOutputStream p_w_picpathOut = ImageIO

.createImageOutputStream(output);

ImageIO.write(p_w_picpath, "JPEG", p_w_picpathOut);

p_w_picpathOut.close();

input = new ByteArrayInputStream(output.toByteArray());

} catch (Exception e) {

System.out.println("驗證碼圖片產生出現錯誤:" + e.toString());

}

return input;

}


/*

* 功能:給定範圍獲得隨機顏色

*/

private Color getRandColor(int fc, int bc) {

Random random = new Random();

if (fc > 255)

fc = 255;

if (bc > 255)

bc = 255;

int r = fc + random.nextInt(bc - fc);

int g = fc + random.nextInt(bc - fc);

int b = fc + random.nextInt(bc - fc);

return new Color(r, g, b);

}


/**

* 功能:獲取驗證碼的字符串值

*

* @return

*/

public String getVerificationCodeValue() {

return this.getStr();

}


/**

* 功能:取得驗證碼圖片

*

* @return

*/

public ByteArrayInputStream getImage() {

return this.p_w_picpath;

}


public String getStr() {

return str;

}


public void setStr(String str) {

this.str = str;

}


public void setImage(ByteArrayInputStream p_w_picpath) {

this.p_w_picpath = p_w_picpath;

}

}


control層中

ImageIO.write(vcu.getImages(), "jpeg",response().getOutputStream());

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章