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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java生成校验码

發布時間:2023/12/31 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java生成校验码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.Random; import javax.imageio.ImageIO;public class SimpleCaptcha {private static final String VALID_CHARS = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz0123456789"; // remove o,i,lprivate int width;// 驗證碼圖片的寬度。private int height;// 驗證碼圖片的高度。private int fontSize; // 字體大小private int chars; // 字符個數private BufferedImage image;private Random random = new Random();public SimpleCaptcha() {this(100, 30, 26, 4);}public SimpleCaptcha(int width, int height, int fontSize, int chars) {this.width = width;this.height = height;this.fontSize = fontSize;this.chars = chars;this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);}public void createCode(File file) {try {FileOutputStream fos = new FileOutputStream(file);createCode(fos);fos.close();} catch (IOException e) {throw new RuntimeException(e);}}public void createCode(OutputStream os) {Graphics2D g = image.createGraphics();drawBackground(g);drawRandomLines(g, 100);drawFrame(g);drawString();try {ImageIO.write(image, "JPEG", os);} catch (Exception e) {throw new RuntimeException(e);}}private void drawBackground(Graphics2D g) {// 設定圖像背景色(因為是做背景,所以偏淡)g.setColor(getRandomColor(180, 250));g.fillRect(0, 0, width, height);}/*** 繪制干擾線* @param g Graphics2D對象,用來繪制圖像* @param nums 干擾線的條數*/private void drawRandomLines(Graphics2D g, int nums) {g.setColor(getRandomColor(100, 200));int maxlength = 12;for (int i = 0; i < nums; i++) {int x = random.nextInt(width);int y = random.nextInt(height);int xl = random.nextInt(maxlength);int yl = random.nextInt(maxlength);g.drawLine(x, y, x + xl, y + yl);}}private void drawFrame(Graphics2D g) {//畫邊框。g.setColor(Color.BLACK);g.drawRect(0, 0, width - 1, height - 1);}/*** 生成隨機顏色* @param fc 前景色* @param bc 背景色* @return Color對象,此Color對象是RGB形式的。*/private Color getRandomColor(int fc, int bc) {//給定范圍獲得隨機顏色if (fc > 255) fc = 255;if (bc > 255) bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);}private String getRandomString(int length) {char[] codes = new char[length];int size = VALID_CHARS.length();for (int i = 0; i < length; i++) {codes[i] = VALID_CHARS.charAt(random.nextInt(size));}return new String(codes);}private void drawString() {final String randomCode = getRandomString(chars);for (int i = 0; i < chars; i++) {String ch = randomCode.substring(i, i + 1);//縮放文字double scale = (random.nextInt(60) + 70D) / 100D;Font font = new Font("Consola", Font.BOLD, (int) (fontSize * scale));Graphics2D g = image.createGraphics();g.setFont(font);//旋轉文字int w = g.getFontMetrics().charWidth(ch.charAt(0));int h = g.getFontMetrics().getAscent() - g.getFontMetrics().getDescent();int x1 = (width / chars) * i + (width / chars - w) / 2;int y1 = height - (height - h) / 2;int x2 = x1 + w / 2;int y2 = y1 - h / 2;g.rotate((random.nextInt(90) - 45) * Math.PI / 180, x2, y2);// 輸出字符g.setColor(getRandomColor(1, 120));g.drawString(ch, x1, y1);}}public static void main(String[] args) {new SimpleCaptcha().createCode(new File("c:\\1.jpg"));} }

轉載于:https://my.oschina.net/sub/blog/144947

總結

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

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