效果图 2:
效果图 3:
效果图 4:
效果图 5:
代码片段:
- import java.awt.AlphaComposite;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.OutputStream;
- public class GifCaptcha {
- private Font font = new Font("宋体", Font.BOLD, 20); // 字体
- private int width = 160; // 验证码显示长度
- private int height = 40; // 验证码显示高度
- private String Word = ""; // 当前的字符串
- private int delay = 100; // 帧延迟 (默认 100)
- private int quality = 10;// 量化器取样间隔 - 默认是 10ms
- private int repeat = 0; // 帧循环次数
- private int minColor = 0;// 设置随机颜色时, 最小的取色范围
- private int maxColor = 255;// 设置随机颜色时, 最大的取色范围
- private int right = 0; // 设置字符最右边的相对位置 --- 相对原始位置 , 默认为 0
- /**
- * 空参构造函数
- */
- public GifCaptcha() {
- }
- /**
- * 可以设置验证码宽度, 高度的构造函数
- *
- * @param width
- * - 验证码宽度
- * @param height
- * - 验证码高度
- */
- public GifCaptcha(int width, int height) {
- this.width = width;
- this.height = height;
- }
- /**
- *
- * @param width
- * - 验证码宽度
- * @param height
- * - 验证码高度
- * @param font
- * - 字体
- */
- public GifCaptcha(int width, int height, Font font) {
- this(width, height);
- this.font = font;
- }
- /**
- * @param width
- * - 验证码宽度
- * @param height
- * - 验证码高度
- * @param font
- * - 字体
- * @param delay
- * - 帧延迟
- */
- public GifCaptcha(int width, int height, Font font, int delay) {
- this(width, height, font);
- this.delay = delay;
- }
- public Font getFont() {
- return font;
- }
- /**
- * 设置字体
- *
- * @param font
- */
- public void setFont(Font font) {
- this.font = font;
- }
- public int getWidth() {
- return width;
- }
- /**
- * 设置验证码宽度
- *
- * @param width
- */
- public void setWidth(int width) {
- this.width = width;
- }
- public int getHeight() {
- return height;
- }
- /**
- * 设置验证码高度
- *
- * @param height
- */
- public void setHeight(int height) {
- this.height = height;
- }
- public String getWord() {
- return Word;
- }
- /**
- * 设置验证码字符
- *
- * @param chars
- */
- public void setWord(String chars) {
- this.Word = chars;
- }
- public int getDelay() {
- return delay;
- }
- /**
- * 设置每一帧之间的延迟时间, 或改变它的后续帧(适用于最后一帧添加).
- *
- * @param delay
- * 单位是毫秒
- */
- public void setDelay(int delay) {
- this.delay = delay;
- }
- public int getQuality() {
- return quality;
- }
- /**
- * 设置图像的颜色量化 (转换质量 由 GIF 规范允许的最大 256 种颜色). 低的值(最小值 = 1) 产生更好的颜色, 但处理显著缓慢.
- * 10 是默认, 并产生良好的颜色而且有以合理的速度. 值更大 (大于 20) 不产生显著的改善速度
- *
- * @param quality
- * 大于 1
- */
- public void setQuality(int quality) {
- if (quality <1) {
- quality = 1;
- }
- this.quality = quality;
- }
- public int getRepeat() {
- return repeat;
- }
- /**
- * 设置 GIF 帧应该播放的次数. 默认是 0; 0 意味着无限循环. 必须在添加的第一个图像之前被调用.
- *
- * @param repeat
- * 必须大于等于 0
- */
- public void setRepeat(int repeat) {
- if (repeat>= 0) {
- this.repeat = repeat;
- }
- }
- public int getRight() {
- return right;
- }
- public void setRight(int right) {
- this.right = right;
- }
- public int getMaxColor() {
- return maxColor;
- }
- public void setMaxColor(int maxColor) {
- this.maxColor = maxColor;
- }
- public int getMinColor() {
- return minColor;
- }
- public void setMinColor(int minColor) {
- this.minColor = minColor;
- }
- /**
- * 给定一个输出流 , 输入图片
- *
- * @param os
- */
- public void outImage(OutputStream os) {
- try {
- AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder();// gif 编码类
- // 生成字符
- gifEncoder.start(os);
- gifEncoder.setQuality(quality);// 设置量化器取样间隔
- gifEncoder.setDelay(delay);// 设置帧延迟
- gifEncoder.setRepeat(repeat);// 帧循环次数
- BufferedImage frame;
- char[] rands = randomCaptcha(4);
- Color fontcolor[] = new Color[Word.length()];
- for (int i = 0; i <Word.length(); i++) {
- fontcolor[i] = Randoms.randomColor(minColor, maxColor, 200);
- }
- for (int i = 0; i < Word.length(); i++) {
- frame = graphicsImage(fontcolor, rands, i);
- gifEncoder.addFrame(frame);
- frame.flush();
- }
- gifEncoder.finish();
- } finally {
- try {
- os.close();
- } catch (IOException e) {
- // ignore
- }
- }
- }
- /**
- * 生成验证码图片
- *
- * @param fontcolor
- * 随机字体颜色
- * @param strs
- * 字符数组
- * @param alpha
- * 透明度使用
- * @return BufferedImage
- */
- private BufferedImage graphicsImage(Color[] fontcolor, char[] strs, int alpha) {
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics2D g2d = image.createGraphics();
- g2d.setColor(Color.WHITE);// 利用指定颜色填充背景
- g2d.fillRect(0, 0, width, height);
- AlphaComposite ac;
- float y = ((height - 8)>> 1) + (font.getSize()>> 1);// 字符的 y 坐标
- float m = (width - (Word.length() * font.getSize())) / Word.length();
- float x = m / 10;// 字符的 x 坐标
- g2d.setFont(font);
- for (int i = 0; i <Word.length(); i++) {
- ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getPellucidity(alpha, i));
- g2d.setComposite(ac);
- g2d.setColor(fontcolor[i]);
- g2d.drawOval(Randoms.num(width), Randoms.num(height), Randoms.num(5, 30), 5 + Randoms.num(5, 30));// 绘制椭圆边框
- g2d.drawString(strs[i] + "", x + (font.getSize() + m) * i + right, y);
- }
- Randoms.randomLine(g2d, width, height, 3, 100);
- g2d.dispose();
- return image;
- }
- /**
- * 获取透明度, 从 0 到 1, 自动计算步长
- *
- * @return float 透明度
- */
- protected float getPellucidity(int i, int j) {
- int num = i + j;
- float r = (float) 1 / Word.length(), s = (Word.length() + 1) * r;
- return num> Word.length() ? (num * r - s) : num * r;
- }
- /**
- * 生成随机验证码
- */
- protected char[] randomCaptcha(int count) {
- char[] c = Randoms.randomCaptcha(count);
- Word = String.valueOf(c);
- return c;
- }
- }
下载地址: https://download.csdn.net/download/zxcnlmx/10677140
来源: http://www.bubuko.com/infodetail-2773844.html