java後端產生圖片驗證碼並轉化成base64

來源:互聯網
上載者:User
package com.ucf.staging.utils;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import javax.imageio.ImageIO;import javax.imageio.stream.FileImageOutputStream;import java.awt.*;import java.awt.image.BufferedImage;import java.io.*;import java.util.Random;/** * 產生隨機數字或字母串,以映像方式顯示,用於人工識別,使程式很難識別。 * 減小系統被程式自動攻擊的可能性。 * 產生的圖形顏色由紅、黑、藍、紫4中隨機組合而成,數字或字母垂直方向位置在 * 一定範圍內也是隨機的,減少被程式自動識別的幾率。 * 由於數位0,1,2易和字母的o,l,z混淆,使人眼難以識別,因此不產生數字和字母的混合串。 * 產生的串字母統一用小寫,串的最大長度為16。 * */public class RandomGraphic {//字元的高度和寬度,單位為像素    private int wordHeight = 10;    private int wordWidth = 15;//字元大小    private int fontSize = 16;//最大字串個數    private  static final int MAX_CHARCOUNT = 16;//垂直方向起始位置    private final int initypos = 5;//要產生的字元個數,由Factory 方法得到    private int charCount = 0;//顏色數組,繪製字串時隨機播放一個    private static final Color[] CHAR_COLOR = {Color.RED,Color.BLUE,Color.MAGENTA,Color.blue};//隨機數產生器    private Random r = new Random();    /**     * 產生映像的格式常量,JPEG格式,產生為檔案時副檔名為.jpg;     * 輸出到頁面時需要設定MIME type 為image/jpeg     */    public static String GRAPHIC_JPEG = "JPEG";    /**     * 產生映像的格式常量,PNG格式,產生為檔案時副檔名為.png;     * 輸出到頁面時需要設定MIME type 為image/png     */    public static String GRAPHIC_PNG = "PNG";//用Factory 方法建立對象    protected RandomGraphic(int charCount){        this.charCount = charCount;    }    /**     * 建立對象的Factory 方法     * @param charCount 要產生的字元個數,個數在1到16之間     * @return 返回RandomGraphic對象執行個體     * @throws Exception 參數charCount錯誤時拋出     */    public static RandomGraphic createInstance(int charCount) throws Exception{        if (charCount < 1 || charCount > MAX_CHARCOUNT){            throw new Exception("Invalid parameter charCount,charCount should between in 1 and 16");        }        return new RandomGraphic(charCount);    }    /**     *  隨機產生一個數字串,並以映像方式繪製,繪製結果輸出到流out中     * @param graphicFormat 設定產生的映像格式,值為GRAPHIC_JPEG或GRAPHIC_PNG     * @param out 映像結果輸出資料流     * @return 隨機產生的串的值     * @throws IOException     */    public String drawNumber(String graphicFormat,OutputStream out) throws IOException{// 隨機產生的串的值        String charValue = "";        /*charValue = randNumber();*/        charValue = randAlphaStr(4);        return draw(charValue,graphicFormat,out);    }    /**     * 隨機產生一個字母串,並以映像方式繪製,繪製結果輸出到流out中     * @param graphicFormat 設定產生的映像格式,值為GRAPHIC_JPEG或GRAPHIC_PNG     * @param out 映像結果輸出資料流     * @return 隨機產生的串的值     * @throws IOException     */    public String drawAlpha(String graphicFormat,OutputStream out) throws IOException{//  隨機產生的串的值        String charValue = "";        charValue = randAlphaStr(4);        return draw(charValue,graphicFormat,out);    }    // 給定範圍獲得隨機顏色    Color getRandColor(int fc, int bc) {        Random random = new Random();        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);    }    /**     * 以映像方式繪製字串,繪製結果輸出到流out中     * @param charValue 要繪製的字串     * @param graphicFormat 設定產生的映像格式,值為GRAPHIC_JPEG或GRAPHIC_PNG     * @param out 映像結果輸出資料流     * @return 隨機產生的串的值     * @throws IOException     */    protected String draw(String charValue,String graphicFormat,OutputStream out) throws IOException{        int width = (charCount+2) * wordWidth;        int height = wordHeight * 3;        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        // 建立一個隨機數產生器類。        Random random = new Random();        // 擷取圖形上下文        Graphics g = image.getGraphics();        // 設定背景色        g.setColor(getColor(100));        g.fillRect(0, 0, width, height);        // 設定字型        g.setFont(new Font("宋體", Font.BOLD, 18));        // 隨機產生155條幹擾線,使圖象中的認證碼不易被其它程式探測到        g.setColor(getRandColor(160, 200));        for (int i = 0; i < 155; i++) {            int x = random.nextInt(width);            int y = random.nextInt(height);            int xl = random.nextInt(12);            int yl = random.nextInt(12);            g.setColor(getColor(25));            g.drawLine(x, y, x + xl, y + yl);        }        // 繪製charValue,每個字元顏色隨機        for(int i = 0; i < charCount; i++){            String c = charValue.substring(i,i+1);            Color color =  CHAR_COLOR[randomInt(0,CHAR_COLOR.length)];            g.setColor(color);            int xpos = (i+1) * wordWidth;            // 垂直方向上隨機            int ypos = randomInt(initypos+wordHeight,initypos+wordHeight*2);            g.drawString(c,xpos,ypos);        }        g.dispose();        image.flush();        // 輸出到流        ImageIO.write(image,graphicFormat,out);        return charValue;    }    /*** 隨機返回一種顏色,透明度0~255 0表示全透     * @return 隨機返回一種顏色     * @param alpha 透明度0~255 0表示全透     */    private Color getColor(int alpha)    {        int R=(int) (Math.random()*255);        int G=(int) (Math.random()*255);        int B=(int) (Math.random()*255);        return new Color(R,G,B,alpha);    }    public String drawInputstr(int num,String graphicFormat,OutputStream out) throws IOException{        String charValue = randAlphaStr(num);        int width = (charCount+2) * wordWidth;        int height = wordHeight * 3;        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();        // 建立一個隨機數產生器類。        Random random = new Random();        // 擷取圖形上下文        Graphics g = image.getGraphics();        // 設定背景色        g.setColor(getColor(80));        g.fillRect(0, 0, width, height);        //設定幹擾點        CreateRandomPoint(width, height,50,g,255);        // 設定字型        g.setFont(new Font("宋體", Font.BOLD, 18));        // 隨機產生155條幹擾線,使圖象中的認證碼不易被其它程式探測到        g.setColor(getRandColor(160, 200));        for (int i = 0; i < 135; i++) {            int x = random.nextInt(width);            int y = random.nextInt(height);            int xl = random.nextInt(12);            int yl = random.nextInt(12);            g.setColor(getColor(200));            g.drawLine(x, y, x + xl, y + yl);        }        // 繪製charValue,每個字元顏色隨機        for(int i = 0; i < charCount; i++){            String c = charValue.substring(i,i+1);            Color color =  CHAR_COLOR[random

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.