日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java验证码工具_java 验证码工具

發布時間:2025/3/8 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java验证码工具_java 验证码工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

importjavax.imageio.ImageIO;import java.awt.*;importjava.awt.image.BufferedImage;importjava.io.IOException;importjava.io.OutputStream;importjava.util.Random;public classCaptchaUtils {private final static Object lock = newObject();/*** 圖片的寬度。*/

private int width = 132;/*** 圖片的高度。*/

private int height = 40;/*** 驗證碼字符個數*/

private int codeCount = 4;/*** 驗證碼干擾線數*/

private int lineCount = 10;private staticCaptchaUtils instance;static{synchronized(lock) {if (instance == null) {

instance= newCaptchaUtils();

}

}

}public staticCaptchaUtils getInstance() {returninstance;

}private static final char[] codeSequence ={'A', 'B', 'C', 'D', 'E', 'F', 'G','H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T','U', 'V', 'W', 'X', 'Y', 'Z','1', '2', '3', '4', '5', '6', '7', '8', '9'};publicString createCode() {

Random random= newRandom(System.currentTimeMillis());

StringBuffer buffer= newStringBuffer();for (int i = 0; i < codeCount; i++) {

String strRand=String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);

buffer.append(strRand);

}returnbuffer.toString().toLowerCase();

}public voiddraw(String code, OutputStream sos) {

BufferedImage bufferedImage= this.drawCode(code);try{

ImageIO.write(bufferedImage,"png", sos);

sos.close();

}catch(IOException e) {

e.printStackTrace();

}

}privateBufferedImage drawCode(String code) {int x = 0, fontHeight = 0, codeY = 0;int red = 0, green = 0, blue = 0;//生成隨機數

Random random = newRandom(System.currentTimeMillis());//每個字符的寬度

x = width /codeCount;//字體的高度

fontHeight = height - 8;

codeY=height;//圖像buffer

BufferedImage buffImg = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics2D g=buffImg.createGraphics();//將圖像填充為白色

g.setColor(Color.WHITE);

g.fillRect(0, 0, width, height);

Font font= new Font("Arial", Font.PLAIN, fontHeight);

g.setFont(font);for (int i = 0; i < lineCount; i++) {

red= random.nextInt(255);

green= random.nextInt(128) + 127;

blue= random.nextInt(128) + 127;

g.setColor(newColor(red, green, blue));//初始角度必須大于0

int angRange = random.nextInt(270) + 30;int arcX = random.nextInt(width) - (width >> 4);int arcY =random.nextInt(height);int arcWidth =random.nextInt(width);int arcHeight =random.nextInt(height);int arcStartAng = random.nextInt(angRange) + 1;int arcAng = random.nextInt(angRange) + 1;

g.drawArc(arcX, arcY, arcWidth, arcHeight, arcStartAng, arcAng);

}int offsetX = random.nextInt(5) + 5;int offsetY = random.nextInt(5) + 10;for (int i = 0; i < code.length(); i++) {

String strRand= code.substring(i, i + 1);//產生隨機的顏色值,讓輸出的每個字符的顏色值都將不同。

red = random.nextInt(255);

green= random.nextInt(127);

blue= random.nextInt(255);

g.setColor(newColor(red, green, blue));//正負波動微調

double degree = (random.nextInt(20) - 10) * Math.PI / 180;int fx = (i * x) +offsetX;int fy = codeY -offsetY;

g.rotate(degree, fx, fy);

g.drawString(strRand, fx, fy);//輸出完成旋轉回來

g.rotate(-degree, fx, fy);

}returnbuffImg;

}

}

總結

以上是生活随笔為你收集整理的java验证码工具_java 验证码工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。