創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
使用Java手?jǐn)]一個(gè)生成圖片驗(yàn)證碼的功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
具體內(nèi)容如下
1、首先新建一各專門生成驗(yàn)證碼圖片的類VerifyCode:
public class VerifyCode { private int w=70; private int h=35; private Random r=new Random(); //宋體,華文楷書,黑體,華文新魏,華文隸書,微軟雅黑,楷體_GB2312,Times New Roman private String [] fontNames={"宋體","華文楷書","黑體","華文新魏","微軟雅黑","華文隸書","楷體_GB2312","Times New Roman"}; //可選的字符 private String codes="23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; //背景色 private Color bgColor=new Color(255,255,255); //驗(yàn)證碼上的文本 private String text; //生成隨機(jī)顏色 private Color randomColor(){ int red=r.nextInt(150); int green=r.nextInt(150); int blue=r.nextInt(150); return new Color(red,green,blue); } //生成隨機(jī)字體 private Font randomFont(){ int index=r.nextInt(fontNames.length);//獲取下標(biāo) String fontName=fontNames[index];//生成隨機(jī)的字體名稱 int style=r.nextInt(4);//獲取隨機(jī)樣式,0表示無樣式,1表示粗體,2表示斜體,3表示粗體加斜體 int size=r.nextInt(5)+24;//生成隨機(jī)字號(hào),24~28 return new Font(fontName,style,size); } //畫干擾線 private void drawLine(BufferedImage image){ int num=3;//一共畫3條 Graphics2D g2=(Graphics2D) image.getGraphics(); for(int i=0;i<num;i++){//隨機(jī)生成坐標(biāo),即4個(gè)值 int x1=r.nextInt(w); int y1=r.nextInt(h); int x2=r.nextInt(w); int y2=r.nextInt(h); g2.setStroke(new BasicStroke(1.5F));//設(shè)置筆畫的寬度 g2.setColor(Color.blue);//干擾線顏色 g2.drawLine(x1, y1, x2, y2);//畫線 } } //隨機(jī)生成一個(gè)字符 private char randomChar(){ int index=r.nextInt(codes.length()); return codes.charAt(index); } //創(chuàng)建BufferedImage private BufferedImage createImage(){ BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics2D g2=(Graphics2D) image.getGraphics(); g2.setColor(this.bgColor); g2.fillRect(0, 0, w, h); return image; } //外界調(diào)用這個(gè)方法得到驗(yàn)證碼 public BufferedImage getImage(){ BufferedImage image=createImage();//創(chuàng)建圖片緩沖區(qū) Graphics2D g2=(Graphics2D)image.getGraphics();//得到繪制環(huán)境 StringBuilder sb=new StringBuilder();//用來裝載生成的驗(yàn)證碼文本 //向圖中畫4個(gè)字符 for(int i=0;i<4;i++){//循環(huán)4次 String s=randomChar()+"";//隨機(jī)生成一個(gè)字符 sb.append(s);//把字母添加到sb中 double x=i*1.0*w/4;//設(shè)置當(dāng)前字符的x軸坐標(biāo) g2.setFont(randomFont());//設(shè)置隨機(jī)字體 g2.setColor(randomColor());//設(shè)置隨機(jī)顏色 g2.drawString(s, (int)x, h-5); } this.text=sb.toString();//把生成的字符串賦給了this.text drawLine(image);//添加干擾線 return image; } //返回驗(yàn)證碼圖片上面的文本 public String getText(){ return text; } //保存圖片到指定的輸出流 public static void output(BufferedImage image,OutputStream out) throws IOException{ ImageIO.write(image, "JPEG", out); } }
新聞標(biāo)題:使用Java手?jǐn)]一個(gè)生成圖片驗(yàn)證碼的功能-創(chuàng)新互聯(lián)
本文來源:http://aaarwkj.com/article38/dihosp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、App設(shè)計(jì)、網(wǎng)站排名、品牌網(wǎng)站設(shè)計(jì)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容