第一种:https://blog.csdn.net/weixin_33835103/article/details/86783010
第二种:(有问题,打包时,报程序包不存在):
转载:https://blog.csdn.net/kioo_i_see/article/details/82775097
先看整体效果:
生成的二维码数据:
https://img-blog.csdnimg.cn/2022010701314662329.png
前端页面显示:
<img src="https://img-blog.csdnimg.cn/2022010701314662329.png" />
代码:
<!--谷歌二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
1.
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
/**
* @description:
* @author: Administrator
* @date: 2019-03-18 10:40
*/
@Service
public class QRCodeService {
public String crateQRCode(String content, int width, int height) throws IOException {
String resultImage = "";
if (!StringUtils.isEmpty(content)) {
ServletOutputStream stream = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
@SuppressWarnings("rawtypes")
HashMap<EncodeHintType, Comparable> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 指定字符编码为“utf-8”
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 指定二维码的纠错等级为中级
hints.put(EncodeHintType.MARGIN, 2); // 设置图片的边距
try {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
ImageIO.write(bufferedImage, "png", os);
/**
* 原生转码前面没有 data:image/png;base64 这些字段,返回给前端是无法被解析,可以让前端加,也可以在下面加上
*/
resultImage = new String("data:image/png;base64," + Base64.encode(os.toByteArray()));
return resultImage;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
stream.flush();
stream.close();
}
}
}
return null;
}
}
2.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
/**
* @description: 二维码功能相关
* @author: Administrator
* @date: 2019-03-18 10:13
*/
@RestController
@RequestMapping("/qrcode")
public class QRCodeController {
@Autowired
QRCodeService qrCodeService;
@RequestMapping(value="/getQRCode")
public String getQRCode() throws IOException {
//return qrCodeService.crateQRCode("http://xxx/xx.html",200,200);
return qrCodeService.crateQRCode("this is qrcode",200,200);
}
}
3.浏览器打开:http://localhost:8902/qrcode
把生成的base64数据复制到 http://imgbase64.duoshitong.com/ 或类似在线的Base64转图片的网站进行图片转换