JAVA生成问答式验证码图片,支持加减算法

Posted 起个名字好难

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA生成问答式验证码图片,支持加减算法相关的知识,希望对你有一定的参考价值。

原文: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生成问答式验证码图片,支持加减算法的主要内容,如果未能解决你的问题,请参考以下文章

数字加减法验证码 识别 使用K-近邻算法(KNN)

JAVA生成验证码代码

java 登陆时的验证码怎么做?

利用PHP 简单实现加减法验证码

图片验证码文件,加减法计算方式

java实现一个验证码图片的功能