利用java画图向前端输出验证码图片
2017-11-08 17:40
309 查看
/**
* 适用于后台验证码图片的输出
* @param width
* @param height
* @param len
* @param response
* @return code验证码
* @throws IOException
*/
public String randomCodeImage(int width,int height,int len, HttpServletResponse response) throws IOException {
String code=null;
//生成随即验证码
code=checkCode(len);
//开始画图
//设置画布大小
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//加入画笔
Graphics g=image.getGraphics();
//设置画笔颜色
g.setColor(new Color(255,255,255));
//用笔给画布设置背景颜色
g.fillRect(0, 0, width, height);
//换笔颜色,等下写字,,随机颜色
Random r=new Random();
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
//设置字体
g.setFont(new Font(null, Font.ITALIC,(int)(height*0.82)));
//写入验证码
g.drawString(code,(int)(width*(1.0/6)), (int)(height*0.85));
//加干扰线
for (int i = 0; i < 3+r.nextInt(8); i++) {
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.drawLine(r.nextInt(80), r.nextInt(30), r.nextInt(80), r.nextInt(30));
}
response.setContentType("image/jpeg");
OutputStream os=response.getOutputStream();
//压缩并且输出
javax.imageio.ImageIO.write(image, "jpeg", os);
os.close();
return code;
}
//生成随即字符串,如果要生成计算体就要改并这段代码
/**
*@param len字符串长度
*
*/
private String checkCode(int len) {
String code=null;
char[] codeAll= {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M',
'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m',
'1','2','3','4','5','6','7','8','9','0'};
StringBuffer str=new StringBuffer();
Random r=new Random();
for (int i = 0; i <len; i++) {
str.append(codeAll[r.nextInt(codeAll.length)]);
}
code=str.toString();
return code;
}
* 适用于后台验证码图片的输出
* @param width
* @param height
* @param len
* @param response
* @return code验证码
* @throws IOException
*/
public String randomCodeImage(int width,int height,int len, HttpServletResponse response) throws IOException {
String code=null;
//生成随即验证码
code=checkCode(len);
//开始画图
//设置画布大小
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//加入画笔
Graphics g=image.getGraphics();
//设置画笔颜色
g.setColor(new Color(255,255,255));
//用笔给画布设置背景颜色
g.fillRect(0, 0, width, height);
//换笔颜色,等下写字,,随机颜色
Random r=new Random();
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
//设置字体
g.setFont(new Font(null, Font.ITALIC,(int)(height*0.82)));
//写入验证码
g.drawString(code,(int)(width*(1.0/6)), (int)(height*0.85));
//加干扰线
for (int i = 0; i < 3+r.nextInt(8); i++) {
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.drawLine(r.nextInt(80), r.nextInt(30), r.nextInt(80), r.nextInt(30));
}
response.setContentType("image/jpeg");
OutputStream os=response.getOutputStream();
//压缩并且输出
javax.imageio.ImageIO.write(image, "jpeg", os);
os.close();
return code;
}
//生成随即字符串,如果要生成计算体就要改并这段代码
/**
*@param len字符串长度
*
*/
private String checkCode(int len) {
String code=null;
char[] codeAll= {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M',
'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m',
'1','2','3','4','5','6','7','8','9','0'};
StringBuffer str=new StringBuffer();
Random r=new Random();
for (int i = 0; i <len; i++) {
str.append(codeAll[r.nextInt(codeAll.length)]);
}
code=str.toString();
return code;
}
相关文章推荐
- java生成输出验证码图片,并进行验证的代码
- JavaWeb 输出随机验证码图片
- 如何把java生成打的验证码生成图片,并输出到文件夹
- PHP利用GD库画图和生成验证码图片
- java文字转换成图片输出
- java画图输出到磁盘
- java输出扭曲验证码
- Java生成随机验证码图片
- 300.31输出随机验证码图片
- Javaweb开发中通过Servlet生成验证码图片
- 利用Java+POI 读写Excel文档&向Excel中插入图片
- java生成图片验证码实例代码
- 验证码图片生成工具类——Captcha.java
- 验证码图片生成工具类——Captcha.java
- java 缩放图片并输出,支持 jpg 和 png 输入及同类型输出
- Java 获取前端页面代码段中img标签的src属性值,即获取图片Url
- <Java>利用for循环在控制台输出99乘法表的探索
- 输出随机验证码图片
- java图形界面基础之画图(使用Graphics类从画图形到图片到gif)
- JAVA 实现的 生成验证码图片 工具类