Java QRCode 二维码

8 篇文章 0 订阅
8 篇文章 0 订阅

pom文件中引入jar:net.glxn.qrgen.javase.jar

<!--二维码依赖-->
<dependency>
   <groupId>net.glxn.qrgen</groupId>
   <artifactId>javase</artifactId>
   <version>2.0</version>
</dependency>

2种测试:

package com.dist.QRCode;

import com.dist.base.BaseTest;
import net.glxn.qrgen.core.AbstractQRCode;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.core.vcard.VCard;
import net.glxn.qrgen.javase.QRCode;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 引入jar:net.glxn.qrgen.javase
 *
 * @program: springbootdemo
 * @Date: 2019/1/25 9:01
 * @Author: Mr.Zheng
 * @Description:
 */
public class QRCodeTest extends BaseTest {

    private final Logger log = LoggerFactory.getLogger(QRCodeTest.class);

    /**
     * 二维码生成测试1
     */
    @Test
    public void testQrcode1() {

        AbstractQRCode qrCode = QRCode.from("https://www.baidu.com");
        // 设置字符集,支持中文
        qrCode.withCharset("utf-8");
        // 设置生成的二维码图片大小
        qrCode.withSize(260,260);
        ByteArrayOutputStream out = qrCode.to(ImageType.PNG).stream();
        File file = new File("D:\\qrCode.png");
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(file);
            fout.write(out.toByteArray());
            fout.flush();
            System.out.println("***********二维码生成成功!**********");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fout.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 二维码生成测试2
     */
    @Test
    public void testQrcode2() {
        ByteArrayOutputStream bout =
                QRCode.from("https://www.baidu.com")
                        .withCharset("utf-8")
                        .withSize(250, 250)
                        .to(ImageType.PNG)
                        .stream();

        try {
            OutputStream out = new FileOutputStream("D:\\qr-code.png");
            bout.writeTo(out);
            out.flush();
            out.close();

        } catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 推荐使用:模板
     * 二维码生成测试3 :VCard是标准电子商务名片格式,包含元数据有,名称,地址,公司,手机号,职位,邮箱,网站等
     */
    @Test
    public void testQrcode3() {

        VCard vCard = new VCard();
        vCard.setName("张三");
        vCard.setAddress("上海市浦东新区张江镇");
        vCard.setCompany("公司名称");
        vCard.setPhoneNumber("15937966356");
        vCard.setTitle("Java开发");
        vCard.setEmail("126354652@qq.com");
        vCard.setWebsite("https://www.jianshu.com/u/70d69269bd09");

        ByteArrayOutputStream bout =
                QRCode.from(vCard)
                        .withCharset("utf-8")
                        .withSize(250, 250)
                        .to(ImageType.PNG)
                        .stream();

        try {
            OutputStream out = new FileOutputStream("D:\\qr-code-vcard.png");
            bout.writeTo(out);
            out.flush();
            out.close();

        } catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

以上为测试,想要用到项目种需要做更改。

更改:

​    存储位置:可以是本地路径,或者存到mongodb,或数据库等

​    存储内容:url/String/对象VCard

​    生成形式:可以是文件/流

​    类型:ImageType:JPG, GIF, PNG, BMP

​    大小:withSize 自定义

​    支持中文:withCharset(“utf-8”)

​    编码规范:适用于自己的项目(此实例只是测试使用)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇宙小神特别萌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值