1、用于生成带四位数字验证码的图片
package com.test.web.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
@RestController
@RequestMapping("/test")
public class VerifyController {
@GetMapping("/getVerificationCode")
public void getVerificationCode(HttpServletResponse response, HttpServletRequest request) {
try {
int width = 200;
int height = 69;
BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
String randomText = drawRandomText(width, height, verifyImg);
System.out.println(randomText);
request.getSession().setAttribute("verifyCode", randomText);
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
ImageIO.write(verifyImg, "png", os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String drawRandomText(int width, int height, BufferedImage verifyImg) {
Graphics2D graphics = (Graphics2D) verifyImg.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
graphics.setFont(new Font("微软雅黑", Font.BOLD, 40));
String baseNumLetter = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
StringBuilder sBuffer = new StringBuilder();
int x = 10;
String ch = "";
Random random = new Random();
for (int i = 0; i < 4; i++) {
graphics.setColor(getRandomColor());
int degree = random.nextInt() % 30;
int dot = random.nextInt(baseNumLetter.length());
ch = baseNumLetter.charAt(dot) + "";
sBuffer.append(ch);
graphics.rotate(degree * Math.PI / 180, x, 45);
graphics.drawString(ch, x, 45);
graphics.rotate(-degree * Math.PI / 180, x, 45);
x += 48;
}
for (int i = 0; i < 6; i++) {
graphics.setColor(getRandomColor());
graphics.drawLine(random.nextInt(width), random.nextInt(height),
random.nextInt(width), random.nextInt(height));
}
for (int i = 0; i < 30; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
graphics.setColor(getRandomColor());
graphics.fillRect(x1, y1, 2, 2);
}
return sBuffer.toString();
}
private String drawRandomText2(int width, int height, BufferedImage verifyImg) {
Graphics2D graphics = (Graphics2D) verifyImg.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
graphics.setFont(new Font("微软雅黑", Font.BOLD, 40));
String baseNumLetter = "123456789";
String operationSign = "+-*/";
StringBuilder sBuffer = new StringBuilder();
int x = 10;
String num = "";
String operation = "";
Random random = new Random();
for (int i = 1; i <= 3; i++) {
graphics.setColor(getRandomColor());
int degree = random.nextInt() % 30;
int numIndex = random.nextInt(baseNumLetter.length());
int operationIndex = random.nextInt(operationSign.length());
num = baseNumLetter.charAt(numIndex) + "";
operation = operationSign.charAt(operationIndex) + "";
if (i == 2) {
sBuffer.append(operation);
} else {
sBuffer.append(num);
}
graphics.rotate(degree * Math.PI / 180, x, 45);
graphics.drawString(num, x, 45);
graphics.rotate(-degree * Math.PI / 180, x, 45);
x += 48;
}
for (int i = 0; i < 6; i++) {
graphics.setColor(getRandomColor());
graphics.drawLine(random.nextInt(width), random.nextInt(height),
random.nextInt(width), random.nextInt(height));
}
for (int i = 0; i < 30; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
graphics.setColor(getRandomColor());
graphics.fillRect(x1, y1, 2, 2);
}
return sBuffer.toString();
}
private Color getRandomColor() {
Random ran = new Random();
return new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256));
}
}
2、生成二维码
依赖包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
@GetMapping("/create")
public void createImage(@RequestParam(value = "linkUrl") String linkUrl, HttpServletResponse response) throws Exception {
Map<EncodeHintType, Object> map = new HashMap<>();
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
map.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(linkUrl, BarcodeFormat.QR_CODE, 300, 300, map);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0XFF000000 : 0xFFFFFFFF);
}
}
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
ImageIO.write(bufferedImage, "png", os);
os.flush();
os.close();
}