JAVA產生問答式驗證碼圖片,支援加減演算法

來源:互聯網
上載者:User

標籤:cti   color   ssi   new   code   har   blog   nal   oid   

原文: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;      }      }

 

JAVA產生問答式驗證碼圖片,支援加減演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.