生成带有二维码的海报
public static void main(String[] args) throws IOException { //生成二维码图片 File qrFile = new File("D:\\data\\poster" + File.separator + UUID.randomUUID().toString() + ".jpg"); generateQr(QR_WIDTH, QR_HEIGHT, "https://www.baidu.com/", qrFile); //创建一个画布 BufferedImage thumbImage = new BufferedImage(GRAPHICS_WIDTH, GRAPHICS_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics graphics = thumbImage.getGraphics(); //绘制海报 // String template = this.getClass().getResource("/poster/testFiveG.jpg").getPath(); graphics.drawImage(ImageIO.read(new File("D:\\data\\testFiveG.jpg")), 0, 0, null); //添加二维码 //qrLocation graphics.drawImage(ImageIO.read(qrFile), 317, 944, null); //添加文字 graphics.setColor(Color.white); graphics.setFont(new Font("微软雅黑", Font.PLAIN, 24)); //nicknameLocation graphics.drawString("分享官:" + 130 + "****" + 77820, 255, 1143); //生成海报 File file = new File("D:\\data\\poster" + File.separator + UUID.randomUUID().toString() + ".jpg"); ImageIO.write(thumbImage, "jpg", file); }
生成二维码,使用的hutool工具
private static void generateQr(int width, int height, String content, File targetFile) { QrConfig config = new QrConfig(width, height); config.setMargin(1); QrCodeUtil.generate(content, config, targetFile); }