java生成驗(yàn)證碼工具類
java生成驗(yàn)證碼工具類,供大家參考,具體內(nèi)容如下
package com.demo.utils; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.QuadCurve2D; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; /** * 生成驗(yàn)證碼 * @author dongyangyang * @Date 2017/3/13 12:31 * @Version 1.0 * */ public class AuthCodeUtils { private final static Random random = new Random(); // 隨機(jī)字體樣式 private final static int[] fontStyle = { Font.HANGING_BASELINE, Font.ITALIC, Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_NO_LIMIT_CONTEXT, Font.LAYOUT_NO_START_CONTEXT, Font.LAYOUT_RIGHT_TO_LEFT, Font.CENTER_BASELINE, Font.PLAIN, Font.ROMAN_BASELINE, Font.TRUETYPE_FONT, Font.TYPE1_FONT, Font.BOLD }; private final static String[] font = { "Ravie", "Forte", "Arial", "Courier" }; private final static int[] xarr = { 23, 24, 19, 20, 21 }; private final static int[] yarr = { 20, 21, 22, 23, 24, 25, 16, 17, 18 }; private final static int[] font_size = { 28, 29, 30, 21, 22, 23, 24, 27, 26, 25 }; /** * 畫隨機(jī)碼圖 * @param out * @param width * @param height * @throws IOException */ public static void draw(OutputStream out, String value) throws IOException { int width = 100, height = 40; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bi.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(randColor(240, 255)); g.fillRect(0, 0, width, height); g.drawRect(1, 1, width - 2, height - 2); for (int i = 0; i < 10; i++) { g.setColor(randColor(150, 250)); g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10)); } g.setFont(getFont()); g.setColor(randColor(xarr[(int) (Math.random() * 5)], 254)); for (int i = 0, len = value.length(); i < len; i++) { String rand = String.valueOf(value.charAt(i)); int degree = random.nextInt(23); if (i % 2 == 0) { degree = degree * (-1); } int x = xarr[(int) (Math.random() * 5)] * i, y = yarr[(int) (Math.random() * 8)]; g.rotate(Math.toRadians(degree), x, y); g.setColor(randColor(48, 254)); g.drawString(rand, x + 8, y + 10); g.rotate(-Math.toRadians(degree), x, y); } // 圖片中間線 g.setColor(randColor(0, 200)); // width是線寬,float型 BasicStroke bs = new BasicStroke(2); g.setStroke(bs); // 畫出曲線 QuadCurve2D.Double curve = new QuadCurve2D.Double(0d, random.nextInt(height - 8) + 4, width / 2, height / 2, width, random.nextInt(height - 8) + 4); g.draw(curve); // 銷毀圖像 g.dispose(); ImageIO.write(bi, "png", out); } private static Font getFont() { return new Font(font[(int) (Math.random() * 4)], fontStyle[(int) (Math.random() * 12)], font_size[(int) (Math.random() * 10)]); } private static Color randColor(int fc, int bc) {// 給定范圍獲得隨機(jī)顏色 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 static char[] c = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '3', '5', '6', '7', '8', '9' }; public static String getRandom(int size) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < size; i++) { sb.append(c[Math.abs(random.nextInt()) % c.length]); } return sb.toString(); } public static void main(String[] args) throws IOException { for (int i = 1; i < 10; i++) { FileOutputStream out = new FileOutputStream("D:\\bb"+ i +".png"); draw(out, getRandom(4)); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何使用elasticsearch進(jìn)行模糊查詢
這篇文章主要介紹了Java如何使用elasticsearch進(jìn)行模糊查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02java開發(fā)之讀寫txt文件操作的實(shí)現(xiàn)
本篇文章介紹了,java開發(fā)之讀寫txt文件操作的實(shí)現(xiàn)。需要的朋友參考下2013-05-05SWT(JFace) 簡易瀏覽器 制作實(shí)現(xiàn)代碼
SWT(JFace) 簡易瀏覽器 制作實(shí)現(xiàn)代碼2009-06-06idea報(bào)錯(cuò):java程序包c(diǎn)om.github.xiaoymin.knife4j.spring.annotations
這篇文章主要介紹了idea報(bào)錯(cuò):java程序包c(diǎn)om.github.xiaoymin.knife4j.spring.annotations不存在問題解決,需要的朋友可以參考下2023-06-06springboot+nacos+gateway實(shí)現(xiàn)灰度發(fā)布的實(shí)例詳解
灰度發(fā)布是一種在軟件部署過程中用于平滑過渡的技術(shù),通過引入灰度發(fā)布SDK和配置網(wǎng)關(guān)策略實(shí)現(xiàn),本文就來介紹一下,感興趣的可以了解一下2022-03-03淺談Java中@Autowired和@Inject注解的區(qū)別和使用場景
本文主要介紹了淺談Java中@Autowired和@Inject注解的區(qū)別和使用場景,@Autowired注解在依賴查找方式和注入方式上更加靈活,適用于Spring框架中的依賴注入,而@Inject注解在依賴查找方式上更加嚴(yán)格,適用于Java的依賴注入標(biāo)準(zhǔn),感興趣的可以了解一下2023-11-11