代码————刷新验证码
来源:互联网 发布:欧洲 旅馆 网络 编辑:程序博客网 时间:2024/05/16 07:36
package cn.itcast.response;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
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 CodeServlet extends HttpServlet {
/**
* 实现验证码的操作
第一步:生成图片
第二步:生成随机的数字和字母
第三步:把数字和字母画到图片上
第四步:把图片显示到页面上
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//生成图片
int width = 150;
int height = 50;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
//得到画笔
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
//生成四个随机的数字和字母
String words = "asdfghjklqwertyuiopzxcvbASDFGHJKLQWERTYUIOPZXCVB1234567890";
//创建Random对象
Random r = new Random();
int x = 25;
int y = 30;
//设置颜色
g2d.setColor(Color.YELLOW);
//设置字体的样式
g2d.setFont(new Font("宋体",Font.BOLD,25));
//rotate(double theta, double x, double y)
//弧度=角度*3.14/180
for(int i=1;i<=4;i++) {
int idx = r.nextInt(words.length());
//根据位置得到具体的字符
char ch = words.charAt(idx);
//旋转+- 30度
int jiaodu = r.nextInt(60)-30;
double hudu = jiaodu*Math.PI/180;
//旋转的效果
//g2d.rotate(hudu, x, y);
//把字符画到图片上
g2d.drawString(ch+"", x, y);
x += 25;
//转回去
//g2d.rotate(-hudu, x, y);
}
//生成三条干扰线
g2d.setColor(Color.green);
int x1,y1,x2,y2;
for(int m=1;m<=3;m++) {
x1 = r.nextInt(width);
y1 = r.nextInt(height);
x2 = r.nextInt(width);
y2 = r.nextInt(height);
g2d.drawLine(x1, y1, x2, y2);
}
//把图片显示到页面上
ImageIO.write(bufferedImage, "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.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
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 CodeServlet extends HttpServlet {
/**
* 实现验证码的操作
第一步:生成图片
第二步:生成随机的数字和字母
第三步:把数字和字母画到图片上
第四步:把图片显示到页面上
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//生成图片
int width = 150;
int height = 50;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
//得到画笔
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
//生成四个随机的数字和字母
String words = "asdfghjklqwertyuiopzxcvbASDFGHJKLQWERTYUIOPZXCVB1234567890";
//创建Random对象
Random r = new Random();
int x = 25;
int y = 30;
//设置颜色
g2d.setColor(Color.YELLOW);
//设置字体的样式
g2d.setFont(new Font("宋体",Font.BOLD,25));
//rotate(double theta, double x, double y)
//弧度=角度*3.14/180
for(int i=1;i<=4;i++) {
int idx = r.nextInt(words.length());
//根据位置得到具体的字符
char ch = words.charAt(idx);
//旋转+- 30度
int jiaodu = r.nextInt(60)-30;
double hudu = jiaodu*Math.PI/180;
//旋转的效果
//g2d.rotate(hudu, x, y);
//把字符画到图片上
g2d.drawString(ch+"", x, y);
x += 25;
//转回去
//g2d.rotate(-hudu, x, y);
}
//生成三条干扰线
g2d.setColor(Color.green);
int x1,y1,x2,y2;
for(int m=1;m<=3;m++) {
x1 = r.nextInt(width);
y1 = r.nextInt(height);
x2 = r.nextInt(width);
y2 = r.nextInt(height);
g2d.drawLine(x1, y1, x2, y2);
}
//把图片显示到页面上
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
/**
*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
0 0
- 代码————刷新验证码
- js——点击验证码图片进行刷新验证码
- 代码片段——验证码
- 手机发送验证码—.net代码
- JS 即时刷新验证码图片代码
- asp验证码代码 (带刷新)
- asp带刷新的验证码代码
- ajax经典案例—无刷新验证用户名(GET)
- JAVA学习代码——验证码生生成步骤
- JAVA学习代码——短信验证码倒计时功能
- JAVA学习代码——短信验证码倒计时功能
- js代码-不刷新网页更换验证码
- recyclerview上下拉刷新加载——代码顺序
- php与Ajax(三)—无刷新验证用户名实例之分析
- jsp验证码刷新
- 局部刷新验证码
- 自动刷新验证码
- 局部刷新验证码
- jsonlib使用的依赖jar包
- 解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid
- 深度优先算法--之模拟迷宫
- servlet之response和request对象的引用
- NGUI-EventDelegate使用
- 代码————刷新验证码
- Android开源动画框架:NineOldAndroids
- linux批量修改密码、拷贝文件至远程机器
- 美国NOAA/AVHRR遥感数据
- spring-boot中使用springloaded实现热部署
- 用字符指针变量指向一个字符串,然后输出字符及其长度
- Java1.5语法糖的味道--增强for循环
- 欢迎使用CSDN-markdown编辑器
- char如何计算溢出值