java仿Servlet生成驗(yàn)證碼實(shí)例詳解
在成都做網(wǎng)站、網(wǎng)站建設(shè)中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報(bào)的無錫營銷推廣。創(chuàng)新互聯(lián)建站專業(yè)成都網(wǎng)站建設(shè)十多年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。實(shí)現(xiàn)原理:使用BufferedImage對象的Graphics來進(jìn)行繪制,然后輸出成一張圖片進(jìn)行保存
實(shí)現(xiàn)代碼及詳解:
public class validateCode{ private static Random rand = new Random(); public static void main(String[] args){ int val1 = rand.nextInt(9); int val2 = rand.nextInt(9); int val3 = rand.nextInt(9); int val4 = rand.nextInt(9); String val = val1 + " " + val2 + " " + val3 + " " + val4' BufferedImage buf = drawImage(val); //將最終的圖片保存到D://cheng.png下 ImageIO.write(buf,"png",new File("D://cheng.png"); } public static BufferedImage drawImage(String code){ int height = 30; int width = 60; BufferedImage buf = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics2D gs = buf.createGraphics(); gs.setBackground(Color.black); gs.drawRect(0,0,width,height); //繪制隨機(jī)干擾線 int total = 100; drawRandLine(gs,total); //繪制驗(yàn)證碼 Font font = new Font("行楷",Font.BOLD,20); gs.setFont(font); gs.setColor(getRandColor(155,255)); gs.drawString(code,5,20); return buf; } public static void drawRandLine(Graphics2D gs,int total){ for(int i=0; i
文章標(biāo)題:java仿Servlet生成驗(yàn)證碼實(shí)例詳解-創(chuàng)新互聯(lián)
分享地址:http://weahome.cn/article/djshhh.html