java插入二维码图片到文档中

生成二维码图片,其中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),具体可参照官方文档(如下)
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要生成二维码并将其保存到数据库,您可以使用以下步骤: 1. 导入相关的Java库:您可以使用ZXing库来生成二维码。请确保已将其添加到您的Java项目。 2. 生成二维码:使用ZXing库的QRCodeWriter类,您可以创建一个QRCode对象,该对象可以转换为图片格式并保存到本地。 3. 将二维码图片转换为字节数组:使用ImageIO类将二维码图片转换为字节数组。 4. 将字节数组保存到数据库:使用JDBC连接到您的数据库,并使用PreparedStatement类将字节数组保存到数据库。 以下是一些示例代码来演示这些步骤: ```java import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Base64; import javax.imageio.ImageIO; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) throws SQLException { String data = "Hello, world!"; // 数据内容 int size = 300; // 生成二维码图片大小 String format = "png"; // 二维码图片格式 byte[] imageBytes = null; // 生成二维码 QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix; try { matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, size, size); BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, format, baos); imageBytes = baos.toByteArray(); } catch (WriterException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } // 将字节数组保存到数据库 Connection conn = null; // 假设您已连接到数据库 PreparedStatement ps = conn.prepareStatement("INSERT INTO qr_codes (data) VALUES (?)"); ps.setBytes(1, imageBytes); ps.executeUpdate(); } } ``` 在这个示例,我们生成一个包含字符串“Hello, world!”的二维码,将其转换为PNG格式的图片,将图片字节数组保存到名为“qr_codes”的数据库表。请注意,此示例仅用于演示目的,您需要根据您的具体需求进行适当的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值