Java 生成二维码(正常二维码和带Logo的二维码)

1 Maven依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.2</version>
        </dependency>
        <!--二维码-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>

2 调试代码

package com.controller;

import cn.hutool.extra.qrcode.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/QcCode")
public class QcCodeController {
    /**
     * 获取二维码
     *
     * @param response
     */
    @GetMapping("/getQcCode")
    public void getQcCode(HttpServletResponse response) {
        try {
            QrCodeUtil.generate("https://blog.csdn.net/qq_38974638", 400, 400, "PNG", response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取带Logo二维码
     *
     * @param response
     */
    @GetMapping("/getLogoQcCode")
    public void getLogoQcCode(HttpServletResponse response) {
        try {
            QrCodeUtil.generate("https://blog.csdn.net/qq_38974638", QrConfig.create().setImg(new ClassPathResource("/img/logo.jpg").getFile()), "PNG", response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3 Logo文件

文件地址:

4 调试结果

正常二维码:

带Logo的二维码:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java生成二维码并保存图片的示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeGenerator { public static void main(String[] args) { String text = "https://www.csdn.net"; int width = 500; int height = 500; String format = "jpg"; String filePath = "d://test//out.jpg"; String logoPath = "E:\\test.jpg"; try { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } File qrFile = new File(filePath); ImageIO.write(bufferedImage, format, qrFile); if (logoPath != null && !"".equals(logoPath.trim())) { QRCodeUtil.addLogo(qrFile, new File(logoPath), new LogoConfig()); } } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 其中,QRCodeUtil是一个工具类,用于在二维码中添加Logo。你可以在这里找到QRCodeUtil的完整代码:https://github.com/liuyueyi/quick-media/blob/master/quick-media/src/main/java/com/github/hui/quick/plugin/qrcode/util/QRCodeUtil.java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值