JAVA生成一次性圖片驗證碼


現在很多地方都需要寫驗證碼登錄驗證,這樣的好處是可以減輕服務器的壓力等,下面就用java實現一次性登錄驗證碼的書寫。

1.驗證碼生成類:

package com.easyteam;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class ImageCode {
     int w=70;//寬度
     int h=35;//高度
     Random r=new Random();
    
    public  BufferedImage CreateImage(){//生成圖片的方法
        BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);//內存中創建一張圖片
        Graphics gps=img.getGraphics();//獲取當前圖片的畫筆
        
        gps.setColor(new Color(240,240,240));//設置畫筆
        
        gps.fillRect(0, 0, w, h);//填充一個與圖片一樣大小的矩形(其實就是為了設置背景顏色)
        return img;
        
        
    }
    public   BufferedImage getImage(){//得到圖片的方法
        BufferedImage img=CreateImage();
        Graphics gps=img.getGraphics();//獲取當前圖片的畫筆
        //開始畫東西
        for(int i=0;i<4;i++){
            String ch=this.getContent();
            gps.setColor(this.getColor());
            gps.setFont(this.getFont());
            gps.drawString(ch, w/4*i, h-5);//寬度讓其不滿圖片
        }
        drawLine(img);
        return img;
        
    }
    public  void saveImage(BufferedImage img,OutputStream out) throws  IOException {
        //這里為了測試將生成的圖片放入f盤,在實際的項目開發中是需要將生成的圖片寫入客戶端的:ImageIO.write(img,"JPEG",response.getOutputStream());
        
        ImageIO.write(img, "JPEG", new FileOutputStream("F:\\a.jpg"));
        
    }
    //在圖片中插入字母和十個數字
    String str="abcdefghijklmnupqrstuvwxyzABCDEFGHIJKLMNUPQRSTUVWZYZ1234567890";
    public   String getContent(){
        int index=r.nextInt(str.length());
        return str.charAt(index)+"";
    }
     String[] font={"宋體","華文楷體","華文隸書","黑體","華文新魏"};//字體
     int[] fontSize={24,25,26,27,28};//字號大小
     int[] fontStyle={0,1,2,3};//字體樣式
    public   Font getFont(){
        int index1=r.nextInt(font.length);
        String name=font[index1];
        int style=r.nextInt(4);
        int index2=r.nextInt(fontSize.length);
        int size=fontSize[index2];
        return new Font(name,style,size);
    }
    public   Color getColor(){//得到不同的顏色
        int R=r.nextInt(256);//取值范圍是0-255
        int G=r.nextInt(256);
        int B=r.nextInt(256);
        return new Color(R,G,B);
    }
    public void drawLine(BufferedImage img){//畫干擾線
        Graphics2D gs=(Graphics2D) img.getGraphics();
        gs.setColor(Color.BLACK);
        gs.setStroke(new BasicStroke(1.5F));//設置線的寬度
        for(int i=0;i<5;i++){//橫坐標不能超過寬度,縱坐標不能超過高度
            int x1=r.nextInt(w);
            int y1=r.nextInt(h);
            int x2=r.nextInt(w);
            int y2=r.nextInt(h);
            gs.drawLine(x1, y1, x2, y2);
                    
        }
    }
    
}

2.測試類

package com.easyteam;

import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

    
    public static void main(String[] args) throws  IOException {
        ImageCode imagecode=new ImageCode();
        BufferedImage img=imagecode.getImage();
        imagecode.saveImage(img, new FileOutputStream("F:\\a.jpg"));
    }

}

在實際的開發當中,還需要將圖片上生成的文字保存下來,然后將其保存在Session對象中。將得到圖片的代碼進行改動

public   BufferedImage getImage(){//得到圖片的方法
        BufferedImage img=CreateImage();
        Graphics gps=img.getGraphics();//獲取當前圖片的畫筆
        StringBuilder sb=new StringBuilder();
        //開始畫東西
        for(int i=0;i<4;i++){
            String ch=this.getContent();
            sb.append(ch);
            gps.setColor(this.getColor());
            gps.setFont(this.getFont());
            gps.drawString(ch, w/4*i, h-5);//寬度讓其不滿圖片
        }
        //獲取Session 對象,將sb保存
        drawLine(img);
        return img;
        
    }

附加:開發中可能生成的驗證碼看不清,需要換一張,但是有的瀏覽器有緩存,無法進行刷新,此時寫一段JavaSctipt代碼進行解決。

<script type="text/javascript">
     function fun(){
       1.通過id得到圖片驗證碼
       2.將圖片的連接后面加上new Date().gettime(); 這樣每一次的鏈接就不同,就不會存在緩存的問題了。
     }
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM