DDR爱好者之家 Design By 杰米

小编整理了一篇关于模仿腾讯随机验证图片的文章。下面直接上代码吧!一个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 模仿腾讯随机验证图片全部代码,希望对大家的学习有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。