• 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 = { "u5b8bu4f53", "u65b0u5b8bu4f53", "u9ed1u4f53", "u6977u4f53", "u96b6u4e66" };  
                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;  
        }  
        
    }
  • 相关阅读:
    NowCoder数列(矩阵快速幂变式)
    P1147 连续自然数和(思维题)
    P1029 最大公约数和最小公倍数问题(思维题)
    Diophantus of Alexandria
    欧拉筛法求素数( 洛谷)
    欧拉函数
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/shihaiming/p/7662356.html
Copyright © 2020-2023  润新知