这篇文章主要介绍“怎么用imageIO图像流实现验证码效果”,在日常操作中,相信很多人在怎么用imageIO图像流实现验证码效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用imageIO图像流实现验证码效果”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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 { //验证码的长宽 int len = 4; int width = len * 26; int height =50; //验证码中的数字 字母 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); //背景的随机把颜色 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); //生成四个随机验证码内容 for(int i = 0;i<len;i++){ //将其转换成字符串(返回指定索引处的char值。索引范围是从0到length() - 1 (返回一个大于0小于letter的长度的随机数)) //就是从letter中随机取出一个 String str = String.valueOf(letter.charAt(rand.nextInt(letter.length()))); sub.append(str); //验证码中内容的位置 var x = i*25+5; var y = 35; //设置文字旋转角度 double radianPercent = Math.PI*(rand.nextInt(35)/180D); if(rand.nextBoolean()) radianPercent = -radianPercent; //文字正向旋转 g.rotate(radianPercent,x,y); //验证码内容的字体样式 粗细正常 字体大小[20-40) g.setFont(new Font("微软雅黑",Font.PLAIN,rand.nextInt(20)+20)); g.drawString(str,x,y); //文字反向旋转 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); } // 干扰正统曲线 //字体颜色相同 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"); } //设置线条平滑 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; } }
到此,关于“怎么用imageIO图像流实现验证码效果”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。