JAVA如何批量生成二维码,并返回成压缩包形式?

该博客介绍了如何在Java中批量生成二维码图片,并将其打包成压缩文件返回。通过使用雪花算法确保线程安全和数据唯一性,然后将生成的二维码字符串转换为图片,最后利用ZipOutputStream将图片整合到一个ZIP压缩包中,以供下载。
摘要由CSDN通过智能技术生成

业务:后台根据前端传入的需生成的数量,生成二维码图片并统一打包成压缩包返回。


首先根据一定规则生成二维码字符串,然后返回成list(因为有多条):

为了线程安全与数据唯一,可以给雪花算法中的nextId加上synchronized static关键字,实现类对象锁。

接着将list里的每个元素转成一个个二维码图片,并打包返回。


具体实现:

	/**
	 * 获取二维码压缩包
	 * 
	 * @param response
	 * @param list     二维码字符串列表
	 */
	private void getCodeZip(HttpServletResponse response, List<SampleCode> list) {
		// 生成二维码图片
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment; filename=QrCode-" + System.currentTimeMillis() + ".zip");// 压缩包名
		ZipOutputStream zos = null;
		try {
			zos = new ZipOutputStream(response.getOutputStream());
			// zos.setLevel(5);//压缩等级
			for (int j = 0; j < list.size(); j++) {
				String codeString = list.get(j).getCode();// 获取二维码字符串
				BufferedImage qrCode = QrCodeCreateUtil.createQrCode(codeString, 900, "JPEG");// 生成二维码图片
				InputStream inputStream = bufferedImageToInputStream(qrCode);// 将bufferedImage转成inputStream
				zos.putNextEntry(new ZipEntry(j + ".JPEG")); // 压缩文件名称 设置ZipEntry对象
				// zos.setComment("采样二维码"); // 设置注释
				int temp = 0;
				while ((temp = inputStream.read()) != -1) { // 读取内容
					zos.write(temp); // 压缩输出
				}
				inputStream.close(); // 关闭输入流
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != zos) {
					zos.flush();
					zos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 生成包含字符串信息的二维码图片
	 * 
	 * @param content     二维码携带信息
	 * @param qrCodeSize  二维码图片大小
	 * @param imageFormat 二维码的格式
	 * @throws WriterException
	 * @throws IOException
	 */
	public static BufferedImage createQrCode(String content, int qrCodeSize, String imageFormat)
			throws WriterException, IOException {
		// 设置二维码纠错级别MAP
		Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
		hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		// 创建比特矩阵(位矩阵)的QR码编码的字符串
		BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
		// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
		int matrixWidth = byteMatrix.getWidth();
		int picWidth = matrixWidth - 200;
		int picHeight = matrixWidth - 150;// 除掉二维码过长的底部留白
		BufferedImage image = new BufferedImage(picWidth, picHeight, BufferedImage.TYPE_INT_RGB);
		image.createGraphics();
		Graphics2D graphics = (Graphics2D) image.getGraphics();
		// 画二维码
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, matrixWidth, matrixWidth);
		// 使用比特矩阵画并保存图像
		graphics.setColor(Color.BLACK);
		for (int i = 0; i < matrixWidth; i++) {
			for (int j = 0; j < matrixWidth; j++) {
				if (byteMatrix.get(i, j)) {
					graphics.fillRect(i - 100, j - 100, 1, 1);
				}
			}
		}
		graphics.setColor(Color.BLACK);
		Font font = new Font("黑体", Font.BOLD, 80); // 设置底部文字
		graphics.setFont(font);
		graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 消除文字锯齿
		// 计算文字长度,计算居中的x点坐标
		FontMetrics fm = graphics.getFontMetrics(font);
		int textWidth = fm.stringWidth(content);
		int widthX = (picWidth - textWidth) / 2;
		graphics.drawString(content, widthX, picHeight);
		graphics.dispose();
		BufferedImage bufferedImage = resize(image, 200, 200);// 设置图片大小,并在底部加上字
		return bufferedImage;
	}
	/**
	 * 设置图片大小,并在底部加上字
	 * 
	 * @param content
	 */
	public static BufferedImage resize(BufferedImage img, int newW, int newH) {
		Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
		BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = dimg.createGraphics();
		g2d.drawImage(tmp, 0, 0, null);
		g2d.dispose();
		return dimg;
	}

原文链接:java如何批量生成二维码,并返回成压缩包形式? - 灰信网(软件开发博客聚合) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值