java生成验证码图片

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { BufferedImage image = new BufferedImage(, ,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 1.设置背景色
setBackGround(g); // 2.设置边框
setBorder(g); // 3.画干扰线
drawRandomLine(g); // 4.写随机数
drawRandomNum(g); // 5.图形写给浏览器
response.setContentType("image/jpeg");
ImageIO.write(image, "jpg", response.getOutputStream()); } private void drawRandomNum(Graphics g) {
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD,));
//所有汉字区间 \u4e00 \u9fa5
for (int i = ; i < ; i++) {
int r=new Random().nextInt();
int r2=new Random().nextInt();
g.drawString("好", r, r2);
} } private void drawRandomLine(Graphics g) {
g.setColor(Color.GREEN);
for (int i = ; i < ; i++) {
int x1 = new Random().nextInt();
int y1 = new Random().nextInt();
int x2 = new Random().nextInt();
int y2 = new Random().nextInt();
g.drawLine(x1, y1, x2, y2);
}
} private void setBorder(Graphics g) {
g.setColor(Color.BLUE);
g.drawRect(, , , );
} private void setBackGround(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(, , , );
}
上一篇:测试用(编写优质嵌入式C程序)


下一篇:Windows7 下配置添加ASP功能