JSP隨機驗證圖片如何製作_JSP編程

來源:互聯網
上載者:User

小編整理了一篇關於模仿騰訊隨機驗證圖片的文章。下面直接上代碼吧!一個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 模仿騰訊隨機驗證圖片全部代碼,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.