代码改变世界

验证码,java

2016-05-23 00:31  我的梦想那么大  阅读(323)  评论(0编辑  收藏  举报

这几天打算写一个验证码出来

遇到了几个问题

imageio写入失败:原因我创建文件的时候是先建立一个text文本,然后修改后缀,图片写不进去,还有没有编译

图像扭曲:粘连的问题,目前解决图像扭曲的问题,粘连没有找到算法

抗锯齿:g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //抗锯齿设置,对图像扭曲的时候还要再次设置抗锯齿不然没用

把各种形式的验证码分开来:例如纯数字,数字英文混合等等,消除o0,l1等模糊字符

git要发布至github是选中项目push上去,commit只会提交到本地

代码目前还有问题。。。。。。。。。生成的字符数会少一个,被遮挡住了,字体是强制设置

效果图:

package com.validate.tool;


/**
 * @author 
 * @version 1.0
 * */
public class CodeType {
    public static final int CODE_TYPE_NUM = 1;//验证码为纯数字
    public static final int CODE_TYPE_B_LETTER = 2;//验证码为纯大写字母
    public static final int CODE_TYPE_S_LETTER = 3;//验证码为纯小写字母
    public static final int CODE_TYPE_B_N_LETTER_N = 4;//验证码为大写字母与数字混合,不开启模糊
    public static final int CODE_TYPE_B_N_LETTER_V = 5;//验证码为大写字母与数字混合,开启模糊
    public static final int CODE_TYPE_S_N_LETTER_N = 6;//验证码为小写字母与数字混合,不开启模糊
    public static final int CODE_TYPE_S_N_LETTER_V = 7;//验证码为小写字母与数字混合,开启模糊
    public static final int CODE_TYPE_B_S_N_LETTER_N = 8;//验证码为大小写字母数字混合,不开启模糊
    public static final int CODE_TYPE_B_S_N_LETTER_V = 9;//验证码为大小写字母数字混合,开启模糊
    
    
}
package com.validate.tool;

import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.Serializable;

public class ImgInfo implements Serializable{
    /**
     * @author 
     * @version 1.0
     * */

    private Integer codeNum;//验证码数量
    private Integer codeType;//验证码类型:纯数字、字母等
    private Integer lineNum;//干扰线
    private String font;//字体
    private Integer imgType;//图片类型
    private boolean openSingleCase;//是否需要单例
    
    private BufferedImage bufferedImage;//返回的图片缓存
    private int fontSize;//图片文字大小
    private String code;

    public Integer getCodeNum() {
        return codeNum;
    }
    public void setCodeNum(Integer codeNum) {
        this.codeNum = codeNum;
    }
    public Integer getLineNum() {
        return lineNum;
    }
    public void setLineNum(Integer lineNum) {
        this.lineNum = lineNum;
    }
    
    
    
    public boolean isOpenSingleCase() {
        return openSingleCase;
    }
    public void setOpenSingleCase(boolean openSingleCase) {
        this.openSingleCase = openSingleCase;
    }
    public Integer getImgType() {
        return imgType;
    }
    public void setImgType(Integer imgType) {
        this.imgType = imgType;
    }
    public Integer getCodeType() {
        return codeType;
    }
    public void setCodeType(Integer codeType) {
        this.codeType = codeType;
    }
    public String getFont() {
        return font;
    }
    public void setFont(String font) {
        this.font = font;
    }
    
    
    public BufferedImage getBufferedImage() {
        return bufferedImage;
    }
    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }
    
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    
    public int getFontSize() {
        return fontSize;
    }
    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
    
    
    
    
    
}
package com.validate.tool;

/**
 * @author 
 * @version 1.0
 * 获取各种随机数
 * 
 * */
public class Random implements RandomBase{

    private static final String[] B_S_N_LETTER_V = { "1", "3", "4", "5", "6", "7", "8",//开启后的字符串,大小写混合
            "9", "a", "b", "c", "d", "e", "f", "i", "j", "k", "m", "n", "p",
            "q", "r", "s", "t", "u", "v", "w", "x", "y", "A", "B", "C", "D",
            "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y" };
    
