生成二维码

1,需要依赖:

        <dependency>
        	<groupId>com.google.zxing</groupId>
        	<artifactId>core</artifactId>
        	<version>3.3.0</version>
    	</dependency>
    	<dependency>
        	<groupId>com.google.zxing</groupId>
        	<artifactId>javase</artifactId>
        	<version>3.3.0</version>
    	</dependency>

2,生成二维码工具类

package cn.hyt.hyt_zhdj_cs.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;


public class QRCodeGenerator {

    //生成为字节数组
    public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        return pngData;
    }

   

    //生成二维码图片,并保存在桌面
    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

        Path path = FileSystems.getDefault().getPath(filePath);

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }
}

3,调用:

3.1: 二维码图片,返回到前端


@ApiOperation(value = "二维码")
    @GetMapping("qrcode")
    public AppResponseJson qrcode(HttpServletResponse response) throws Exception {
        // 存放在二维码中的内容
        String text = "我是小铭";
        // 嵌入二维码的图片路径
        String imgPath = null;
        // 生成的二维码的路径及名称
        String destPath = "E:/untitled/jam.jpg";
        //生成二维码,true:表示将嵌入二维码的图片进行压缩,如果为“false”则表示不压缩。
        QRCodeUtil.encode(text, imgPath, destPath, true);
        File file = new File(destPath);
        if(file.exists()) {
            FileInputStream in = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename="+destPath);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            response.setContentType("application/msexcel");
            byte[] b = new byte[1024];
            while(in.read(b)!= -1) {
                os.write(b);
            }
            in.close();
            os.flush();
            os.close();
        }
        return AppResponseJson.successRequest("成功");
    }

3.2:二维码byte数组,返回到前端

 @ApiOperation(value = "二维码1")
    @GetMapping("qrcode1")
    public AppResponseJson qrcode1(HttpServletResponse response) throws WriterException, IOException {
        //二维码内的信息
        String info = "This is my first QR Code";
        byte[] qrcode = QRCodeGenerator.getQRCodeImage(info, 360, 360);
        OutputStream os = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=zsc.jpg");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        response.setContentType("application/msexcel");
        os.write(qrcode);
        os.flush();
        os.close();
        return AppResponseJson.successRequest("成功");
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值