java仿Servlet生成验证码实例详解-创新互联
java仿Servlet生成验证码实例详解
在成都做网站、网站建设中从网站色彩、结构布局、栏目设置、关键词群组等细微处着手,突出企业的产品/服务/品牌,帮助企业锁定精准用户,提高在线咨询和转化,使成都网站营销成为有效果、有回报的无锡营销推广。创新互联建站专业成都网站建设十多年了,客户满意度97.8%,欢迎成都创新互联客户联系。实现原理:使用BufferedImage对象的Graphics来进行绘制,然后输出成一张图片进行保存
实现代码及详解:
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); //绘制随机干扰线 int total = 100; drawRandLine(gs,total); //绘制验证码 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
网页名称:java仿Servlet生成验证码实例详解-创新互联
标题路径:http://pwwzsj.com/article/djshhh.html