java画布生成验证码2.0与第三方代码生成
2017-08-31 22:25
363 查看
package com.zzh; 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; import cn.dsna.util.images.ValidateCode; public class ResponseDemo4 extends HttpServlet { /** * Constructor of the object. */ public ResponseDemo4() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 生成验证码 // writeValidateCode(response); // 生成第三方jar来实现 // 第一个第二个参数是宽高 // 第三个第四个参数是验证码位数和噪点数 ValidateCode code=new ValidateCode(120,25,4,8); code.write(response.getOutputStream()); //获取这个验证码对应的文本内容 String text=code.getCode(); } // 生成验证码的方法 使用java的画布 private void writeValidateCode(HttpServletResponse response) throws IOException { // 图片宽高 int width = 120; int height = 40; // 用java生成一个图片 // 前两个参数是宽高,第三个参数表示像素点的颜色,用int型组成rgb组成颜色有三位0~255,0~255,0~255 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 得到画布 Graphics graphics = img.getGraphics(); // 设置使用的颜色 java画布无论是边框还是内容的颜色都是一个颜色 不会分别设置 // 灰色 graphics.setColor(Color.gray); // 画一个灰色的背景外边留了一像素 graphics.fillRect(1, 1, width - 2, height - 2); // 画一个边框 graphics.setColor(Color.RED); graphics.drawRect(0, 0, width - 1, height - 1); // 绘制文字 // 设置绘制文字的字体 黑体 加粗|斜体 字体大小 graphics.setFont(new Font("黑体", Font.BOLD | Font.ITALIC, 30)); Random r = new Random(); // 往图片上画 int p = 15; for (int i = 0; i < 4; i++) { graphics.drawString(r.nextInt(10) + "", p, 30); p += 15; } // 往图片上画线也就是噪点 for (int i = 0; i < 10; i++) { graphics.setColor(new Color(r.nextInt(256), r.nextInt(256), r .nextInt(256))); graphics.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); } // 把图片交给浏览器 ImageIO.write(img, "jpg", response.getOutputStream()); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }
相关文章推荐
- JAVA代码生成验证码
- JAVA技术:代码实现验证码图片的生成
- JAVA学习代码——验证码生生成步骤
- java验证码生成代码
- java生成动态gif格式与png格式的验证码(代码2)
- java生成验证码代码
- java随机验证码生成实现实例代码
- Java生成验证码功能实例代码
- 生成验证码的java代码
- java生成动态gif格式与png格式的验证码(代码4)
- 用java代码 生成验证码的一个示例类
- java生成图片验证码实例代码
- java生成动态gif格式与png格式的验证码(代码1)
- java验证码生成具体代码
- 用java实现验证码的生成,以下代码是一个controller,可以直接使用!
- java生成动态gif格式与png格式的验证码(代码5)
- java随机动态生成汉字验证码图片的实例代码分享
- Java验证码图片生成代码
- java生成输出验证码图片,并进行验证的代码
- java随机动态生成汉字验证码图片的实例代码分享