SpringBoot+zxing批量生成二维码_南国

SpringBoot生成和解析二维码完整工具类分享(提供Gitee源码) 在日常的开发工作当中可能需要实现一个二维码小功能,我参考了网上很多关于SpringBoot生成二维码的教程,最终还是自己封装了一套完整生成二维码的工具类,可以支持基础的黑白二维码、带颜色的二维码、带Logo的二维码、带颜色和Logo的二维码和解析二维码,一共4种非常的全面了,可以生成具体的二维码文件或返回Base64,都是博主自己手写封装好的,这边免费开源给大家一键使用!只求大家一个免费的三连支持! 阅读详情

这个没啥好说的俩类就完事,随便写写,做个记录吧

直接上代码,三步完成

第一步:引Maven的jar包

	<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>javase</artifactId>
			<version>3.3.1</version>
	</dependency>

第二步:添加工具类

package com.hnyfkj.agriculturalpark.biz.code;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeUtil {
	private static final String CHARSET = "utf-8";
	private static final String FORMAT_NAME = "JPG";
	// 二维码尺寸
	private static final int QRCODE_SIZE = 1000;
	// LOGO宽度
	private static final int WIDTH = 60;
	// LOGO高度
	private static final int HEIGHT = 60;

	private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
		@SuppressWarnings("rawtypes")
		Hashtable<EncodeHintType, Comparable> hints = new Hashtable<EncodeHintType, Comparable>();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
		hints.put(EncodeHintType.MARGIN, 1);
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
				hints);
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
			}
		}
		if (imgPath == null || "".equals(imgPath)) {
			return image;
		}
		// 插入图片
		QRCodeUtil.insertImage(image, imgPath, needCompress);
		return image;
	}

	private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
		File file = new File(imgPath);
		if (!file.exists()) {
			System.err.println("" + imgPath + "   该文件不存在!");
			return;
		}
		Image src = ImageIO.read(new File(imgPath));
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		if (needCompress) { // 压缩LOGO
			if (width > WIDTH) {
				width = WIDTH;
			}
			if (height > HEIGHT) {
				height = HEIGHT;
			}
			Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
			BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null); // 绘制缩小后的图
			g.dispose();
			src = image;
		}
		// 插入LOGO
		Graphics2D graph = source.createGraphics();
		int x = (QRCODE_SIZE - width) / 2;
		int y = (QRCODE_SIZE - height) / 2;
		graph.drawImage(src, x, y, width, height, null);
		Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
		graph.setStroke(new BasicStroke(3f));
		graph.draw(shape);
		graph.dispose();
	}

	public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
		BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
		mkdirs(destPath);
		// String file = new Random().nextInt(99999999)+".jpg";
		// ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
		ImageIO.write(image, FORMAT_NAME, new File(destPath));
	}

	public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {
		BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
		return image;
	}

	public static void mkdirs(String destPath) {
		File file = new File(destPath);
		// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}
	}

	public static void encode(String content, String imgPath, String destPath) throws Exception {
		QRCodeUtil.encode(content, imgPath, destPath, false);
	}
	// 被注释的方法
	/*
	 * public static void encode(String content, String destPath, boolean
	 * needCompress) throws Exception { QRCodeUtil.encode(content, null,
	 * destPath, needCompress); }
	 */

	public static void encode(String content, String destPath) throws Exception {
		QRCodeUtil.encode(content, null, destPath, false);
	}

	public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)
			throws Exception {
		BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
		ImageIO.write(image, FORMAT_NAME, output);
	}

	public static void encode(String content, OutputStream output) throws Exception {
		QRCodeUtil.encode(content, null, output, false);
	}

	public static String decode(File file) throws Exception {
		BufferedImage image;
		image = ImageIO.read(file);
		if (image == null) {
			return null;
		}
		BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
		Result result;
		Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
		hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
		result = new MultiFormatReader().decode(bitmap, hints);
		String resultStr = result.getText();
		return resultStr;
	}

	public static String decode(String path) throws Exception {
		return QRCodeUtil.decode(new File(path));
	}
}

第三步:引用工具类中的生成方法

package com.hnyfkj.agriculturalpark.biz.code.controller;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hnyfkj.agriculturalpark.biz.assetsinfo.service.AssetsInfoService;
import com.hnyfkj.agriculturalpark.biz.code.QRCodeUtil;
import com.hnyfkj.agriculturalpark.biz.tracesourcelist.entity.TracesourceListEntity;
import com.hnyfkj.agriculturalpark.biz.tracesourcelist.service.TracesourceListService;

import io.swagger.annotations.Api;

@RestController
@RequestMapping("/down")
@Api(tags = { "资产信息列表导出" })
public class ErWeiMazipController {

	@Autowired
	private TracesourceListService tracesourceListService;