    private static final String[] B_S_N_LETTER_N = {"1", "3", "4", "5", "6", "7", "8",//不开启的字符串,大小写混合
        "9","0", "a", "b", "c", "d", "e", "f","g","h", "i", "j", "k", "m", "n","o", "p",
        "q", "r", "s", "t", "u", "v", "w", "x", "y","z", "A", "B", "C", "D",
        "E", "F", "G", "H", "J", "K", "M", "N", "P", "O","Q", "R", "S", "T",
        "U", "V", "W", "X", "Y","Z"};
    
    private static final Integer[] NUM = {1,2,3,4,5,6,7,8,9,0};//纯数字的验证码
    
    private static final String[] S_LETTER = {//小写字母
        "a", "b", "c", "d", "e", "f","g","h", "i", "j", "k", "m", "n","o", "p",
        "q", "r", "s", "t", "u", "v", "w", "x", "y","z"};
    
    private static final String[] B_LETTER = {"A", "B", "C", "D",//大写字母
        "E", "F", "G", "H", "J", "K", "M", "N", "P", "O","Q", "R", "S", "T",
        "U", "V", "W", "X", "Y","Z"};
    
    private static final String[] B_N_LETTER_V = {//大写字母与数字混合,开启模糊
        "1", "3", "4", "5", "6", "7", "8","9","A", "B", "C", "D",
        "E", "F", "G", "H", "J", "K", "M", "N", "P", "O","Q", "R", "S", "T",
        "U", "V", "W", "X", "Y"
    };
    
    private static final String[] B_N_LETTER_N = {//大写字母与数字混合,关闭模糊
        "1", "3", "4", "5", "6", "7", "8","9","0","A", "B", "C", "D",
        "E", "F", "G", "H","I", "J", "K", "M", "N", "P", "O","Q", "R", "S", "T",
        "U", "V", "W", "X", "Y","Z"
    };
    
    private static final String[] S_N_LETTER_V = {//小写字母与数字混合,开启模糊
        "1", "3", "4", "5", "6", "7", "8","9","0",
         "a", "b", "c", "d", "e", "f", "i", "j", "k", "m", "n", "p","q", "r", "s", "t", "u", "v", "w", "x", "y"
    };
    
    private static final String[] S_N_LETTER_N = {//小写字母与数字混合,关闭模糊
        "1", "3", "4", "5", "6", "7", "8","9","0",
        "a", "b", "c", "d", "e", "f","g","h", "i", "j", "k", "m", "n","o", "p","q", "r", "s", "t", "u", "v", "w", "x", "y","z"
    };
    
    
    private static volatile Random random = null;//是否使用单例模式
    

//    public  Random()//默认单例模式打开
//    {
//        if(random == null)
//        {
//            synchronized (Random.class) {
//                if(random == null)
//                {
//                    random = new Random();//内存溢出
//                }
//            }
//        }
//    }
//    
//    public Random(boolean notOpenSingleCase)//是否打开单例模式,true打开单例模式,false不使用单例模式
//    {
//        if(notOpenSingleCase == false)//不开单例模式
//        {
//            
//        }
//    }
    
    
    
    public static Random getInstance(boolean openSingleCase)//是否开启单例模式,是否开启字母字符,0o区分默认开启
    {
        if(openSingleCase == true)//开启单例模式
        {
            if(random == null)
            {
                synchronized (Random.class) {
                    random = new Random();
                }
            }
        }else//不开启单例模式
        {
            return new Random();
        }
        
        return random;
    }

