二维码生成 带简单说明

news/2025/1/15 22:49:49/

二维码url+标题 拼接 压缩包下载

public void areaSeatQRAndN(HttpServletResponse response, Long areaId) throws IOException {List<StoreSeatInfoVO> storeSeats = storeSeatService.getSeatInfo(areaId, null).stream().filter(i -> StrUtil.isNotEmpty(i.getImg())).collect(Collectors.toList());if (storeSeats.size() == 0) {return;}ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);for (StoreSeatInfoVO item : storeSeats) {URL url = new URL(item.getImg());HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");InputStream inputStream = connection.getInputStream();// 加载原有图片BufferedImage originalImage = ImageIO.read(inputStream);// 新建一个更大的 BufferedImage,用于绘制原有图片和座位名称int width = originalImage.getWidth();int height = originalImage.getHeight() + 100;BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);Graphics2D graphics = newImage.createGraphics();graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());// 将原有图片绘制在中央位置int x = (width - originalImage.getWidth()) / 2;int y = height - originalImage.getHeight();graphics.drawImage(originalImage, x, y, null);// 在顶部左侧位置绘制座位名称graphics.setColor(Color.RED);System.setProperty("awt.useSystemAAFontSettings", "on");System.setProperty("swing.aatext", "true");//TODO 动态设置字体大小 防止太大溢出int fontSize = 64;Font font = new Font("Arial", Font.BOLD, fontSize);graphics.setFont(font);FontMetrics fm = graphics.getFontMetrics(font);String dec=String.format("%s %s %s",item.getStoreName(),item.getAreaName(),item.getSeatName());while (fm.stringWidth(dec) > width - 20) {fontSize -= 2;font = new Font("Arial", Font.BOLD, fontSize);graphics.setFont(font);fm = graphics.getFontMetrics(font);}graphics.drawString(dec, 10, 100);graphics.dispose();// 将图片写入到 ByteArrayOutputStream 中ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);// 将新的图片加入到 zip 压缩文件中zipOutputStream.putNextEntry(new ZipEntry(item.getStoreName() + "-" + item.getAreaName() + "-" + item.getSeatName() + ".png"));zipOutputStream.write(baos.toByteArray());zipOutputStream.closeEntry();inputStream.close();}zipOutputStream.close();byte[] zipFileBytes = outputStream.toByteArray();response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment; filename=\"QRCode.zip\"");response.getOutputStream().write(zipFileBytes);}

二维码url+标题 单图片下载

