package heima.response;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
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;
//输出随机验证码图片
public class YanZhengMa extends HttpServlet
{
private static int WIDTH = 120;
private static int HEIGHT = 25;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//BufferedImage:代表内存图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
//Graphics:代表画笔
Graphics g = image.getGraphics();
//画边框
g.setColor(Color.YELLOW);//画笔颜色
g.drawRect(0, 0, WIDTH, HEIGHT);
//填充背景颜色
g.setColor(Color.GREEN);//画笔颜色
g.fillRect(1, 1, WIDTH-2, HEIGHT-2);//留出边框的位置
//画干扰线:9条
g.setColor(Color.RED);//画笔颜色
Random r = new Random();//随机数
for (int i = 0; i < 9; i++)
{
g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH), r.nextInt(HEIGHT));//设置开始结束坐标,切不能超过边框
}
//画数字:4个
g.setColor(Color.BLUE);//画笔颜色
g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));//设置字体样式(宋体、加粗、斜体、字号)
int x = 20;
for (int i = 0; i < 4; i++)
{
String number = r.nextInt(10)+"";//获取10以内的随机数并转化为字符串
g.drawString(number, x, 20);
x+=15;
}
//ImageIo:输出
ImageIO.write(image, "jpg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
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;
//输出随机验证码图片
public class YanZhengMa extends HttpServlet
{
private static int WIDTH = 120;
private static int HEIGHT = 25;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//BufferedImage:代表内存图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
//Graphics:代表画笔
Graphics g = image.getGraphics();
//画边框
g.setColor(Color.YELLOW);//画笔颜色
g.drawRect(0, 0, WIDTH, HEIGHT);
//填充背景颜色
g.setColor(Color.GREEN);//画笔颜色
g.fillRect(1, 1, WIDTH-2, HEIGHT-2);//留出边框的位置
//画干扰线:9条
g.setColor(Color.RED);//画笔颜色
Random r = new Random();//随机数
for (int i = 0; i < 9; i++)
{
g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH), r.nextInt(HEIGHT));//设置开始结束坐标,切不能超过边框
}
//画数字:4个
g.setColor(Color.BLUE);//画笔颜色
g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));//设置字体样式(宋体、加粗、斜体、字号)
int x = 20;
for (int i = 0; i < 4; i++)
{
String number = r.nextInt(10)+"";//获取10以内的随机数并转化为字符串
g.drawString(number, x, 20);
x+=15;
}
//ImageIo:输出
ImageIO.write(image, "jpg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
}