    public String get(Integer length,int codeType) {
        StringBuffer stringBuffer =  new StringBuffer();
        
        switch (codeType) {
        case CodeType.CODE_TYPE_NUM://纯数字
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(NUM.length);
                stringBuffer.append(NUM[n]);
            }
            break;
        case CodeType.CODE_TYPE_B_LETTER://纯大写字母
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(B_LETTER.length);
                stringBuffer.append(B_LETTER[n]);
            }
            break;
        case CodeType.CODE_TYPE_S_LETTER://纯小写字母
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(S_LETTER.length);
                stringBuffer.append(S_LETTER[n]);
            }
            break;
        case CodeType.CODE_TYPE_B_N_LETTER_N://大写字母与数字混合,不开启模糊
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(B_N_LETTER_N.length);
                stringBuffer.append(B_N_LETTER_N[n]);
            }
            break;
        case CodeType.CODE_TYPE_B_N_LETTER_V://大写字母与数字混合,开启模糊
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(B_N_LETTER_V.length);
                stringBuffer.append(B_N_LETTER_V[n]);
            }
            break;
        case CodeType.CODE_TYPE_S_N_LETTER_N://小写字母与数字混合,不开启模糊
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(S_N_LETTER_N.length);
                stringBuffer.append(S_N_LETTER_N[n]);
            }
            break;
        case CodeType.CODE_TYPE_S_N_LETTER_V://小写字母与数字混合,开启模糊
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(S_N_LETTER_V.length);
                stringBuffer.append(S_N_LETTER_V[n]);
            }
            break;
        case CodeType.CODE_TYPE_B_S_N_LETTER_N://小写字母与数字混合,开启模糊
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(B_S_N_LETTER_N.length);
                stringBuffer.append(B_S_N_LETTER_N[n]);
            }
            break;
        case CodeType.CODE_TYPE_B_S_N_LETTER_V://小写字母与数字混合,开启模糊
            for(int i = 1; i <= length; i++) {
                java.util.Random random = new java.util.Random();
                int n = random.nextInt(B_S_N_LETTER_V.length);
                stringBuffer.append(B_S_N_LETTER_V[n]);
            }
            break;
        default:
            break;
        }
        
        
//        if(vague == true)//开启 
//        {
//            for (int i = 1; i <= length; i++) {
//                java.util.Random random = new java.util.Random();
//                int n = random.nextInt(50);
//                stringBuffer.append(rBase[n]);
//            }
//        }else
//        {
//            for (int i = 1; i <= length; i++) {
//                java.util.Random random = new java.util.Random();
//                int n = random.nextInt(58);
//                stringBuffer.append(rBaseVague[n]);
//            }
//        }
        
        return stringBuffer.toString();

    }

    
    
}
package com.validate.tool;

/**
 * @author 
 * @version 1.0
 * */
public interface RandomBase {

