工具类-随机产生指定位数的验证码
来源:互联网 发布:线性规划的单纯形算法 编辑:程序博客网 时间:2024/06/05 02:58
public class ImageUtil { private static String[] strs = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "2", "3", "4", "5", "6", "7", "8", "9" }; /** * 方法的描述: 随机生成随字符串 * @param n 生成字符个数 * @return String 生成的字符串 */ private static String getRondomStr(int n) { StringBuffer s = new StringBuffer(); for (int i = 0; i < n; i++) { String temp = ""; // 随机产生下标 Random r = new Random(); int a = r.nextInt(strs.length); if (a < 24) { int b = r.nextInt(100); if (b % 2 == 0) { temp = strs[a].toUpperCase(); s.append(temp); } else { s.append(strs[a]); } } else { s.append(strs[a]); } } return s.toString(); } /** * 方法的描述:生成验证码图片(默认的宽和高) * @param str 验证码字符串 * @param out 输出流 * @throws IOException */ public static void getImage(String str, OutputStream out) throws IOException { getImage1(115, 37, str, out); } /** * 方法的描述:创建图片 * @param width 图片宽 * @param height 图片高 * @param str 图片上需要画的字符串 * @param out 输出流 * @throws IOException */ public static void getImage1(int width, int height, String str, OutputStream out) throws IOException { BufferedImage imag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) imag.getGraphics(); g.setColor(getRundomRGB(120, 255)); g.fillRect(0, 0, width, height); g.setColor(getRundomRGB(145, 175)); Random rd = new Random(); for (int i = 0; i < 100; i++) { if (i % 4 == 0) g.setColor(getRundomRGB(120, 220)); int x = rd.nextInt(width); int y = rd.nextInt(height); int x1 = rd.nextInt(width / 3); int y1 = rd.nextInt(height / 3); g.drawLine(x, y, x + x1, y + y1); } char[] c = str.toCharArray(); g.setFont(new Font("Serif", Font.BOLD, 24)); for (int i = 0; i < c.length; i++) { g.setColor(getRundomRGB(0, 90)); if (rd.nextInt(1000) % 2 == 0) { g.rotate(.8, width / 10 + (width / 4) * i, height + height / 2 + height / 12); g.drawString(String.valueOf(c[i]), width / 4 - width / 5 - width / 7 + (width / 4) * i, height - height / 8); g.rotate(-.8, width / 10 + (width / 4) * i, height + height / 2 + height / 12); } else { g.drawString(String.valueOf(c[i]), width / 4 - width / 6 + (width / 4) * i, height - height / 4); } } for (int i = 0; i < 20; i++) { if (i % 2 == 0) g.setColor(getRundomRGB(142, 220)); int x = rd.nextInt(width); int y = rd.nextInt(height); int x1 = rd.nextInt(width / 3); int y1 = rd.nextInt(height / 3); g.drawLine(x, y, x + x1, y + y1); } g.dispose(); ImageIO.write(imag, "png", out); } // 颜色随机生成 private static Color getRundomRGB(int pr, int pg) { if (pr > 255) pr = 255; if (pg > 255) pg = 255; Random rd = new Random(); int r = pr + rd.nextInt(pg - pr); int g = pr + rd.nextInt(pg - pr); int b = pr + rd.nextInt(pg - pr); return new Color(r, g, b); }public static void main(String[] args) throws IOException {FileOutputStream out = new FileOutputStream("E:/c.png");String str = ImageUtil.getRondomStr(4);System.out.println(str);ImageUtil.getImage(str, out);}}
运行main方法,在指定盘符生成指定位数的验证码。
0 0
- 工具类-随机产生指定位数的验证码
- 随机生成指定位数的验证码
- java生成指定位数的随机验证码
- 产生随机的验证码
- 验证码的随机产生
- 原生JS实现随机生成指定位数的验证码并校验
- 产生随机验证码
- 产生随机验证码
- 随机产生验证码
- 随机产生验证码
- 随机产生验证码
- 随机产生指定位数的字符,包括数字,大写字母,小写字母
- C#产生随机验证码的代码
- java产生任意位数的随机字符
- 生成固定位数验证码的工具类
- iOS 验证码随机出现的六位数
- java如何随机生成6位数的验证码
- c# 生成指定位数的随机字符串
- 一组数字中只有两个数单独出现,其他数成对出现,找出这两个数
- Selenium+python操作浏览器问题汇总
- 工作中常用的mysql操作
- web5.form表单
- load-on-startup的作用
- 工具类-随机产生指定位数的验证码
- 从一个简单的例子学习For循环(适合刚刚学习C语言的同学)
- 第四课:css3-圆角 | 文本属性(文本超出部分省略) | 过渡transition | 动画animation
- js中事件绑定3种方法以及事件委托
- 函数的数据传递
- 如何实现Android桌面小部件(二)
- 【Python】字典items返回列表,iteritems返回迭代器
- mysql索引最左匹配原则的理解?
- 大话计算机系统