//工具类
public class CreateQrcode {
/**
* 生成二维码
*
* @param url 访问链接
* @param response 响应对象
* @throws Exception 抛出异常
*/
public void getQrcode(String url, HttpServletResponse response) throws Exception {
ServletOutputStream servletOutputStream = null;
try {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
servletOutputStream = response.getOutputStream();
BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, 300, 300);
MatrixToImageWriter.writeToStream(bitMatrix, "png", servletOutputStream);
} catch (WalkingException e) {
e.getStackTrace();
} finally {
if (servletOutputStream != null) {
servletOutputStream.flush();
servletOutputStream.close();
}
}
}
}
//应用
/**
* 固定资产二维码生成
*
* @param type 类型
* @param id 资产ID
* @param response 响应
*/
@GetMapping(value = "v1/fixedAssets/qrCode/get")
@ApiOperation(value = "固定资产二维码生成", notes = "二维码类型,固定资产id,请求")
public void findFixedAssetQrcode(Integer type, Integer id, HttpServletResponse response) {
CreateQrcode createQrcode = new CreateQrcode();
String url = "https://www.baidu.com/?type=" + type + "&id=" + assetsId;
try {
createQrcode.getQrcode(url, response);
} catch (Exception e) {
e.printStackTrace();
}
}