在这里插入代码片
public void getSeatQRAndN(HttpServletResponse response, String seatId) throws IOException {List<StoreSeatInfoVO> storeSeatInfoVOS = storeSeatService.getSeatInfo(null, seatId).stream().filter(i -> StrUtil.isNotEmpty(i.getImg())).collect(Collectors.toList());if (storeSeatInfoVOS.size() > 0) {StoreSeatInfoVO seatInfoVO = storeSeatInfoVOS.get(0);URL url = new URL(seatInfoVO.getImg());try (InputStream inputStream = url.openStream()) {// 加载图片BufferedImage originalImage = ImageIO.read(inputStream);// 新建一个更大的 BufferedImage,用于绘制原有图片和座位名称int width = originalImage.getWidth();int height = originalImage.getHeight() + 100;BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);Graphics2D graphics = newImage.createGraphics();graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());// 将原有图片绘制在中央位置int x = (width - originalImage.getWidth()) / 2;int y = height - originalImage.getHeight();graphics.drawImage(originalImage, x, y, null);// 在顶部左侧位置绘制座位名称graphics.setColor(Color.RED);System.setProperty("awt.useSystemAAFontSettings", "on");System.setProperty("swing.aatext", "true");FontMetrics fm = graphics.getFontMetrics();//TODO 动态设置字体大小 防止太大溢出int fontSize = 64;Font font = new Font("Arial", Font.BOLD, fontSize);graphics.setFont(font);FontMetrics fm = graphics.getFontMetrics(font);String dec=String.format("%s %s %s",seatInfoVO.getStoreName(),seatInfoVO.getAreaName(),seatInfoVO.getSeatName());while (fm.stringWidth(dec) > width - 20) {fontSize -= 2;font = new Font("Arial", Font.BOLD, fontSize);graphics.setFont(font);fm = graphics.getFontMetrics(font);}graphics.drawString(dec, 10, 100);graphics.dispose();// 将新的图片写入到 ByteArrayOutputStream 中ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);// 将新的图片写入到 response 中byte[] bytes = baos.toByteArray();response.setContentType("application/png");String filename = seatInfoVO.getSeatName() + ".png";response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");System.out.println(response.getHeader("Content-Disposition"));response.setContentLength(bytes.length);response.getOutputStream().write(bytes);response.flushBuffer();}}
}

二维码内容(生成二维码)+ 加标题

//系统字头
String fonts[] =GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();for (String font : fonts) {System.out.println(font);}// 生成二维码String url = "https://example.com/qrcode"; // 这里是现成的二维码urlQRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, 300, 300);BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);// 在二维码上方添加标题Graphics g = qrCodeImage.getGraphics();g.setColor(Color.BLACK);System.setProperty("awt.useSystemAAFontSettings", "on");System.setProperty("swing.aatext", "true");g.setFont(new Font("Arial", Font.BOLD, 24));g.drawString("My Title", 50, 30);g.dispose();// 保存图像数据到内存中ByteArrayOutputStream os = new ByteArrayOutputStream();ImageIO.write(qrCodeImage, "jpg", os);//转byte[] imageData = os.toByteArray();InputStream is = new ByteArrayInputStream(os.toByteArray());// 将合成后的图像保存到本地文件File outputImageFile = new File("/Users/li_ang/Downloads/output.jpg");ImageIO.write(qrCodeImage, "jpg", outputImageFile);

优化后代码

  @Overridepublic void areaSeatQRAndN(HttpServletResponse response, Long areaId) throws IOException {List<StoreSeatInfoVO> storeSeats = storeSeatService.getSeatInfo(areaId, null).stream().filter(i -> StrUtil.isNotEmpty(i.getImg())).collect(Collectors.toList());if (storeSeats.size() == 0) {return;}ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);Resource resource = resourceLoader.getResource("classpath:seatBG.png");BufferedImage seatBG = ImageIO.read(resource.getInputStream());for (StoreSeatInfoVO item : storeSeats) {URL url = new URL(item.getImg());HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");InputStream inputStream = connection.getInputStream();// 加载原有图片BufferedImage originalImage = ImageIO.read(inputStream);originalImage = resize(originalImage, 1000, 1000);//  BufferedImage,用于绘制原有图片和座位名称BufferedImage newImage = new BufferedImage(seatBG.getWidth(), seatBG.getHeight(), BufferedImage.TYPE_INT_ARGB);Graphics g = newImage.getGraphics();//边角为原型g.fillRoundRect(0, 0, newImage.getWidth(), newImage.getHeight(), 100, 100);// 绘制底图g.drawImage(seatBG, 0, 0, null);// 绘制二维码int width = originalImage.getWidth();int height = originalImage.getHeight();int x = (newImage.getWidth() - width) / 2;int y = (newImage.getHeight() - height) / 2;g.drawImage(originalImage, x, y, null);// 在顶部左侧位置绘制座位名称g.setColor(Color.white);//动态设置字体大小 防止太大溢出int fontSize = 128;Font font = new Font("Microsoft YaHei", Font.BOLD, fontSize);g.setFont(font);FontMetrics fm = g.getFontMetrics(font);String dec = String.format("%s %s", item.getAreaName(), item.getSeatName());
//            String dec = item.getStoreName() + " " + item.getAreaName() + " " + item.getSeatName();dec = new String(dec.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);while (fm.stringWidth(dec) > width - 20) {fontSize -= 2;font = new Font("Microsoft YaHei", Font.BOLD, fontSize);g.setFont(font);fm = g.getFontMetrics(font);}//居中int x1 = (newImage.getWidth() - fm.stringWidth(dec)) / 2;g.drawString(dec, x1, originalImage.getHeight() + y + 150);g.dispose();// 将图片写入到 ByteArrayOutputStream 中ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);// 将新的图片加入到 zip 压缩文件中zipOutputStream.putNextEntry(new ZipEntry(item.getStoreName() + "-" + item.getAreaName() + "-" + item.getSeatName() + ".png"));zipOutputStream.write(baos.toByteArray());zipOutputStream.closeEntry();inputStream.close();}zipOutputStream.close();byte[] zipFileBytes = outputStream.toByteArray();String filename = URLEncoder.encode("二维码压缩包.zip", "UTF-8");response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment; filename=" + filename);response.getOutputStream().write(zipFileBytes);}@Overridepublic void getSeatQRAndN(HttpServletResponse response, String seatId) throws IOException {List<StoreSeatInfoVO> storeSeatInfoVOS = storeSeatService.getSeatInfo(null, seatId).stream().filter(i -> StrUtil.isNotEmpty(i.getImg())).collect(Collectors.toList());if (storeSeatInfoVOS.size() > 0) {StoreSeatInfoVO seatInfoVO = storeSeatInfoVOS.get(0);URL url = new URL(seatInfoVO.getImg());Resource resource = resourceLoader.getResource("classpath:seatBG.png");BufferedImage seatBG = ImageIO.read(resource.getInputStream());try (InputStream inputStream = url.openStream()) {// 加载图片BufferedImage originalImage = ImageIO.read(inputStream);originalImage = resize(originalImage, 1000, 1000);BufferedImage newImage = new BufferedImage(seatBG.getWidth(), seatBG.getHeight(), BufferedImage.TYPE_INT_ARGB);Graphics g = newImage.getGraphics();//边角为原型g.fillRoundRect(0, 0, newImage.getWidth(), newImage.getHeight(), 100, 100);// 绘制底图g.drawImage(seatBG, 0, 0, null);//绘制二维码int width = originalImage.getWidth();int height = originalImage.getHeight();int x = (newImage.getWidth() - width) / 2;int y = (newImage.getHeight() - height) / 2;g.drawImage(originalImage, x, y, null);// 在顶部左侧位置绘制座位名称g.setColor(Color.white);//动态设置字体大小 防止太大溢出int fontSize = 128;Font font = new Font("Microsoft YaHei", Font.BOLD, fontSize);g.setFont(font);FontMetrics fm = g.getFontMetrics(font);String dec = String.format("座位号:%s %s", seatInfoVO.getAreaName(), seatInfoVO.getSeatName());while (fm.stringWidth(dec) > width - 20) {fontSize -= 2;font = new Font("Microsoft YaHei", Font.BOLD, fontSize);g.setFont(font);fm = g.getFontMetrics(font);}//居中int x1 = (newImage.getWidth() - fm.stringWidth(dec)) / 2;g.drawString(dec, x1, originalImage.getHeight() + y + 150);g.dispose();// 将新的图片写入到 ByteArrayOutputStream 中ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);// 将新的图片写入到 response 中byte[] bytes = baos.toByteArray();response.setContentType("application/png");String filename = seatInfoVO.getSeatName() + ".png";filename = URLEncoder.encode(filename, "UTF-8");response.setHeader("Content-Disposition", "attachment; filename=" + filename);System.out.println(response.getHeader("Content-Disposition"));response.setContentLength(bytes.length);response.getOutputStream().write(bytes);response.flushBuffer();}}}

http://www.ppmy.cn/news/418716.html

相关文章

二维码生成(带文字)

话不多说 直接上代码 先在pom文件引入zxing包 <!--二维码--><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.2.1</version></dependency><dependency><groupId>…

二维码生成器怎么在线制作?制作二维码其实很简单

二维码生成器怎么在线制作&#xff1f;在互联网快速发展的今天&#xff0c;相信男男女女老少都知晓二维码了&#xff0c;二维码和我们的日常生活息息相关。无论是我们扫码看新闻&#xff0c;还是我们扫码进行支付&#xff0c;可以说我们的生活离不开二维码。那么我们除了保存系…

tp5 QRcode生成带参数二维码

当时为了这个生成二维码&#xff0c;踩了各种坑&#xff0c;搞了一晚上才搞出来&#xff0c;综合网上各位大神&#xff0c;写下笔记以备下次使用 头部use use qrcode\qrcode; header(content-type:image/png); //设置gif Image ob_clean(); $url urldecode("您的链接…

有什么办法可以让微信群二维码永久有效?这类的二维码生成器怎么制作?

微信群在引流时经常会受到7天有效期和200人入群限制&#xff0c;因为这类二维码通常是一个静态的二维码&#xff0c;如果想要让微信群二维码长久有效的话&#xff0c;那么就需要做一个与之相反的二维码&#xff0c;我们称为“动态二维码”&#xff0c;也就是“活码”。 活码可…

用草料二维码生成器制作二维码

用草料二维码生成器制作二维码 打开网页&#xff0c;http://cli.im/,弹出草料二维码生成器。 在“网址”选项输入网址。 点“生成二维码”按钮。 点“转成活码”按钮。 按要求输入账号、密码。 注册成功后&#xff0c;登录。…

生成二维码之后,给二维码 上方添加文字(二)

生完二维码之后,获取因为需求 我们需要 给二位吗上方添加 一些文字之类的提示信息: 如果 座位号什么的; int fontStyle 1; //字体风格int font 24; //字体大小//用来存放带有logo文字的二维码图片String realPath2 url"/new/"shopName"/";String newIma…

微信二维码生成字

http://rd.wechat.com/qrcode/confirm?block_type101&content我的中国心&langzh_CN&scene4 改中文字 然后生成二维码 微信扫一扫&#xff0c;就出来了

二维码生成使用规则

&#xff11;、QR码可用尺寸知识普及 QR码设有1到40的不同版本(种类)&#xff0c;每个版本都具备固有的码元结构(码元数)。(码元是指构成QR码的方形黑白点。) “码元结构”是指二维码中的码元数。从版本1(21码元21码元)开始&#xff0c;在纵向和横向各自以4码元为单位递增&…