java 生成二维码

java 生成二维码最近在做一个自提的需求 当用户下单后给该笔订单生成一个二维码 当用户去实体店自提的时候 实体店扫用户提供的二维码 这样该笔订单就算完成了 1 相关的依赖 dependency groupId com google zxing groupId artifactId javase artifactId version 3 3 0 amp l version dependency

最近在做一个自提的需求,当用户下单后给该笔订单生成一个二维码。当用户去实体店自提的时候,实体店扫用户提供的二维码,这样 该笔订单就算完成了。

1.相关的依赖

 <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.2</version> </dependency> 

2.创建工具类

利用google的zxing工具类,生成二维码。由于encode 只能存储字符串,我们需要把对象或者集合转换成json ,我选择了gson 。

package org.java.qrcode; import com.google.gson.Gson; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; public class CodeImageUtil { 
    // 默认二维码宽度 public static final int WIDTH = 366; // 默认二维码高度 public static final int HEIGHT = 366; // 默认二维码文件格式 public static final String FORMAT = "jpg"; // 二维码参数 public static final Map<EncodeHintType, Object> HINTS = new HashMap<EncodeHintType, Object>(); //初始化编码格式等参数 static { 
    // 字符编码 HINTS.put(EncodeHintType.CHARACTER_SET, "utf-8"); HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 二维码与图片边距 HINTS.put(EncodeHintType.MARGIN, 2); } / * * @description:功能描述 将二维码写出到输出流中 * @param content 二维码内容即要存储在二维码中的内容(扫描二维码之后获取的内容) * @param stream 输出流 * @param width 二维码宽 * @param height 二维码高 * @throws WriterException * @throws IOException * @see: 需要参见的其它内容 */ public static void writeToStream(Object content, OutputStream stream, Integer width, Integer height) throws WriterException, IOException { 
    if(width==null){ 
    width=WIDTH; } if(height==null){ 
    height=HEIGHT; } Gson gson = new Gson(); String json = gson.toJson(content); BitMatrix bitMatrix = new MultiFormatWriter().encode(json, BarcodeFormat.QR_CODE, width, height, HINTS); MatrixToImageWriter.writeToStream(bitMatrix, FORMAT, stream); } } 

3.创建二维码并且保存

可以把图片保存在本地,也可以存到nginx图片服务器中,也可以存到阿里云oss里。我这里就存到本地了。

package org.java.qrcode; import com.google.zxing.WriterException; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.*; @SpringBootApplication public class QrcodeApplication { 
    public static void main(String[] args) throws IOException, WriterException { 
    User user=new User(); user.setId(1); user.setName("张三"); user.setSex("男"); ByteArrayOutputStream out = new ByteArrayOutputStream(); // 生成二维码图片 CodeImageUtil.writeToStream(user, out, 300, 300); InputStream in = new ByteArrayInputStream(out.toByteArray()); //将生成的二维码写入图片,也可直接使用流 String filePath="X:\\image\\"+user.getName()+".jpg"; FileOutputStream fos = new FileOutputStream(filePath); int length; byte[] b = new byte[1024]; while ((length=in.read(b))>0){ 
    fos.write(b,0,length); } fos.flush(); in.close(); fos.close(); SpringApplication.run(QrcodeApplication.class, args); } } 

4.扫描二维码

在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222214.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午4:18
下一篇 2026年3月17日 下午4:19


相关推荐

  • 一文讲透 OpenClaw 安装Skill

    一文讲透 OpenClaw 安装Skill

    2026年3月13日
    2
  • android的四大组件_android sdk是什么

    android的四大组件_android sdk是什么android四大组件分别为activity、service、contentprovider、broadcastreceiver。一、android四大组件详解1、activity(1)一个Activity通常就是一个单独的屏幕(窗口)。(2)Activity之间通过Intent进行通信。(3)android应用中每一个Activity都必须要在AndroidManif

    2025年8月20日
    5
  • python中divmod函数的用法_Python中divmod函数的用法

    python中divmod函数的用法_Python中divmod函数的用法Python 中 divmod 函数的用法 语言 余数 是一种 面向对象 函数 Python 中 divmod 函数的用法 Python 中 divmod 函数的用法在 Python 中 divmod 函数的作用是把除数和余数运算结果结合起来 其用法为 divmod a b 其中 a 和 b 的类型都是数字类型 返回值为一个包含商和余数的元组 使用时该函数无需导入 可直接使用 PythonPython 是一个高层次的结合了解释性

    2025年12月3日
    8
  • Brup插件开发手记

    Brup插件开发手记前言在一些攻防演练中,像Shiro、Fastjson等常见高危漏洞一直被高频利用。但在一些情况下,这些漏洞通过几轮的洗刷下来出现的频率会逐渐变少。在打点的时候,一些平时并不会去

    2021年12月13日
    57
  • 安全帽识别的系统应用

    安全帽识别的系统应用安全帽识别的原理是用AI技术对工作现场的视频进行实时分析,如果发现工作人员未按要求佩戴安全帽,系统会自动发出警报,在提醒管理人员的同时,系统会自动保存时间、地点及相应的照…

    2022年5月19日
    40
  • Webdriver下载及使用

    Webdriver下载及使用Chrome浏览器驱动下载地址:http://chromedriver.storage.proxy.ustclug.org/index.htmlFirfox浏览器驱动下载地址:https://github.com/mozilla/geckodriver/releasesIE浏览器驱动下载地址:https://www.selenium.dev/downloads/Edge浏览器驱动下载地址:https://developer.microsoft.com/en-us/microsoft-edg…

    2026年1月27日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号