java后台生成图片二维码

  controller:

 1                                     
 2 /**                                        
 3  * 获取登录的验证码                                        
 4  * @param request                                        
 5  * @param response                                        
 6  */                                        
 7 public void getLoginCode(HttpSession session,HttpServletRequest request,                                        
 8     HttpServletResponse response) throws Exception{                                    
 9     // 获取验证码以及图片                                        
10     Map<String,Object> map = CodeUtil.generateCodeAndPic();                                        
11     // 将验证码存入Session                                        
12     session.setAttribute("imageCode",map.get("code"));                                        
13     // 获取图片                                        
14     BufferedImage image = (BufferedImage) map.get("codePic");                                        
15     // 转成base64                                        
16     BASE64Encoder encoder = new BASE64Encoder();                                        
17     ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流                                        
18     ImageIO.write(image, "png", baos);//写入流中                                        
19     byte[] bytes = baos.toByteArray();//转换成字节                                        
20     String png_base64 =  encoder.encodeBuffer(bytes).trim();//转换成base64串                                        
21     //删除 
                                        
22     png_base64 = png_base64.replaceAll("
", "").replaceAll("
", "");                                        
23     Map<String,String> maps = new HashMap<>();                                        
24     maps.put("code","" + map.get("code"));                                        
25     maps.put("address",png_base64);                                        
26     JSONObject jsonObject = JSONObject.fromObject(maps);                                        
27     PrintWriter writer = response.getWriter();                                        
28     writer.print(jsonObject);                                        
29     writer.flush();                                        
30     writer.close();                                        
31 }                                        

private static int width = 180;    
private static int height = 40;    
private static int codeCount = 4;    
private static int xx = 30;    
private static int fontHeight = 36;    
private static int codeY = 32;    
private static char[] codeSequence = {    
        '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'    
};    
    
/**    
 * 生成验证码图片    
 * @return    
 */    
public static Map<String,Object> generateCodeAndPic(){    
    //定义图像buffer    
    BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);    
    Graphics graphics = bufferedImage.getGraphics();    
    //创建一个随机数类    
    Random random = new Random();    
    // 将图像填充为白色    
    graphics.setColor(Color.WHITE);    
    // 填充图像    
    graphics.fillRect(0,0,width,height);    
    // 创建字体,字体的大小应该根据图片的高度来定    
    Font font = new Font("Fixedsys", Font.BOLD, fontHeight);    
    // 设置字体    
    graphics.setFont(font);    
    // 画边框    
    graphics.setColor(Color.black);    
    graphics.drawRect(0,0,width-1,height-1);    
    // 随机产生30条干扰线,让图形码不便于被其他程序检测到    
    graphics.setColor(Color.black);    
    for (int i=0;i<30;i++){    
        int x = random.nextInt(width);    
        int y = random.nextInt(height);    
        int x1 = random.nextInt(12);    
        int y1 = random.nextInt(12);    
        graphics.drawLine(x,y,x + x1,y + y1);    
    }    
    // randomCode用于保存随机生成的二维码,便于用户登录时验证    
    StringBuffer randomCode = new StringBuffer();    
    int red = 0, green=0,blue = 0;    
    
    // 随机产生codeCount数字的验证码    
    for (int i =0;i<codeCount;i++){    
        // 得到随机的验证码数字    
        String code = String.valueOf(codeSequence[random.nextInt(36)]);    
        // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色都不一样    
        red = random.nextInt(255);    
        blue = random.nextInt(255);    
        green = random.nextInt(255);    
    
        // 用随机产生的颜色将验证码放到图像中    
        graphics.setColor(new Color(red,green,blue));    
        graphics.drawString(code,(i + 1) * xx,codeY);    
    
        // 将产生的四个随机数组合在一起    
        randomCode.append(code);    
    }    
    
    Map<String,Object> map = new HashMap<>();    
    // 存放验证码    
    map.put("code",randomCode);    
    // 存放生成的验证码BufferedImage对象    
    map.put("codePic",bufferedImage);    
    return map;    
}    
原文地址:https://www.cnblogs.com/txppp/p/10538075.html