A. 怎么用java代码实现一个验证码,求具体实现方法

packageutil;

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;

publicfinalclassImageUtil{

//验证码字符集
privatestaticfinalchar[]chars={
'0','1','2','3','4','5','6','7','8','9',
'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',
'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'};
//字符数量
privatestaticfinalintSIZE=4;
//干扰线数量
privatestaticfinalintLINES=5;
//宽度
privatestaticfinalintWIDTH=80;
//高度
privatestaticfinalintHEIGHT=40;
//字体大小
privatestaticfinalintFONT_SIZE=30;

/**
*生成随机验证码及图片
*返回的数组中,第1个值是验证码,第2个值是图片
*/
publicstaticObject[]createImage(){
StringBuffersb=newStringBuffer();
//1.创建空白图片
BufferedImageimage=newBufferedImage(
WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
//2.获取图片画笔
Graphicsgraphic=image.getGraphics();
//3.设置画笔颜色
graphic.setColor(Color.LIGHT_GRAY);
//4.绘制矩形背景
graphic.fillRect(0,0,WIDTH,HEIGHT);
//5.画随机字符
Randomran=newRandom();
for(inti=0;i<SIZE;i++){
//取随机字符索引
intn=ran.nextInt(chars.length);
//设置随机颜色
graphic.setColor(getRandomColor());
//设置字体大小
graphic.setFont(newFont(
null,Font.BOLD+Font.ITALIC,FONT_SIZE));
//画字符
graphic.drawString(
chars[n]+"",i*WIDTH/SIZE,HEIGHT/2);
//记录字符
sb.append(chars[n]);
}
//6.画干扰线
for(inti=0;i<LINES;i++){
//设置随机颜色
graphic.setColor(getRandomColor());
//随机画线
graphic.drawLine(ran.nextInt(WIDTH),ran.nextInt(HEIGHT),
ran.nextInt(WIDTH),ran.nextInt(HEIGHT));
}
//7.返回验证码和图片
returnnewObject[]{sb.toString(),image};
}

/**
*随机取色
*/
(){
Randomran=newRandom();
Colorcolor=newColor(ran.nextInt(256),
ran.nextInt(256),ran.nextInt(256));
returncolor;
}

publicstaticvoidmain(String[]args)throwsIOException{
Object[]objs=createImage();
BufferedImageimage=(BufferedImage)objs[1];
OutputStreamos=newFileOutputStream("d:/1.png");
ImageIO.write(image,"jpeg",os);
os.close();
}

}

B. 求java语言随机生成图片验证码的程序代码,有详细注释最好,本人初学者,谢谢各位的帮忙!!!

楼上正解

/**
*图片验证码
*
*@authorHuangYong
*
*/
{
=1532787236715859525L;
/**
*验证图片宽
*/
privatestaticfinalintWIDTH=80;

/**
*验证图片高度
*/
privatestaticfinalintHEIGHT=20;

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//获得图片
BufferedImageimage=newBufferedImage(WIDTH,HEIGHT,
BufferedImage.TYPE_INT_RGB);
//获取画笔
Graphicsg=image.getGraphics();

//设置边框
setBounds(g);

////设置背景
//setBackGround(g);

//干扰线
drawRandomLine(g);

//验证码
drawRandomString((Graphics2D)g,request);

//发送给页面
response.setContentType("image/jpeg");
response.setDateHeader("expries",-1);
response.setHeader("cache-control","no-cache");
response.setHeader("progma","no-cache");
ImageIO.write(image,"jpg",response.getOutputStream());
}

/**
*边框
*
*@paramg
*/
privatevoidsetBounds(Graphicsg){

g.setColor(Color.BLUE);
g.fillRect(0,0,WIDTH,HEIGHT);

g.setColor(Color.WHITE);
g.fillRect(1,1,WIDTH-2,HEIGHT-2);

}

/**
*背景
*
*@paramg
*/
//privatevoidsetBackGround(Graphicsg){
//
//g.setColor(Color.WHITE);
//g.drawRect(1,1,WIDTH-2,HEIGHT-2);
//
//}

/**
*干扰线
*
*@paramg
*/
privatevoiddrawRandomLine(Graphicsg){

g.setColor(Color.GREEN);

intx1,y1;
intx2,y2;

Randomr=newRandom();

for(inti=0;i<5;i++){
//起始xy
x1=r.nextInt(WIDTH);
y1=r.nextInt(HEIGHT);
//结束xy
x2=r.nextInt(WIDTH);
y2=r.nextInt(HEIGHT);

g.drawLine(x1,y1,x2,y2);
}

}

/**
*验证码
*
*@paramg
*/
privatevoiddrawRandomString(Graphics2Dg,HttpServletRequestrequest){

g.setColor(Color.RED);
g.setFont(newFont("宋体",Font.BOLD,20));
Stringbase="";
StringBuildersb=newStringBuilder();
charch='u0000';
intdegree=0;
Randomr=newRandom();
intstartX=8;

for(inti=0;i<4;i++){
ch=base.charAt(r.nextInt(base.length()));

//设置旋转,±20°
degree=r.nextInt()%20;
g.rotate(degree*Math.PI/180,startX,14);
g.drawString(ch+"",startX,16);
//取消旋转
g.rotate(-degree*Math.PI/180,startX,14);

startX+=18;
sb.append(ch);
}

request.getSession().setAttribute("checkCode",sb.toString().toLowerCase());
}

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}

