Java生成二维码

本文档详细介绍了如何使用Java库快速生成各种类型的二维码,包括普通二维码、带有logo的二维码、彩色二维码、背景图片二维码、特殊样式二维码及动态背景二维码。通过简单的API调用,你可以实现从基础到复杂样式的二维码生成,适用于多种场景需求。
摘要由CSDN通过智能技术生成

Java生成二维码

官方文档:

https://liuyueyi.github.io/quick-media

1.导入依赖
		<!--二维码-->
        <dependency>
            <groupId>com.github.liuyueyi.media</groupId>
            <artifactId>qrcode-plugin</artifactId>
            <version>2.6.3</version>
        </dependency>
2.简单实现

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.*;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author liqy
 * @Date 2021/12/3 15:34
 * @describe 生成二维码示例,比较简洁的,要复杂的需自己追加
 * @Version 1.0
 */
@Slf4j
public class QRCodeUtil {

    /**
     * 普通的二维码,增加其他的需自己增加
     * @param content 内容
     * @return base64 格式的图片
     * @throws IOException
     * @throws WriterException
     */
    public static String normalQR(String content) throws IOException, WriterException {
        return QrCodeGenWrapper.of(content).asString();
    }

    /**
     * 普通的中间有log的二维码,增加或改变其他的需自己扩展,例如当前的比例为7
     * @param content 内容
     * @param loginFile 中间的logo
     * @return base64 格式的图片
     * @throws IOException
     * @throws WriterException
     */
    public static String logoQR(String content,InputStream loginFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(content)
                .setLogo(loginFile)
                .setLogoRate(7)
                .setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
                .asString();
    }

    /**
     * 带颜色的二维码,增加或改变其他的需自己扩展,例如当前颜色是红色
     * @param content 内容
     * @return base64 格式的图片
     * @throws IOException
     * @throws WriterException
     */
    public static String colorQR(String content) throws IOException, WriterException {
        return QrCodeGenWrapper.of(content)
                .setDrawPreColor(Color.RED)
                .asString();
    }

    /**
     * 带背景图片的二维码,增加或改变其他的需自己扩展,例如当前透明度为0.5
     * @param content 内容
     * @param backGroundFile 背景图片
     * @return base64 格式的图片
     * @throws IOException
     * @throws WriterException
     */
    public static String backGroundQR(String content,InputStream backGroundFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(content)
                .setBgImg(backGroundFile)
                .setBgOpacity(0.5f)
                .asString();
    }

    /**
     * 特殊样式二维码,增加或改变其他的需自己扩展,例如样式为DrawStyle.CIRCLE
     * @param content 内容
     * @return base64 格式的图片
     * @throws IOException
     * @throws WriterException
     */
    public static String styleQR(String content) throws IOException, WriterException {
        return QrCodeGenWrapper.of(content)
                .setDrawEnableScale(true)
                .setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
                .asString();
    }

    /**
     * 特殊样式为图片的二维码,增加或改变其他的需自己扩展
     * @param content 内容
     * @param fillFile 填充图片
     * @return base64 格式的图片
     * @throws IOException
     * @throws WriterException
     */
    public static String styleFillQR(String content,InputStream fillFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(content)
                .setW(500)
                .setH(500)
                .setDrawEnableScale(true)
                .setErrorCorrection(ErrorCorrectionLevel.H)
                .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
                .addImg(1,1,fillFile)
                .asString();
    }

    /**
     * 背景为动图的二维码,增加或改变其他的需自己扩展
     * @param content 内容
     * @param gifFile 动图
     * @return
     * @throws IOException
     * @throws WriterException
     */
    public static String gifQR(String content,InputStream gifFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(content)
                .setW(500)
                .setH(500)
               .setBgImg(gifFile)
                .setBgOpacity(0.5f)
                .setPicType("pic")
                .asString();
    }

}

更多详情请查看官方文档

https://liuyueyi.github.io/quick-media

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值