分享一个Java生成二维码工具类
直接上代码:
1、CodeUtil.class
package top.lrshuai.blog.util
import java.awt.BasicStroke
import java.awt.Color
import java.awt.Font
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.util.Random
public class CodeUtil {
private static final String[] CODE = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9" }
public static Object[] CreateCode() {
int imgW = 120
int imgH = 42
int r, g, b
Color color
String result = ""
Random random = new Random()
BufferedImage img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_RGB)
Graphics2D graphics = img.createGraphics()
graphics.setFont(new Font("MicroSoft YaHei", Font.PLAIN, 30))
// 绘制背景色
r = random.nextInt(20) + 230
g = random.nextInt(20) + 230
b = random.nextInt(20) + 230
color = new Color(r, g, b)
graphics.setColor(color)
graphics.fillRect(0, 0,