希望能帮助你

C. 求一篇用java生成的验证码程序。。。

把生成验证码图片的程序封装为一个JavaBean,这个JavaBean的源代码如下:
makeCertPic.java
package pic;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/**
* @author dzy
* 生成验证码图片
*/
public class makeCertPic {
//验证码图片中可以出现的字符集,可根据需要修改
private char mapTable[]={
'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','0','1','2','3',
'4','5','6','7','8','9'};
/**
* 功能:生成彩色验证码图片
* 参数width为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流
*/
public String getCertPic(int width, int height, OutputStream os) {
if(width<=0)width=60;
if(height<=0)height=20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取随机产生的认证码
String strEnsure = "";
// 4代表4位验证码,如果要生成更多位的认证码,则加大数值
for(int i=0; i<4; ++i) {
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
// 将认证码显示到图像中,如果要生成更多位的认证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str,20,15);
str = strEnsure.substring(2,3);
g.drawString(str,35,18);
str = strEnsure.substring(3,4);
g.drawString(str,45,15);
// 随机产生10个干扰点
Random rand = new Random();
for (int i=0;i<10;i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
// 释放图形上下文
g.dispose();
try {
// 输出图像到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
return "";
}
return strEnsure;
}
} 在getCertPic()方法中,首先创建了一个内存图像的实例对象,再得到此内存图像的图形上下文对象,接着再用这个上下文对象画背景、边框。接下来,随机生成4个在mapTable[]数组中的字符,组成字符串作为验证字符串,并输出在内存中,为了造成一定的干扰,随机画了10个干扰点,如果要加大干扰效果,可再多画一些点。
makeCertPic.jsp页面用于调用生成验证码图片的JavaBean,并在客户端显示,源代码如下:
makeCertPic.jsp
<%@page contentType="image/jpeg" %>
<jsp:useBean id="image" scope="page" class="pic.makeCertPic" />
<%
Stringstr=image.getCertPic(0,0,response.getOutputStream());
// 将认证码存入SESSION
session.setAttribute("certCode", str);
%>这里把生成的验证码作为session变量写入,因此在接收登录页面输入的数据页面中,可用用户输入的验证码和这个session变量作比较,如果相同则表示验证通过。
LoginPic.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head><title>登录页面</title></head>
<body>
<table align="center" border="0">
<tralign="center"><td><fontcolor="red"><html:errors/></font></td></tr>
<tr align="center"><td>系统登录</td></tr>
<form. action="loginCheck.jsp" method="post" focus="username">
<tr><td>用户名:<input type="text" name="username"/></td></tr>
<tr><td>密 码:<input type="password"name="password"/></td></tr>
<tr><td>验证码:<input type="text" name="certCode"/>
<img src="makeCertPic.jsp">
</td></tr>
<tralign="left"><td>
<input type="submit" value="确定"/></td></tr>
</form>
</table>
</body>
</html> 验证码的输入是否正确可用如下语句验证:
String certCode=request.getParameter("certCode");
if(certCode.equals((String)session.getAttribute("certCode")))
out.print("验证码输入正确");
else
out.print("验证码输入错误");

D. 如何在java中生成验证码在页面生成

以下是我写的生成的图片验证码

//调用验证码生成并把验证码返回页面
Map<String,BufferedImage>imageMap=ImageUtil.createImage();
Stringcode=imageMap.keySet().iterator().next();
session.setAttribute("imageCode",code);
BufferedImageimage=imageMap.get(code);
response.setContentType("image/jpeg");
OutputStreamops=response.getOutputStream();
ImageIO.write(image,"jpeg",ops);
ops.close();
//生成验证码工具类
publicfinalclassImageUtil{

privatestaticfinalchar[]chars={'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','0','1','2','3','4','5','6','7','8','9'};
privatestaticfinalintSIZE=4;
privatestaticfinalintLINES=5;
privatestaticfinalintWIDTH=80;
privatestaticfinalintHEIGHT=40;
privatestaticfinalintFONT_SIZE=30;

publicstaticMap<String,BufferedImage>createImage(){
StringBuffersb=newStringBuffer();
BufferedImageimage=newBufferedImage(WIDTH,HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphicsgraphic=image.getGraphics();
graphic.setColor(Color.LIGHT_GRAY);
graphic.fillRect(0,0,WIDTH,HEIGHT);
Randomran=newRandom();
//画随机字符
for(inti=1;i<=SIZE;i++){
intr=ran.nextInt(chars.length);
graphic.setColor(getRandomColor());
graphic.setFont(newFont(null,Font.BOLD+Font.ITALIC,FONT_SIZE));
graphic.drawString(chars[r]+"",(i-1)*WIDTH/SIZE,HEIGHT/2);
sb.append(chars[r]);
}
//画干扰线
for(inti=1;i<=LINES;i++){
graphic.setColor(getRandomColor());
graphic.drawLine(ran.nextInt(WIDTH),ran.nextInt(HEIGHT),
ran.nextInt(WIDTH),ran.nextInt(HEIGHT));
}
Map<String,BufferedImage>map=newHashMap<String,BufferedImage>();
map.put(sb.toString(),image);
returnmap;
}

(){
Randomran=newRandom();
Colorcolor=newColor(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256));
returncolor;
}

(BufferedImageimage)
throwsIOException{
ByteArrayOutputStreambos=newByteArrayOutputStream();
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
byte[]imageBts=bos.toByteArray();
InputStreamin=newByteArrayInputStream(imageBts);
returnin;
}
}

E. JAVA识别图片验证码

package com.he;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

public class CodeFact
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//设置页面不缓存
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(new Color(33,66,99));
g.drawRect(0,0,width-1,height-1);

// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; 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);
}

// 取随机产生的认证码(4位数字)
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
HttpSession session = request.getSession();
session.setAttribute("rand", sRand);

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

// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

//给定范围获得随机颜色
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);
}

}

你试试!!

F. 用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();

G. 如何用java取出随机生成的验证码图片里的数字

为什么要放在session.太浪费资源。放在request里面。每次请求的时候后台生成一个新的数字就request.setAttribult,页面getAtrribult就可以了。你网络下用法就懂了

H. 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">

就可以了。。。

I. java生成图形验证码成符号了怎么回事

AuthImage类:

[java] view plain
package hh.com.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;