java生成验证码工具类
程序员文章站
2022-06-19 23:43:24
java生成验证码工具类,供大家参考,具体内容如下
package com.demo.utils;
import java.awt.basicstroke;...
java生成验证码工具类,供大家参考,具体内容如下
package com.demo.utils; import java.awt.basicstroke; import java.awt.color; import java.awt.font; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.geom.quadcurve2d; import java.awt.image.bufferedimage; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.util.random; import javax.imageio.imageio; /** * 生成验证码 * @author dongyangyang * @date 2017/3/13 12:31 * @version 1.0 * */ public class authcodeutils { private final static random random = new random(); // 随机字体样式 private final static int[] fontstyle = { font.hanging_baseline, font.italic, font.layout_left_to_right, font.layout_no_limit_context, font.layout_no_start_context, font.layout_right_to_left, font.center_baseline, font.plain, font.roman_baseline, font.truetype_font, font.type1_font, font.bold }; private final static string[] font = { "ravie", "forte", "arial", "courier" }; private final static int[] xarr = { 23, 24, 19, 20, 21 }; private final static int[] yarr = { 20, 21, 22, 23, 24, 25, 16, 17, 18 }; private final static int[] font_size = { 28, 29, 30, 21, 22, 23, 24, 27, 26, 25 }; /** * 画随机码图 * @param out * @param width * @param height * @throws ioexception */ public static void draw(outputstream out, string value) throws ioexception { int width = 100, height = 40; bufferedimage bi = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics2d g = (graphics2d) bi.getgraphics(); g.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g.setcolor(randcolor(240, 255)); g.fillrect(0, 0, width, height); g.drawrect(1, 1, width - 2, height - 2); for (int i = 0; i < 10; i++) { g.setcolor(randcolor(150, 250)); g.drawoval(random.nextint(110), random.nextint(24), 5 + random.nextint(10), 5 + random.nextint(10)); } g.setfont(getfont()); g.setcolor(randcolor(xarr[(int) (math.random() * 5)], 254)); for (int i = 0, len = value.length(); i < len; i++) { string rand = string.valueof(value.charat(i)); int degree = random.nextint(23); if (i % 2 == 0) { degree = degree * (-1); } int x = xarr[(int) (math.random() * 5)] * i, y = yarr[(int) (math.random() * 8)]; g.rotate(math.toradians(degree), x, y); g.setcolor(randcolor(48, 254)); g.drawstring(rand, x + 8, y + 10); g.rotate(-math.toradians(degree), x, y); } // 图片中间线 g.setcolor(randcolor(0, 200)); // width是线宽,float型 basicstroke bs = new basicstroke(2); g.setstroke(bs); // 画出曲线 quadcurve2d.double curve = new quadcurve2d.double(0d, random.nextint(height - 8) + 4, width / 2, height / 2, width, random.nextint(height - 8) + 4); g.draw(curve); // 销毁图像 g.dispose(); imageio.write(bi, "png", out); } private static font getfont() { return new font(font[(int) (math.random() * 4)], fontstyle[(int) (math.random() * 12)], font_size[(int) (math.random() * 10)]); } private static color randcolor(int fc, int bc) {// 给定范围获得随机颜色 if (fc > 255) { fc = 255; } if (bc > 255) { bc = 255; } int r = fc + random.nextint(bc - fc); int g = fc + random.nextint(bc - fc); int b = fc + random.nextint(bc - fc); return new color(r, g, b); } private static char[] c = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '3', '5', '6', '7', '8', '9' }; public static string getrandom(int size) { stringbuilder sb = new stringbuilder(); for (int i = 0; i < size; i++) { sb.append(c[math.abs(random.nextint()) % c.length]); } return sb.tostring(); } public static void main(string[] args) throws ioexception { for (int i = 1; i < 10; i++) { fileoutputstream out = new fileoutputstream("d:\\bb"+ i +".png"); draw(out, getrandom(4)); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。