二维码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);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");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 baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);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);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();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 baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);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"; QRCodeWriter 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 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());
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 baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);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 baos = new ByteArrayOutputStream();ImageIO.write(newImage, "png", baos);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();}}}