生成二维码图片,其中text如果是字符串,就是扫描二维码后展示的字符串,如果是路径,就是扫描后的跳转路经
/**
* 将文本信息或跳转路径转换成二维码
*
* @param text 文本信息或跳转路径
* @param width
* @param height
* @param filePath 文件保存路径
* @throws Exception
*/
private static void generateQRCodeImage(String text, int width, int height, String filePath) throws Exception {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
// 设置二维码白边
Map<EncodeHintType, Integer> hints = new HashMap<>();
hints.put(EncodeHintType.MARGIN,0);
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
将二维码生成在指定位置,一般来说二维码图片信息生成后,最好是保存在临时路径下,可销毁不占内存
// 文件上传到obs后,根据返回的链接生成二维码
File pngFile = File.createTempFile(UUID.randomUUID().toString().replaceAll("-", ""), ".png");
String filePath = pngFile.getPath();
//generateQRCodeImage(fileurl,50,25,"C:\\cz\\ceshiFile\\设备图片.png");
generateQRCodeImage(fileurl, 50, 50, filePath);
logger.info("设备信息id="+id+"生成二维码成功filePath="+filePath);
将二维码生成在指定位置
EquipmentExcelVO excelVO = new EquipmentExcelVO();
excelVO.setQRCodeFile(Pictures.ofLocal(filePath).create());
compile = XWPFTemplate.compile(templateFilePath, configs);
compile.render(excelVO);
compile.writeToFile(file.getPath());
XWPFTemplate可以在文档指定位置插入图片信息,如果要插入的图片是网络图片路径,则是
Pictures.ofUrl(filePath),具体可参照官方文档(如下)