java驗證碼的主要思路
????1.生成一個固定尾數(shù)的隨機字符串
? ? 2.在內(nèi)存中創(chuàng)建一個圖片(固定高度和寬度的矩形)
? ? 3.開始繪制驗證碼躏惋,設(shè)置文字的字體嫡丙、顏色雌芽、大小彪标、以及一些干擾線
? ? 4.結(jié)束繪制
? ? 5.重定向?qū)懭耄▓D片格式)
? ? 1、關(guān)于隨機的字符串呼巷,我們可以預先定于一個字符串(26個英文字母加10個阿拉伯數(shù)字)囱修,然后隨機生成一個[0,35]之間的數(shù)字赎瑰,之后利用string.charAt()方法得到其中的一位字母王悍,最后吧生成的字母添加到StringBuilder里。(根據(jù)情況利用for循環(huán)生成指定尾數(shù)的驗證碼餐曼,之后把驗證碼返回)
? ? 2压储、首先指定圖片的高度和寬度(為了后期的維護和封裝),之后利用BufferedImage來在內(nèi)存中創(chuàng)建一個圖片源譬。
? ? 3集惋、根據(jù)Graphics2D來繪制圖像(矩形),設(shè)置字體踩娘、顏色刮刑、背景色及位置和大小,之后計算出文字的坐標及位置养渴,最后將文字畫入里面雷绢。
下面是代碼
package cn.jiaoshou.code;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Random;
/** * 驗證碼工具類 */
public class CaptcahCode {
????/** * 驗證碼生成的方法
????* @param response
????* @return
????*/
????public static String drawImage(HttpServletResponse response) {
????????//1.定義一個字符串的拼接的StringBuilder StringBuilder builder=new StringBuilder();
????????//產(chǎn)生4個字符串的隨機數(shù)
????????for (int i = 0; i < 5; i++) {
????????builder.append(randomChar());
????????}
????????String code=builder.toString();
????????//2.定義圖片的寬度和高度 int width=100;
????????int height=25;
????????//簡歷bufferedImage對象,指定圖片的長度和寬度以及色彩
????????BufferedImage bi =new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); //繪制緩沖流理卑,在內(nèi)存中創(chuàng)建圖片
????????//3.獲取到Graphics2D繪制對象翘紊,開始繪制驗證碼
????????Graphics2D g=bi.createGraphics();
????????//圖片繪制對象
????????//4.設(shè)置文字的字體、顏色藐唠、大小
????????Font font=new Font("微軟雅黑",Font.PLAIN,20);
????????Color color=new Color(255,255,255);
????????g.setFont(font);
????????//設(shè)置字體
????????g.setColor(color);
????????//設(shè)置顏色
????????g.setBackground(new Color(0,0,0));
????????g.clearRect(0,0,width,height);
????????//干擾線的繪制帆疟,繪制線條到圖片中
????????g.setColor(getRandomColor(180,230));
????????Random random = new Random();
????????for(int i=0;i<10;i++){
????????int x = random.nextInt(width);
????????int y = random.nextInt(height);
????????int x1 = random.nextInt(60);
????????int y1 = random.nextInt(60);
????????g.drawLine(x,y,x1,y1);
????????}
????????//繪制形狀鹉究,獲取矩形對象
????????FontRenderContext context=g.getFontRenderContext();//矩形
????????Rectangle2D bounds=font.getStringBounds(code,context);
????????//計算文字的坐標和間距
????????double x=(width-bounds.getWidth())/2;
????????double y=(height-bounds.getHeight())/2;
????????double ascent=bounds.getY();
????????double basey=y-ascent;
????????g.drawString(code,(int)x,(int)basey);
????????//結(jié)束繪制
????????g.dispose();
????????try {
????????????ImageIO.write(bi,"jpg",response.getOutputStream());
????????????//刷新響應(yīng)流
????????????response.flushBuffer();
????????}catch (Exception ex){
????????????ex.printStackTrace();
????????}
????????return code;
????????}
????????/**
????????* 范圍隨機顏色
????????* @param fc
????????* @param bc
????????* @return
????????*/
????????public static Color getRandomColor(int fc,int bc){
????????????//利用隨機數(shù)
????????????Random random = new Random();
????????????//隨機顏色,了解顏色Color(red,green,blue).rgb三元色 0-255
????????????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);
????????}
????????/**
????????* 產(chǎn)生隨機數(shù)字和字母
????????* @return
????????*/
????????public static char randomChar() {
????????//1.定義驗證需要的字母和數(shù)字
????????String string="qwertyuioplkjhgfdsazxcvbnm1234567890";
????????//2.定義隨機對象 Random random=new Random();
????????return string.charAt(random.nextInt(string.length()));
????????}
}
總結(jié):在實戰(zhàn)的過程中,運行的的時候圖片一直出現(xiàn)錯誤踪宠,之后找了很多原因都沒發(fā)現(xiàn)錯誤自赔。知道半個小時候才發(fā)現(xiàn),在寫入的時候把圖片格式寫成了jsp而不是jpg殴蓬。(一個大寫的媽賣批)