java 验证码
步骤:
a.得到图片(ImageIcon)
b.创建画布(BufferedImage)
c.得到画笔(Graphics)
d.制作(drawImage\drawString\drawLine\drawOval)
e.释放画笔(dispose)
f.输出到页面(ImageIO)
在servlet中写,在jsp页面写显示代码
package com.zuxia.servlet;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;
public class RandomServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到验证码的背景图
ImageIcon random = new ImageIcon(this.getServletContext().getRealPath("/images/random.jpg"));
//得到画布
BufferedImage buf = new BufferedImage(random.getIconWidth(), random.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
//得到画笔
Graphics g = buf.getGraphics();
String str = "qwe0rt1yu2iop3asd4fgh5jkl6zx7cvbnm8ASDFGH9JKLQWERTYUIOPZXCVBNM";
StringBuffer sb = new StringBuffer();
Random ran = new Random();
for(int i=1;i<=4;i++){
sb.append(str.charAt(ran.nextInt(str.length())));
}
//制作
g.drawImage(random.getImage(), 0, 0, null);
g.setFont(new Font("黑体", Font.BOLD, 30));
g.drawString(sb.toString(), random.getIconWidth()/4, random.getIconHeight()/2+10);
//杂色
//产生随机线条线条(两点确定一条线)
for(int i=1;i<=10;i++){
int x1 = ran.nextInt(random.getIconWidth());
int y1 = ran.nextInt(random.getIconHeight());
int x2 = ran.nextInt(random.getIconWidth());
int y2 = ran.nextInt(random.getIconHeight());
g.drawLine(x1, y1, x2, y2);
}
//产生随机点
for(int i=1;i<=100;i++){
int x = ran.nextInt(random.getIconWidth());
int y = ran.nextInt(random.getIconHeight());
g.drawOval(x, y, 1, 1);
}
//释放
g.dispose();
//输出
OutputStream os = response.getOutputStream();
ImageIO.write(buf, "jpg", os);
os.close();
}
}
第二种(自己画.不引用图片)
package Upload;
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.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.org.apache.commons.digester.rss.Image;
public class AuthCode extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//定义画布的宽和高
int width =90;
int height=30;
//创建一个画布
BufferedImage buffimage=new BufferedImage(width, height,BufferedImage.TYPE_3BYTE_BGR );
//创建画笔
Graphics g =buffimage.getGraphics();
//产生随机数(四个)
String str = "qwe0rt1yu2iop3asd4fgh5jkl6zx7cvbnm8ASDFGH9JKLQWERTYUIOPZXCVBNM";
StringBuffer sb = new StringBuffer();
Random ran = new Random();
for(int i=1;i<=4;i++){
sb.append(str.charAt(ran.nextInt(str.length())));
}
request.getSession().setAttribute("yzm", sb.toString());
//设置矩形颜色的背景色
g.setColor(new Color(113, 191, 234));
//制作矩形的被背景
g.fillRect(0, 0, width, height);
//设置边框
//设置边框颜色
g.setColor(Color.black);
g.drawRect(0, 0, width-1, height-1);
//制作
g.drawImage(buffimage, buffimage.getType(), 0, 0, height, null, null);
g.setFont(new Font("黑体", Font.BOLD, 30));A
g.drawString(sb.toString(), buffimage.getTileWidth()/8, buffimage.getHeight()/2+10);
//杂色
//线
for(int i=1;i<=10;i++){
int x1 = ran.nextInt(buffimage.getTileWidth());
int y1 = ran.nextInt(buffimage.getHeight());
int x2 = ran.nextInt(buffimage.getTileWidth());
int y2 = ran.nextInt(buffimage.getHeight());
g.drawLine(x1, y1, x2, y2);
}
//点
for(int i=1;i<=100;i++){
int x = ran.nextInt(buffimage.getTileWidth());
int y = ran.nextInt(buffimage.getHeight());
g.drawOval(x, y, 1, 1);
}
//创建输出流对象
OutputStream out =response.getOutputStream();
ImageIO.write(buffimage,"jpg",out);
}
}