欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP随机验证图片如何制作

程序员文章站 2022-05-03 16:26:01
小编整理了一篇关于模仿腾讯随机验证图片的文章。下面直接上代码吧!一个java 类和一个jsp 页面。 java类代码: 以下是jsp模仿腾讯随机验证图片引用片段:...

小编整理了一篇关于模仿腾讯随机验证图片的文章。下面直接上代码吧!一个java 类和一个jsp 页面。

java类代码:

以下是jsp模仿腾讯随机验证图片引用片段:

package icewee.image;

import java.awt.basicstroke;
import java.awt.color;
import java.awt.font;
import java.awt.graphics2d;
import java.awt.renderinghints;
import java.awt.geom.line2d;
import java.awt.image.bufferedimage;
import java.util.random;

public class tokenutil {
  private static final string base = "23456789abcdefghjklmnpqrstuvwxyzabcdefhijklmnpqrstuvwxyz";
  private static final int length = base.length();

  public static bufferedimage createtokenimage(int width, int height,
      string randomcode) {
    bufferedimage image = new bufferedimage(width, height,
        bufferedimage.type_int_rgb);
    graphics2d g = image.creategraphics();
    random random = new random();
    g.setcolor(new color(230, 230, 250)); // 设置背景色
    g.fillrect(0, 0, width, height); // 填充背景
    g.setcolor(color.black);
    // 绘制边缘
    g.drawrect(-1, -1, width + 1, height + 1);
    g.setcolor(color.gray);
    // 设置字体,随机选择字体,暂设置8种字体
    font font = new font((new string[] { "arial", "arial black",
        "arial italic", "courier new", "courier new bold italic",
        "courier new italic", "franklin gothic medium",
        "franklin gothic medium italic" })[random.nextint(8)],
        font.plain, 30);
    // g.setstroke(new basicstroke((float)(math.random()),
    // basicstroke.cap_butt, basicstroke.join_bevel));
    g.setfont(font);
    // 定义字体颜色范围
    int red = random.nextint(160), green = random.nextint(50), blue = random
        .nextint(50);
    // 定义一个无干扰线区间和一个起始位置
    int nor = random.nextint(50), rsta = random.nextint(131);
    // 绘制干扰正弦曲线 m:曲线平折度, d:y轴常量 v:x轴焦距
    int m = random.nextint(15) + 5, d = random.nextint(23) + 15, v = random
        .nextint(5) + 1;
    double x = 0.0;
    double y = m * math.sin(math.toradians(v * x)) + d;
    double px, py;
    for (int i = 0; i < 131; i++) {
      px = x + 1;
      py = m * math.sin(math.toradians(v * px)) + d;
      if (rsta < i && i < (rsta + nor))
        g.setcolor(new color(230, 230, 250));
      else
        g.setcolor(new color(red, green, blue));
      // 随机设置像素点宽带(线宽)
      g.setstroke(new basicstroke((float) (math.random() + 1.5f)));
      g.draw(new line2d.double(x, y, px, py));
      x = px;
      y = py;
    }
    char[] codes = randomcode.tochararray();
    for (int i = 0; i < codes.length; i++) {
      // 旋转图形
      int degree = (random.nextint(20) - 10) % 360;
      double ang = degree * 0.0174532925; // 将角度转为弧度
      g.rotate(ang, width / 2, height / 2);
      g.setcolor(new color(red, green, blue));
      g.setrenderinghint(renderinghints.key_antialiasing,
          renderinghints.value_antialias_on);
      int gr = random.nextint(8);
      g.drawstring(string.valueof(codes[i]), 24 * i + 10 + gr, 38);
    }
    g.dispose();
    return image;
  }

  public static string createrandomcode(int size, int width, int height) {
    stringbuffer randomcode = new stringbuffer();
    for (int i = 0; i < size; i++) {
      random random = new random();
      int start = random.nextint(length);
      string strrand = base.substring(start, start + 1);
      randomcode.append(strrand);
    }
    return randomcode.tostring();
  }
}

jsp页面:

以下是jsp模仿腾讯随机验证图片代码引用片段:

<%@ page contenttype="image/jpeg" pageencoding="gbk"%>
<%@ page import="java.awt.image.bufferedimage" %>
<%@page import="javax.imageio.imageio" %>
<%@ page import="com.icesoft.image.tokenutil" %>

<%
  //设置页面不缓存
  response.setheader("pragma", "no-cache");
  response.setheader("cache-control", "no-cache");
  response.setdateheader("expires", 0);
  int width = 120, height = 50;
  string randomcode = tokenutil.createrandomcode(4, width, height);
  system.out.println("生成的随机码:" + randomcode);
  bufferedimage image = tokenutil.createtokenimage(width, height, randomcode);
  imageio.write(image, "jpeg", response.getoutputstream());
  response.flushbuffer();
  out.clear();
  out = pagecontext.pushbody();
%>

以上就是jsp 模仿腾讯随机验证图片全部代码,希望对大家的学习有所帮助。