⑴ java怎麼生成驗證碼圖片
以下僅供參考:
這是jsp頁面,起個名字例如叫image.jsp:
<%@ page contentType="image/JPEG"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="GBK"%><%!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);
}%><%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 100; 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);
}
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
session.setAttribute("code", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
在你需要引入的地方:
<img border=0 src="image.jsp">
就可以了。。。
⑵ 用java隨機生成四位驗證碼,只求編程代碼
不知道你問的是不是生成這種圖片驗證碼?
如果只要一個隨機四位數 那這行代碼就夠了(new Random().nextInt(9000) + 1000;),
如果是生成頁面圖片驗證碼就是下面的了:
//設定 響應模式
resp.setContentType("image/jpeg");
// 生成令牌環數據;
Integer token = new Random().nextInt(9000) + 1000;
// 保存令牌環數據到session中
req.getSession().setAttribute(IMAGE_TOKEN_NAME, token);
// 生成令牌環圖片
ServletOutputStream out = resp.getOutputStream();
BufferedImage img = new BufferedImage(60, 20,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0, 0, img.getWidth(), img.getHeight());
g.setColor(Color.BLUE);
g.setFont(new Font("", Font.BOLD, 18));
g.drawString(String.valueOf(token), 10, 16);
ImageIO.write(img, "jpg", out);
out.close();
⑶ java怎麼實現隨機4個帶有數字和字母的驗證碼
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.util.Random;
importjavax.imageio.ImageIO;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
publicclassRandomValidateCode{
="RANDOMVALIDATECODEKEY";//放到session中的key
privateRandomrandom=newRandom();
privateStringrandString="";//隨機產生的字元串
privateintwidth=80;//圖片寬
privateintheight=26;//圖片高
privateintlineSize=40;//干擾線數量
privateintstringNum=4;//隨機產生字元數量
/*
*獲得字體
*/
privateFontgetFont(){
returnnewFont("Fixedsys",Font.CENTER_BASELINE,18);
}
/*
*獲得顏色
*/
privateColorgetRandColor(intfc,intbc){
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc-16);
intg=fc+random.nextInt(bc-fc-14);
intb=fc+random.nextInt(bc-fc-18);
returnnewColor(r,g,b);
}
/**
*生成隨機圖片
*/
publicvoidgetRandcode(HttpServletRequestrequest,
HttpServletResponseresponse){
HttpSessionsession=request.getSession();
//BufferedImage類是具有緩沖區的Image類,Image類是用於描述圖像信息的類
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphicsg=image.getGraphics();//產生Image對象的Graphics對象,改對象可以在圖像上進行各種繪制操作
g.fillRect(0,0,width,height);
g.setFont(newFont("TimesNewRoman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(110,133));
//繪制干擾線
for(inti=0;i<=lineSize;i++){
drowLine(g);
}
//繪制隨機字元
StringrandomString="";
for(inti=1;i<=stringNum;i++){
randomString=drowString(g,randomString,i);
}
session.removeAttribute(RANDOMCODEKEY);
session.setAttribute(RANDOMCODEKEY,randomString);
System.out.println(randomString);
g.dispose();
try{
ImageIO.write(image,"JPEG",response.getOutputStream());//將內存中的圖片通過流動形式輸出到客戶端
}catch(Exceptione){
e.printStackTrace();
}
}
/*
*繪制字元串
*/
privateStringdrowString(Graphicsg,StringrandomString,inti){
g.setFont(getFont());
g.setColor(newColor(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
Stringrand=String.valueOf(getRandomString(random.nextInt(randString.length())));
randomString+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
returnrandomString;
}
/*
*繪制干擾線
*/
privatevoiddrowLine(Graphicsg){
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(13);
intyl=random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
/*
*獲取隨機的字元
*/
publicStringgetRandomString(intnum){
returnString.valueOf(randString.charAt(num));
}
}
⑷ 用java怎麼製作驗證碼
驗證方法很多
蠢一點的後台寫代碼,或者前台頁面加js
當然你用框架自帶的也行,例如struts的