您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用imageIO圖像流實(shí)現(xiàn)驗(yàn)證碼效果”,在日常操作中,相信很多人在怎么用imageIO圖像流實(shí)現(xiàn)驗(yàn)證碼效果問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用imageIO圖像流實(shí)現(xiàn)驗(yàn)證碼效果”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
package cn.images; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.Random; public class CapcahaDemo { public static void main(String[] args) throws Exception { //驗(yàn)證碼的長(zhǎng)寬 int len = 4; int width = len * 26; int height =50; //驗(yàn)證碼中的數(shù)字 字母 String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var rand = new Random(); StringBuilder sub = new StringBuilder(); var cc = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); var g= cc.createGraphics(); setRenderingHint(g); //背景的隨機(jī)把顏色 g.setColor(new Color(rand.nextInt(26)+220,rand.nextInt(26)+220,rand.nextInt(26)+220)); //背景 填充矩形 位置 寬高 g.fillRect(0,0,width,height); //字體顏色 var c = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)); g.setColor(c); //生成四個(gè)隨機(jī)驗(yàn)證碼內(nèi)容 for(int i = 0;i<len;i++){ //將其轉(zhuǎn)換成字符串(返回指定索引處的char值。索引范圍是從0到length() - 1 (返回一個(gè)大于0小于letter的長(zhǎng)度的隨機(jī)數(shù))) //就是從letter中隨機(jī)取出一個(gè) String str = String.valueOf(letter.charAt(rand.nextInt(letter.length()))); sub.append(str); //驗(yàn)證碼中內(nèi)容的位置 var x = i*25+5; var y = 35; //設(shè)置文字旋轉(zhuǎn)角度 double radianPercent = Math.PI*(rand.nextInt(35)/180D); if(rand.nextBoolean()) radianPercent = -radianPercent; //文字正向旋轉(zhuǎn) g.rotate(radianPercent,x,y); //驗(yàn)證碼內(nèi)容的字體樣式 粗細(xì)正常 字體大小[20-40) g.setFont(new Font("微軟雅黑",Font.PLAIN,rand.nextInt(20)+20)); g.drawString(str,x,y); //文字反向旋轉(zhuǎn) g.rotate(-radianPercent,x,y); } //干擾文件 for(int i=0;i<len*10;i++) { var x = rand.nextInt(width); var y = rand.nextInt(height); String str = String.valueOf(letter.charAt(rand.nextInt(letter.length()))); double radianPercent = Math.PI * (rand.nextInt(35) / 180D); //if (rand.nextBoolean()) // radianPercent = -radianPercent; //g.rotate(radianPercent, x, y); c = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256),rand.nextInt(30)+60); g.setColor(c); g.setFont(new Font("微軟雅黑", Font.PLAIN, rand.nextInt(10) + 10)); g.drawString(str, x, y); } // 干擾正統(tǒng)曲線 //字體顏色相同 g.setColor(new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255))); int yy = rand.nextInt(35)+15; int hh = rand.nextInt(20)+10; int aa = rand.nextInt(60)+20; for(int x= 0; x<width;x++){ int y = (int)(yy+ hh * Math.sin(x*Math.PI/aa)); g.fillOval(x,y,5,5); } g.dispose(); ImageIO.write(cc,"png",new File("cc.png")); Runtime.getRuntime().exec("cmd /k start cc.png"); } //設(shè)置線條平滑 public static void setRenderingHint(Graphics2D g) { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT); } public static Font getRandFont(int end, int start) { Random rand = new Random(); Font font = null; if(rand.nextBoolean()){ font = new Font("微軟雅黑",Font.PLAIN , rand.nextInt(end) + start); }else { try{ Font f = Font.createFont(Font.TRUETYPE_FONT,new File("fonts/zh264hfyh.ttf")); font = f.deriveFont(Font.PLAIN,rand.nextInt(end)+ start); }catch (Exception e){ e.printStackTrace(); } } return font; } }
到此,關(guān)于“怎么用imageIO圖像流實(shí)現(xiàn)驗(yàn)證碼效果”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。