java生成驗證碼圖片的方法
本文實例為大家分享了java生成驗證碼圖片的具體代碼,供大家參考,具體內容如下
示例一:
import org.apache.commons.codec.binary.Base64;import org.apache.commons.lang.RandomStringUtils;import org.apache.commons.lang.StringUtils;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import java.awt.*;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.util.Random;public class RandomVerifyCode { private static final String RANDOM_NUM = '23456789'; private static final String RANDOM_LETTER = 'ABCDEFGHJKMNPQRSTUVWXYZ'; private static final String RANDOM_LETTER_SMALL = 'abcdefghjkmnpqrstuvwxyz'; private static final String RANDOM_STRING = RANDOM_NUM + RANDOM_LETTER + RANDOM_LETTER_SMALL; private int width = 95;// 圖片寬 private int height = 25;// 圖片高 private int lineSize = 40;// 干擾線數量 private int stringNum = 4;// 隨機產生字符數量 private Random random = new Random(); /** * 獲得字體 */ private Font getFont() { return new Font('Fixedsys', Font.CENTER_BASELINE, 18); } /** * 獲得顏色 */ private Color getRandColor(int fc, int bc) { if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc - 16); int g = fc + random.nextInt(bc - fc - 14); int b = fc + random.nextInt(bc - fc - 18); return new Color(r, g, b); } /** * 生成隨機圖片(BASE64格式) */ public String getRandcode(HttpServletRequest request, StringBuffer imgBuffer) { // BufferedImage類是具有緩沖區的Image類,Image類是用于描述圖像信息的類 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g = image.getGraphics();// 產生Image對象的Graphics對象,改對象可以在圖像上進行各種繪制操作 g.fillRect(0, 0, width, height);//圖片大小 g.setFont(new Font('Times New Roman', Font.ROMAN_BASELINE, 18));//字體大小 g.setColor(getRandColor(110, 133));//字體顏色 // 繪制干擾線 for (int i = 0; i <= lineSize; i++) { drowLine(g); } // 繪制隨機字符 String randomString = ''; for (int i = 1; i <= stringNum; i++) { randomString = drowString(g, randomString, i); } g.dispose(); ByteArrayOutputStream out = new ByteArrayOutputStream(); String encode = null; try { // 將內存中的圖片通過流動形式輸出到客戶端 ImageIO.write(image, 'JPEG', out); encode = 'data:image/jpeg;base64,'+Base64.encodeBase64String(out.toByteArray()); out.close(); } catch (Exception e) { LogHelper.error('登陸控制','驗證碼生成','將內存中生成的驗證碼圖片輸出為BASE64格式編碼失??!'); } imgBuffer.append(encode); return randomString; } /** * 繪制字符串 */ private String drowString(Graphics g, String randomString, int i) { g.setFont(getFont()); g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121))); String rand = String.valueOf(getRandomString(random.nextInt(RANDOM_STRING.length()))); randomString += rand; g.translate(random.nextInt(3), random.nextInt(3)); g.drawString(rand, 13 * i, 16); return randomString; } /** * 繪制干擾線 */ private void drowLine(Graphics g) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(13); int yl = random.nextInt(15); g.drawLine(x, y, x + xl, y + yl); } /** * 獲取隨機的字符 */ public String getRandomString(int num) { return String.valueOf(RANDOM_STRING.charAt(num)); } public static String randomString(int length){ return RandomStringUtils.random(length, RANDOM_STRING.toCharArray()); }}
示例二:
import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;import java.util.Random;public class CodeUtil { private static int width = 90;// 定義圖片的width 90 private static int height = 20;// 定義圖片的height 20 private static int codeCount = 4;// 定義圖片上顯示驗證碼的個數 private static int xx = 15; private static int fontHeight = 18; private static int codeY = 16; private static char[] codeSequence = {’A’, ’B’, ’C’, ’D’, ’E’, ’F’, ’G’, ’H’, ’J’, ’K’, ’M’, ’N’, ’P’, ’Q’, ’R’, ’S’, ’T’, ’U’, ’V’, ’W’, ’X’, ’Y’, ’Z’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’}; public static Map<String,RenderedImage> codePicMap = new HashMap<>(); /** * 生成一個map集合 * code為生成的驗證碼 * codePic為生成的驗證碼BufferedImage對象 * * @return */ public static Map<String, Object> generateCodeAndPic() { // 定義圖像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Graphics2D gd = buffImg.createGraphics(); // Graphics2D gd = (Graphics2D) buffImg.getGraphics(); Graphics gd = buffImg.getGraphics(); // 創建一個隨機數生成器類 Random random = new Random(); // 將圖像填充為藍色// gd.setColor(Color.WHITE); gd.setColor(new Color(232,240,254)); gd.fillRect(0, 0, width, height); // 創建字體,字體的大小應該根據圖片的高度來定。 Font font = new Font('Fixedsys', Font.BOLD, fontHeight); // 設置字體。 gd.setFont(font); // 畫邊框。 gd.setColor(Color.BLACK); gd.drawRect(0, 0, width - 1, height - 1); // 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。 gd.setColor(Color.BLACK); for (int i = 0; i < 10; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); gd.drawLine(x, y, x + xl, y + yl); } // randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 隨機產生codeCount數字的驗證碼。 for (int i = 0; i < codeCount; i++) { // 得到隨機產生的驗證碼數字。 String code = String.valueOf(codeSequence[random.nextInt(31)]); // 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。 red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); // 用隨機產生的顏色將驗證碼繪制到圖像中。 gd.setColor(new Color(red, green, blue)); gd.drawString(code, (i + 1) * xx, codeY); // 將產生的四個隨機數組合在一起。 randomCode.append(code); } Map<String, Object> map = new HashMap<>(); //存放驗證碼 map.put('code', randomCode); //存放生成的驗證碼BufferedImage對象 map.put('codePic', buffImg); return map; } public static void main(String[] args) throws Exception { //創建文件輸出流對象 OutputStream out = new FileOutputStream('D://img/' + System.currentTimeMillis() + '.jpg'); Map<String, Object> map = CodeUtil.generateCodeAndPic(); ImageIO.write((RenderedImage) map.get('codePic'), 'jpeg', out); System.out.println('驗證碼的值為:' + map.get('code')); }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. css代碼優化的12個技巧2. .NET SkiaSharp 生成二維碼驗證碼及指定區域截取方法實現3. django創建css文件夾的具體方法4. ASP中if語句、select 、while循環的使用方法5. ASP中實現字符部位類似.NET里String對象的PadLeft和PadRight函數6. jsp網頁實現貪吃蛇小游戲7. ASP 信息提示函數并作返回或者轉向8. 存儲于xml中需要的HTML轉義代碼9. MyBatis JdbcType 與Oracle、MySql數據類型對應關系說明10. CentOS郵件服務器搭建系列—— POP / IMAP 服務器的構建( Dovecot )