	/**
	 * data为链接,用 , 分割
	 * 
	 * @param response
	 * @param data
	 * @throws Exception
	 */
	@RequestMapping("/downloadAllQr")
	public void download(HttpServletResponse response, String traceSourceBatch) throws Exception {
		ZipOutputStream zos = null;
		String downloadFilename = "追溯码";
		// 转换中文否则可能会产生乱码
		downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
		// 指明response的返回对象是文件流
		response.setContentType("application/octet-stream");
		// 设置在下载框默认显示的文件名
		response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename + ".zip");
		String url = "http://xxxx";
		// 根据溯源码批次查找
		List<TracesourceListEntity> tracesourceListEntity = tracesourceListService.findListByTrace(traceSourceBatch);
		// 组装Url
		zos = new ZipOutputStream(response.getOutputStream());
		// 循环遍历组装
		for (TracesourceListEntity tracesourceList : tracesourceListEntity) {
			String uu = url + "?product_batch=" + tracesourceList.getProductBatch() + "&trace_source_id="
					+ tracesourceList.getTraceSourceId();
			zos.putNextEntry(new ZipEntry(tracesourceList.getTraceSourceId() + ".png"));
			ImageIO.write(QRCodeUtil.encode(uu, "D:/logo.png", true), "jpg", zos);
		}
		zos.flush();
		zos.close();
	}

}

完事了,自己测吧,具体扫码出现的数据根据自己业务来

简单的拼接成url能扫出来就完事了

前人种树后人乘凉,你们慢慢乘凉哈

springboot-批量生成二维码 功能介绍: 可以通过页面上传Excel表格,批量生成二维码,并下载生成二维码图片。 立即下载

相关推荐

SpringBoot生成二维码单个下载和打包下载。

实际开发中有不少二维码生成并下载的需求,单个和批量下载都比较常见,最近又来了这种需求,决定记录一下。 https://blog.csdn.net/weixin_40579395/article/details/124040713?spm=1001.2014.3001.5502 源码

springboot+hutool批量生成二维码压缩导出

文章目录1.引入依赖2.测试编码3.批量生成4.解析excel5.批量图片压缩6.上传excel直接将输出流转成压缩包 1.引入依赖 <!-- 生成二维码依赖--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <ver

作为一个长者,有必要记录下人生经验 1515

生成二维码两种方法

java生产二维码的两种方法,有源码,java代码和js代码。

用itextpdf google.zxing批量生成资产标签pdf 带二维码

这里写自定义目录标题 #资产标签pdf批量生成(含文字和二维码,多页) 第一步:创建基础word文档,并另存为pdf文档格式 第二步:用Adobe Acrobat 9 Pro 打开保存好的pdf文档,启用表单编辑添加域。 第三步:上代码 //jar包依赖 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> <groupId>com.itextpdf&lt

weixin_43913457的博客 934

利用Qrcode批量生成二维码

利用QrCodeNet批量生成二维码,并附有导出,排版打印功能。

SpringBoot二维码和条形码生成工具包

java生成二维码和条形码(一维码),每个工具类自带main方法,改一下包名和生成路径即可测试。本人使用SpringBoot开发,打包上传的资源

SpringBoot (批量)生成二维码工具类多种方法示例

Java (批量)生成二维码工具类示例

白白白鲤鱼的博客 2132

UniTouch:多模态触觉对齐技术如何重塑零样本跨模态应用

UniTouch模型通过创新的多模态触觉对齐技术,成功将触觉数据映射到通用的视觉-语言嵌入空间,解决了触觉模态的“孤岛困境”。该技术利用对比学习和传感器特定标记,实现了高质量的零样本学习能力,使模型无需额外训练即可完成触觉分类、描述等任务,并能驱动跨模态生成,为机器人、VR和智能制造等领域提供了强大的触觉理解新范式。

weixin_29044713的博客 415

SpringBoot利用ZXing工具来生成二维码(简单)

一、简单二维码生成 1.1、依赖 <!--二维码工具--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> 1.2、二维码生成工具 QRCodeUtil package com.example.demo.ut

qq_45660133的博客 2869

LauterBach使用教程

(也就是服务器上编译时候的路径一模一样的路径,选中到工程目录就可以)4.选择自己板子对应的cpu(可以直接界面选择也可以命令行的方式)(cpu里的system setting),我们板子需要sys复位,所以选择sys。注:一般情况下运行状态不可以修改外设寄存器的值,但是可以通过修改配置来修改为运行时可修改(cpu寄存器则不可以)(修改这个配置,运行时也可以查看代码)注意:lauterbach可以使用命令来操作,但是由于本人刚刚使用,目前基本上使用的都是界面的按钮,以后会陆续学习命令行指令和脚本的方式。

wacebb的博客 1万+

NXP应用随记(八):S32K3XX的HSE学习记录(HSE\MU\UTEST\IVT\A,B SWAP)

HSE,即硬件安全引擎(Hardware Security Engine),是NXP S32K312微控制器(MCU)中的一个功能,它提供了一系列的安全特性,包括加密、安全启动和密钥存储等。这些功能旨在增强汽车电子系统的信息安全,符合ISO26262标准,达到ASIL D安全等级。具体来说,S32K312的HSE支持AES-128/192/256加密、RSA、ECC等加密算法,以及侧通道保护,确保了数据传输和存储的安全性。

