java 生成二维码

本文介绍了如何使用Java结合ZXing库来生成二维码。首先,讲述了项目的相关依赖,包括ZXing和Gson。接着,创建了一个工具类,利用ZXing将对象或集合转化为JSON字符串进行编码。然后,详细阐述了如何生成二维码并将其保存到本地。最后,提到了扫描二维码的相关操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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.扫描二维码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值