| package com.xzx.gc.role.controller;  | 
|   | 
| import com.google.zxing.BarcodeFormat;  | 
| import com.google.zxing.MultiFormatWriter;  | 
| import com.google.zxing.WriterException;  | 
| import com.google.zxing.common.BitMatrix;  | 
| import com.xzx.gc.common.annotations.PassToken;  | 
| import com.xzx.gc.common.constant.Constants;  | 
| import com.xzx.gc.common.request.BaseController;  | 
| import com.xzx.gc.common.utils.RedisUtil;  | 
| import com.xzx.gc.util.DoubleUtil;  | 
| import io.swagger.annotations.Api;  | 
| import lombok.extern.slf4j.Slf4j;  | 
| import org.apache.commons.lang3.StringUtils;  | 
| import org.slf4j.Logger;  | 
| import org.slf4j.LoggerFactory;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.web.bind.annotation.RequestMapping;  | 
| import org.springframework.web.bind.annotation.RestController;  | 
|   | 
| import javax.imageio.ImageIO;  | 
| import javax.servlet.http.HttpServletRequest;  | 
| import javax.servlet.http.HttpServletResponse;  | 
| import javax.servlet.http.HttpSession;  | 
| import java.awt.*;  | 
| import java.awt.image.BufferedImage;  | 
| import java.io.IOException;  | 
| import java.util.HashMap;  | 
| import java.util.Map;  | 
| import java.util.Random;  | 
|   | 
| import static com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage;  | 
|   | 
| @RestController  | 
| @Api(tags = "登陆和用户管理的接口(验证码)")  | 
| @Slf4j  | 
| public class VerifyController extends BaseController {  | 
|   | 
|   | 
|     @RequestMapping(Constants.ADMIN_VIEW_PREFIX+"/wxImgApi")  | 
|     @PassToken  | 
|     public void wxImgApi(HttpServletRequest req, HttpServletResponse resp) throws IOException {  | 
|         String content = req.getParameter("content");  | 
|   | 
|         if(StringUtils.isBlank(content)){  | 
|             return;  | 
|   | 
|         }  | 
|         MultiFormatWriter multiFormatWriter = new MultiFormatWriter();  | 
|         Map hints = new HashMap();  | 
|         BitMatrix bitMatrix = null;  | 
|         try {  | 
|             bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);  | 
|             BufferedImage image = toBufferedImage(bitMatrix);  | 
|             //输出二维码图片流  | 
|             try {  | 
|                 ImageIO.write(image, "png", resp.getOutputStream());  | 
|             } catch (IOException e) {  | 
|                 e.printStackTrace();  | 
|             }  | 
|         } catch (WriterException e1) {  | 
|             e1.printStackTrace();  | 
|         }  | 
|     }  | 
|   | 
|     @RequestMapping(Constants.ADMIN_VIEW_PREFIX +"/verifyApi")  | 
|     @PassToken  | 
|     public void verifyApi(HttpServletRequest req, HttpServletResponse resp) throws IOException {  | 
|   | 
|         // 创建图片  | 
|         int width = 80;  | 
|         int height = 40;  | 
|         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  | 
|   | 
| //        创建图层获得画板  | 
|         Graphics g = image.getGraphics();  | 
| //        确认画笔颜色  | 
|         g.setColor(Color.BLACK);  | 
|         //填充矩形  | 
|         g.fillRect(0, 0, width - 2, height - 2);  | 
|         // String dataString="ABCDEFGHIJHLMNOPQRSTUVWXYZabcdefghijklmnopqlstuvwxyz1234567890";  | 
|         String dataString = "1234567890";  | 
|         //设置字体  | 
|         g.setFont(new Font("宋体", Font.BOLD, 30));  | 
|         //缓存随机生成的字符  | 
|         StringBuffer buf = new StringBuffer();  | 
|         Random random = new Random();  | 
|         String password= DoubleUtil.getLowerLetterNumber(4);  | 
|   | 
|         //截取字符  | 
|         int index = 1;  | 
|         for (int i = 0; i < 4; i++) {  | 
|             //设置字体颜色  随机  | 
|             g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));  | 
|             //获得一个随机字符  | 
|             //int index = random.nextInt(10);  | 
|             String str = password.substring(i, index);  | 
|             //String str = dataString.substring(index, index + 1);  | 
|   | 
|             //加入画板  | 
|             g.drawString(str, 20 * i, 30);  | 
|             buf.append(str);  | 
|             index++;  | 
|         }  | 
|         //干扰线  | 
|         for (int i = 0; i < 10; i++) {  | 
|             g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));  | 
|             g.setColor(new Color(16, 16, 16));  | 
|             g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));  | 
|         }  | 
|         HttpSession session = req.getSession();  | 
|         log.debug("获取的验证码是:  {}", buf.toString());  | 
|         session.setAttribute("cap", buf.toString());  | 
|         //设置响应类型  | 
|         resp.setContentType("image/jpeg");  | 
|         //将图片发送给浏览器  | 
|         ImageIO.write(image, "jpg", resp.getOutputStream());  | 
|     }  | 
| }  |