您的位置:首页 > 编程语言 > Java开发

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
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: