第一步:导入依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>`
第二步:创建二维码方法
package com.lc.yangzi.module.marketing.sell;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;
/**
* 二维码生成工具
*
* @author: John
*/
public class QRCodeUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* 生成二位码内存图片
*
* @param content
* @param width
* @param height
* @throws Exception
*/
public static BufferedImage toQRCode(String content, int width, int height) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
return toBufferedImage(bitMatrix);
}
/**
* 写入文件
*
* @param content
* 内容
* @param width
* 宽
* @param height
* 高
* @param format
* 图片格式,jpg、png
* @param path
* 图片路径
* @throws Exception
*/
public static void toQRCode(String content, int width, int height, String format, String path) throws Exception {
File outputFile = new File(path);
ImageIO.write(toQRCode(content, width, height), format, outputFile);
}
/**
* 缓存图生成
*
* @param matrix
* @return
*/
private static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void main(String[] args) throws Exception {
String base_url = "D:/";
String img_url = "/images";
String qrcode_url = base_url + img_url + "/qrcode/"+1+".png";
try {
File file = new File(base_url+img_url+"/qrcode");
if (!file.exists() && !file.isDirectory()) {//目录不存在
System.out.println("//目录不存在");
file.mkdirs();//不存在则创建目录
}
File file1 = new File(base_url + img_url + "/qrcode/"+1+".png");//获取文件
if (!file1.exists()) {//文件不存在就生成二维码图片
System.out.println("//图片不存在");
//第一个参数为二维码内容,如果是网页则直接跳转网页,是接口则直接调用接口,最后一个参数则为二维码地址
QRCodeUtil.toQRCode("http://192.168.1.61:9909/yz"+"/sell/customerorder/scanQrCode/"+1, 1000, 1000, "png", qrcode_url);
}else{
System.out.println("//图片已存在");
}
} catch (Exception e) {
}
}
}
第三步:接口
//扫码获取详情
@RequestMapping(value = "/scanQrCode/{customerOrderId}", method = RequestMethod.GET)
public ReturnObject<Object> scanQrCode(HttpServletRequest request, @PathVariable(name = "customerOrderId") Long customerOrderId) throws JsonProcessingException {
QrCodeInfo qrCodeInfo = customerOrderService.selectQrCodeInfoById(customerOrderId);
if(qrCodeInfo != null) return new ReturnObject<>(ConstantOfReturnCode.GLOBAL_RESULT_SUCESS, "成功", qrCodeInfo);
return new ReturnObject<>(ConstantOfReturnCode.GLOBAL_RESULT_FALSE,"未查到订单信息");
}