    public String get(Integer length ,int codeType);
}
package com.validate.tool;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class RandomImg{

    private static volatile RandomImg randomImg  = null;
    
    private RandomImg(){}
    
    public RandomImg getInstance(boolean openSingleCase)
    {
        if(openSingleCase == true)//开启单例模式
        {
            if(randomImg == null)
            {
                synchronized (RandomImg.class) {
                    if(randomImg == null)
                    {
                        randomImg = new RandomImg();
                    }
                }
            }
        }else//不开启单例模式
        {
            return new RandomImg();
        }
        return randomImg;
    } 
    
    public ImgInfo getImg(ImgInfo imgInfo)throws Exception
    {
        
        
        int x = 0,codeY=0;  
        int red = 0, green = 0, blue = 0;  
          
        x = imgInfo.getFontSize();//每个字符的宽度  
          
        codeY = imgInfo.getFontSize(); //y轴的维度为字体大小
        int imgWidth = imgInfo.getFontSize()*imgInfo.getCodeNum()+20;//图片宽度
        int imgHeight = imgInfo.getFontSize();//图片高度
        
        BufferedImage buffereedImage = new BufferedImage(imgWidth, imgHeight, imgInfo.getImgType());
        
        
        Random random = new Random().getInstance(imgInfo.isOpenSingleCase());
        
        String code = random.get(imgInfo.getCodeNum(),imgInfo.getCodeType());
        
        imgInfo.setCode(code);//得到随机字符串
        
        
        Graphics2D graphics2D = buffereedImage.createGraphics();
        
        graphics2D.setColor(Color.WHITE);//画布沾取白色
        
        graphics2D.fillRect(0, 0, imgWidth, imgHeight);//填充矩形
        
        
        Font font = new Font("Arial", imgInfo.getFontSize(), imgInfo.getFontSize());
        
        graphics2D.setFont(font);
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //抗锯齿设置
        
        java.util.Random randomBase = new java.util.Random(); 
        
        for (int i = 0; i < imgInfo.getLineNum(); i++)//产生干扰线
        {  
            int xs = randomBase.nextInt(imgWidth);  
            int ys = randomBase.nextInt(imgHeight);  
            int xe = xs+randomBase.nextInt(imgWidth/8);  
            int ye = ys+randomBase.nextInt(imgHeight/8);  
            red = randomBase.nextInt(255);  
            green = randomBase.nextInt(255);  
            blue = randomBase.nextInt(255);  
            graphics2D.setColor(new Color(red, green, blue));  
            graphics2D.drawLine(xs, ys, xe, ye);
            
        }
        
        for (int i = 0; i < imgInfo.getCodeNum(); i++) {//写入文字
            String strRand = String.valueOf(code.charAt(i));  
            // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。  
            red = randomBase.nextInt(255);  
            green = randomBase.nextInt(255);  
            blue = randomBase.nextInt(255);  
            graphics2D.setColor(new Color(red, green, blue));  
           // graphics2D.drawString(strRand, (i + 1) * x-(int)(imgInfo.getFontSize()/1.2), codeY);
            graphics2D.drawString(strRand, i>0?i * x+10:i*x-imgInfo.getFontSize()/*文字x轴文字*/, codeY-imgInfo.getFontSize()/8/*文字y轴文字*/);  
            

        } 
        shearGraphics(graphics2D,imgWidth , imgHeight, Color.WHITE);
        
        imgInfo.setBufferedImage(buffereedImage);
        return imgInfo;
        
    }
    
    
    public Graphics2D shearGraphics(Graphics2D graphics2D , int width , int height , Color color)
    {
        Graphics2D g = graphics2D;
        shearX(g , width , height , color);
        shearY(g,width , height,color);
        return g;
    }
    
    public void shearX(Graphics2D g,int w1,int h1,Color color)
    { 
        java.util.Random randomBase = new java.util.Random();
        int period = randomBase.nextInt(2);
        boolean borderGap = true;
        int frames = 1;
        int phase = randomBase.nextInt(2);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //抗锯齿设置
        for(int i =0 ;i<h1;i++)
        {
            double d = (double)(period >> 1)*Math.sin((double)i/(double)period+(6.28318*(double)phase)/(double)frames);
            g.copyArea(0, i, w1, 1, (int)d, 0);
            if(borderGap){
                g.setColor(color);
                g.drawLine((int)d, i, 0, i);
                g.drawLine((int)d+w1, i, w1, i);
            }
        }
    }
    
    public void shearY(Graphics2D g , int w1 , int h1,  Color color)
    {
        java.util.Random randomBase = new java.util.Random();
        int period = randomBase.nextInt(40)+10;
        boolean borderGap = true;
        int frames = 20;
        int phase = 7;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //抗锯齿设置
        for(int i = 0;i< w1;i++){
            double d = (double)(period>>1)*Math.sin((double)i/(double)period+(6.2831*(double)phase)/(double)frames);
            g.copyArea(i, 0, 1, h1, 0, (int)d);
            if(borderGap){
                g.setColor(color);
                g.drawLine(i, (int)d, i, 0);
                g.drawLine(i, (int)d+h1, i, h1);
            }
        }
    }
    
    
    public static void main(String[] args) throws Exception {
        RandomImg randomImg = new RandomImg().getInstance(true);
        
        ImgInfo imginfo = new ImgInfo();
        
        imginfo.setCodeNum(4);
        imginfo.setCodeType(CodeType.CODE_TYPE_B_LETTER);
        imginfo.setFont("");
        imginfo.setFontSize(90);
        
        imginfo.setImgType(BufferedImage.TYPE_INT_RGB);
        imginfo.setLineNum(23);
        imginfo.setOpenSingleCase(true);
        
         
        
        
        randomImg.getImg(imginfo);
        
        File f = new File("E:/e.png");
    
        ImageIO.write(imginfo.getBufferedImage(),"png", f);
        
        
    }

}

迷茫了,大三。。。。现在开始放弃web前段,专心搞java,ssh,ssm,pl/sql、nosql,hadoop,其他的都不搞了。