文章目录
- 1、引入依赖
- 2、Service层实现
- 3、Controller
- 4、效果
之前SpringSecurity时,登录用到了图片验证码辅助登录:【 整合hutool实现集成图片验证码】,以下为整合zxing实现二维码和条形码的生成。
1、引入依赖
引入ZXing依赖的坐标:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
2、Service层实现
Service接口略,实现类:
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
/**
* @author LLG
* @date 2023/12/8
*/
@Service
@Slf4j
public class CodeService {
/**
* 生成二维码
* @param data 扫描二维码后得到的信息
* @param width 二维码的宽
* @param height 二维码的高
* @return image
*/
public BufferedImage generateCode(String data, int width, int height) {
BufferedImage image = null;
try {
Map<EncodeHintType, Object> hints = new HashMap<>();
//字符编码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//错误纠正级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//二维码边距
hints.put(EncodeHintType.MARGIN, 2);
MultiFormatWriter writer = new MultiFormatWriter();
//样式选择QR_CODE,其余枚举类中的可选样式可自己玩
BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, width, height, hints);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//每个框框的颜色
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
image.setRGB(i, j, bitMatrix.get(i, j) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
}
}
/*以下为选择保存二维码
String filePath = "test.png";
File codeFile = new File(filePath);
ImageIO.write(image,"png",codeFile);
log.info("QR码生成成功,保存路径{}",filePath);*/
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
/**
* 生成条形码
* @param data 扫描二维码后得到的信息
* @param width 条形码的宽
* @param height 条形码的高
* @return image
*/
public BufferedImage generateBarCode(String data, int width, int height) {
BufferedImage image = null;
try {
Map<EncodeHintType, Object> hints = new HashMap<>();
//字符编码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter writer = new MultiFormatWriter();
//码的样式这次选CODE_128
BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, height, hints);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
image.setRGB(i, j, bitMatrix.get(i, j) ? 0 : 0xFFFFFF); //黑白条形码
}
}
} catch (WriterException e) {
e.printStackTrace();
}
return image;
}
}
3、Controller
写个简单API调用下:
@RestController
public class CodeController {
@Resource
private CodeService codeService;
@GetMapping("/code/image")
public void getCodeImage(HttpServletRequest request, HttpServletResponse response){
//测试数据
String data = "code9527-test!";
BufferedImage image = codeService.generateCode(data, 100, 100);
try {
ImageIO.write(image,"png",response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@GetMapping("/code/barCode")
public void getBarCode(HttpServletRequest request,HttpServletResponse response){
//条形码中的内容
String data = "TB20231208154900";
BufferedImage barCode = codeService.generateBarCode(data, 400, 100);
try {
ImageIO.write(barCode,"png",response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、效果
二维码:
条形码: