@Override public void getQRCodeAsZip(String garbageRoomId, HttpServletResponse response) throws IOException { //根据roomid在MgtGarbageRoomWeightDeviceEntity中查找对应的imei LambdaQueryWrapper<MgtGarbageRoomWeightDeviceEntity> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(MgtGarbageRoomWeightDeviceEntity::getFkGarbageRoomId, garbageRoomId); List<MgtGarbageRoomWeightDeviceEntity> list = this.list(wrapper); String imei = new String(); for (MgtGarbageRoomWeightDeviceEntity mgtGarbageRoomWeightDeviceEntity : list) { imei = mgtGarbageRoomWeightDeviceEntity.getImei(); } //如果imei为空,则代表该条数据的imei的信息还未录入 if (StringUtils.isBlank(imei)) { throw new RenException("信息还未录入"); } //设置请求头 response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("二维码", "UTF-8") + ".zip\""); response.setHeader("Content-Type", "application/zip"); //设置二维码的内容和logo内容 String baseUrlTemplate = "******"; String[] qrTexts = {"***", "***", "***"}; ByteArrayOutputStream zipOut = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(zipOut); try { for (int i = 0; i < 3; i++) { String url = String.format(baseUrlTemplate, garbageRoomId, imei) + i; BufferedImage qrCodeWithText = createQRCodeWithText(url, 200, 200, qrTexts[i]); ByteArrayOutputStream pngOut = new ByteArrayOutputStream(); ImageIO.write(qrCodeWithText,
java生成二维码添加指定文字logo,通过流将zip文件传回前端
于 2023-07-31 11:45:08 首次发布
本文介绍了如何在Java中使用ZXing库生成二维码,并添加指定文字和Logo。通过设置图像尺寸,创建BufferedImage对象,利用Graphics2D进行绘制,然后将生成的二维码与文字结合。最后,通过流将包含二维码的zip文件传回前端。
摘要由CSDN通过智能技术生成