当前位置: 移动技术网 > IT编程>开发语言>Java > springboot控制层图片验证码生成

springboot控制层图片验证码生成

2019年07月19日  | 移动技术网IT编程  | 我要评论

本次博客记录项目中一个图片验证码的实现,虽然不是很复杂,但好记性不如烂笔头,谨记!

package com.zl.util; 
 
import java.awt.color; 
import java.awt.font; 
import java.awt.graphics; 
import java.awt.image.bufferedimage; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.outputstream; 
import java.util.random; 
 
import javax.imageio.imageio; 
 
/** 
 * @author zzc 
 * @date 2017年11月6日 
 * @param 
 * @desc 图形验证码生成 
 * 
 */ 
public class verifyutil { 
 // 验证码字符集 
  private static final char[] chars = {  
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  
   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
   'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',  
   'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; 
  // 字符数量 
  private static final int size = 4; 
  // 干扰线数量 
  private static final int lines = 5; 
  // 宽度 
  private static final int width = 80; 
  // 高度 
  private static final int height = 40; 
  // 字体大小 
  private static final int font_size = 30; 
 
  /** 
   * 生成随机验证码及图片 
   * object[0]:验证码字符串; 
   * object[1]:验证码图片。 
   */ 
  public static object[] createimage() { 
   stringbuffer sb = new stringbuffer(); 
   // 1.创建空白图片 
   bufferedimage image = new bufferedimage( 
     width, height, bufferedimage.type_int_rgb); 
   // 2.获取图片画笔 
   graphics graphic = image.getgraphics(); 
   // 3.设置画笔颜色 
   graphic.setcolor(color.light_gray); 
   // 4.绘制矩形背景 
   graphic.fillrect(0, 0, width, height); 
   // 5.画随机字符 
   random ran = new random(); 
   for (int i = 0; i <size; i++) { 
    // 取随机字符索引 
    int n = ran.nextint(chars.length); 
    // 设置随机颜色 
    graphic.setcolor(getrandomcolor()); 
    // 设置字体大小 
    graphic.setfont(new font( 
      null, font.bold + font.italic, font_size)); 
    // 画字符 
    graphic.drawstring( 
      chars[n] + "", i * width / size, height*2/3); 
    // 记录字符 
    sb.append(chars[n]); 
   } 
   // 6.画干扰线 
   for (int i = 0; i < lines; i++) { 
    // 设置随机颜色 
    graphic.setcolor(getrandomcolor()); 
    // 随机画线 
    graphic.drawline(ran.nextint(width), ran.nextint(height), 
      ran.nextint(width), ran.nextint(height)); 
   } 
   // 7.返回验证码和图片 
   return new object[]{sb.tostring(), image}; 
  } 
 
  /** 
   * 随机取色 
   */ 
  public static color getrandomcolor() { 
   random ran = new random(); 
   color color = new color(ran.nextint(256),  
     ran.nextint(256), ran.nextint(256)); 
   return color; 
  } 
 
  public static void main(string[] args) throws ioexception { 
   object[] objs = createimage(); 
   bufferedimage image = (bufferedimage) objs[1]; 
   outputstream os = new fileoutputstream("d:/1.png"); 
   imageio.write(image, "png", os); 
   os.close(); 
  } 
} 

controller层

/** 
  * @author zzc 
  * @date 2017年11月6日 
  * @param 
  * @desc 图形验证码生成方法 
  * 
  */ 
 @requestmapping("/valicode") 
 public void valicode(httpservletresponse response,httpsession session) throws exception{ 
  //利用图片工具生成图片 
  //第一个参数是生成的验证码,第二个参数是生成的图片 
  object[] objs = verifyutil.createimage(); 
  //将验证码存入session 
  session.setattribute("imagecode",objs[0]); 
   
  //将图片输出给浏览器 
  bufferedimage image = (bufferedimage) objs[1]; 
  response.setcontenttype("image/png"); 
  outputstream os = response.getoutputstream(); 
  imageio.write(image, "png", os); 
 } 

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网