博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA生成问答式验证码图片,支持加减算法
阅读量:6261 次
发布时间:2019-06-22

本文共 4761 字,大约阅读时间需要 15 分钟。

原文:http://liuguihua0823.iteye.com/blog/1511355

 

import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.ParentPackage;import com.rd.p2p.additional.redisCaptcha.util.ResponseUtil;@ParentPackage("p2p-api")@InterceptorRefs({ @org.apache.struts2.convention.annotation.InterceptorRef("commonCheck") })public class TestAction extends AppBaseAction {                @Action("/app/test2")    public void test() throws IOException{        try {              int width = 140, height = 37;              String baseStr = generateCheckCode(request);                    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);              Graphics g = image.getGraphics();                    Random random = new Random();                    g.setColor(getRandColor(random, 200, 250));              g.fillRect(0, 0, width, height);                    String[] fontTypes = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66" };              int fontTypesLength = fontTypes.length;                    g.setColor(getRandColor(random, 160, 200));              g.setFont(new Font("Times New Roman", Font.PLAIN, 14 + random.nextInt(6)));                            for (int i = 0; i < 255; i++) {                  int x = random.nextInt(width);                  int y = random.nextInt(height);                  int xl = random.nextInt(12);                  int yl = random.nextInt(12);                  g.drawLine(x, y, x + xl, y + yl);              }                            String [] baseChar = baseStr.split(" ");              for (int i = 0; i < baseChar.length; i++) {                  g.setColor(getRandColor(random, 30, 150));                  g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)], Font.BOLD, 22 + random.nextInt(6)));                  g.drawString(baseChar[i], 24 * i + 10, 24);              }                            g.dispose();                    //发送图片            ResponseUtil.sendImg(response, image, "image/jpeg", "code", "jpg");        } catch (IllegalStateException e) {              System.out.println(e.getMessage());              e.printStackTrace();          }              }        private static Color getRandColor(Random random, int fc, int bc){          if (fc > 255)              fc = 255;          if (bc > 255)              bc = 255;          int r = fc + random.nextInt(bc - fc);          int g = fc + random.nextInt(bc - fc);          int b = fc + random.nextInt(bc - fc);          return new Color(r, g, b);      }        private static String generateCheckCode(HttpServletRequest request) {          Random random = new Random();          int intTemp;          int intFirst = random.nextInt(100);          int intSec = random.nextInt(100);          String checkCode = "";          int result = 0;          switch (random.nextInt(6)) {              case 0:                  if (intFirst < intSec) {                      intTemp = intFirst;                      intFirst = intSec;                      intSec = intTemp;                  }                  checkCode = intFirst + " - " + intSec + " = ?";                  result = intFirst-intSec;                  break;              case 1:                  if (intFirst < intSec) {                      intTemp = intFirst;                      intFirst = intSec;                      intSec = intTemp;                  }                  checkCode = intFirst + " - ? = "+(intFirst-intSec);                  result = intSec;                  break;              case 2:                  if (intFirst < intSec) {                      intTemp = intFirst;                      intFirst = intSec;                      intSec = intTemp;                  }                  checkCode = "? - "+intSec+" = "+(intFirst-intSec);                  result = intFirst;                  break;              case 3:                  checkCode = intFirst + " + " + intSec + " = ?";                  result = intFirst + intSec;                  break;              case 4:                  checkCode = intFirst + " + ? ="+(intFirst+intSec);                  result = intSec;                  break;              case 5:                  checkCode = "? + " + intSec + " ="+(intFirst+intSec);                  result = intFirst;                  break;          }          System.out.println("result=" + result);        request.getSession().setAttribute("VERIFY_CODE", result);          return checkCode;      }      }

 

转载地址:http://dvzpa.baihongyu.com/

你可能感兴趣的文章
swift 消息监听和键值监听(kvo)
查看>>
02@在类的头文件中尽量少引入其他头文件
查看>>
Spring定时任务高级使用篇
查看>>
阿里资深技术专家总结:要怎样努力才可以成为公司主力架构师
查看>>
数学推导+Python实现机器学习算法:线性回归
查看>>
Android AccessibilityService机制源码解析
查看>>
Android基础知识
查看>>
寻找数组主元素(Majority Element))
查看>>
如何将ST05生成的trace导入HANA Studio里并以图形化方式显示出来
查看>>
TiDB 在西山居实时舆情监控系统中的应用
查看>>
说一说 JVM 对锁的优化
查看>>
图像处理库GPUImage简单使用
查看>>
基于Java语言构建区块链(五)—— 地址(钱包)
查看>>
Elastic Search 安装和配置
查看>>
手动实现 express
查看>>
Let's Encrypt免费ssl证书安装使用详解
查看>>
有个功能丰富的 react 脚手架,了解下?
查看>>
SnippetsLab - 像纳博科夫写小说一样写代码
查看>>
React-Redux 源码解析 二(middleware)
查看>>
JLRoutes 实现原理分析
查看>>