梦想技术家 9778

SpringBoot项目生成二维码,再生成Excel文件导出,亲测采坑

SpringBoot项目生成二维码,再生成Excel文件导出,亲测采坑

qq_17731471的博客 878

Unity2020 发布Android包,下载安装配置JDK、SDK、NDK详细教程

配置成功页面 JDK、SDK、NDK

qq_41263535的博客 1万+

ZXing开源库生成二维码

ZXing作为一款功能丰富的二维码处理库,为开发者提供了灵活而强大的工具。

奇遇少年 3060

LIN总线从入门到精通:低成本汽车通信协议原理与实战

在汽车电子和嵌入式系统中,串行通信协议是实现分布式控制的关键技术。其中,LIN(Local Interconnect Network)总线作为一种专为低成本应用设计的单主多从网络协议,其核心原理在于通过简化的“领导-汇报”通信模式,在满足车身控制、传感器数据采集等场景需求的同时,将硬件与布线成本降至最低。LIN总线采用标准化的帧结构,包含间隔场、同步场和受保护标识符场,并依赖调度表来管理通信时序,确保了网络的确定性与可靠性。其技术价值在于为车窗、座椅、灯光等非安全关键系统提供了高性价比的解决方案,是构建分层

weixin_29185167的博客 264

Upload Symbols Failed:The archive did not include a dSYM for the LiveKitWebRTC.framework with the UU

你需要从源码构建 LiveKitWebRTC,而不是使用 CocoaPods 安装的预编译 .xcframework。Step 2: 修改 Podfile 使用本地路径引用(不是从源下载的 xcframework)方法 :强制从源码构建 LiveKitWebRTC.framework(推荐)这样它就会编译 WebRTC 并带上正确 UUID 的 dSYM。替换路径为你 clone 下来的 LiveKit 源码位置。Step 1: 克隆源码仓库。

baidu_40537062的博客 2144

SpringBoot生成二维码

目录 Zxing原生方式 添加依赖 二维码生成工具类 添加Controller 添加测试页面 使用postman测试效果 Hutool的方式 添加依赖 创建QRCodeService 添加Controller 效果测试 我们使用两种方式,去生成二维码,但是其实,二维码的生成基础,都是zxing包,这是Google开源的一个包,第一种是使用原始的zxing方式去实现,第二种是使用hutool来实现,hutool其实也是对于zxing的一个封装,但是封装前后,确实...

koko创作 4197

华为OD机考双机位C卷 - 简单的解压缩算法 (Java & Python & JS & GO & C++ & C)

华为OD机考双机位C卷 - 简单的解压缩算法 , 提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。

算法大师! 135

SpringBoot实现二维码生成

SpringBoot实现二维码生成

lixianhe的博客 8738

仿真学习系列(四十三):Sigrity仿真之平面谐振分析

在 SIGRITY 中,平面谐振分析(Plane Resonance Analysis)是一种用于分析信号完整性(SI)问题的高级仿真方法,尤其关注于层间电磁波的传播、谐振现象和信号衰减等问题。平面谐振分析通常用于电源分配网络(PDN)、信号线和参考平面之间的相互作用。本文将详细介绍如何在 SIGRITY 中进行平面谐振分析,并提供一些实用的分析方法与步骤,帮助工程师们识别和优化可能存在的设计问题。

案例文章,高速信号 490

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

springboot 生成二维码

二维码

weixin_51751186的博客 4252

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Java 批量生成二维码图片,并打包成可直接下载的zip包(spring boot)

最近做的一个项目中,需要提供给web端一个接口:downloadQr。用到了Google的Zxing框架,版本是3.3.1 。项目使用的是springboot框架。一、API文档接口名称:/downloadAllQr 接口描述:批量生成二维码图片,并打包成可直接下载的zip包 请求方式:GET 请求参数:String data 响应内容:zip文件流二、下面直接上代码 controll

猫玛尼的技术之路 8269

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

springBoot二维码生成案例

1.首先引入谷歌开源项目依赖: &lt;!-- 二维码支持包 --&gt; &lt;dependency&gt; &lt;groupId&gt;com.google.zxing&lt;/groupId&gt; &lt;artifactId&gt;core&lt;/artifactId&gt; &lt;version&gt;3.3.0&lt;/version&am

永封尘埃的博客 1万+

Java后台直接生成二维码介绍

Java后台直接生成二维码 1、其实jquery也可以直接生成二维码的,但我测试的时候,二维码生成后太模糊,难以识别。所以在这里介绍在后来生成二维码的方式。 2、不善于文字描述,直接上代码了。 import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.Bu

星河漫步的专栏 1万+

使用zxing批量生成二维码立牌

使用zxing批量在做好的立牌背景图的指定位置上,把指定的文本内容(链接地址、文本等)生成二维码并放在该位置, 最后加上立牌编号。

Mr_Smile2014的博客 5511
上一篇: SpringBoot+EasyPoi三步完成Excel导入导出_南国
下一篇: Oracle删除一周,一个月,一年之前的数据Sql
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址