Java生成二维码并输出到页面显示

最近做项目用到了二维码识别功能,记录一下:

1、导入pom.xml依赖


	com.google.zxing
    core
    3.3.3


    com.google.zxing
    javase
    3.3.3

2、生成二维码的工具类:

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.indfintech.common.exception.BizException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

/**
 * 

QRcodeUtil

*

二维码工具类 */ public class QRcodeUtil { public static void creatRrCode(String contents, int width, int height, HttpServletResponse response) { Hashtable hints = new Hashtable(); //设置容错级别最高 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //设置字符编码为utf-8 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右 hints.put(EncodeHintType.MARGIN, 1); try { // 1、读取文件转换为字节数组 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); //转为图片对象格式 BufferedImage image = toBufferedImage(bitMatrix); //转换成png格式的IO流 ImageIO.write(image, "png", response.getOutputStream()); } catch (Exception e) { e.getMessage(); } } /** * image流数据处理 */ public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } return image; } }

3、springboot 环境下测试:

 @RequestMapping("/test")
    public void getQRcode(HttpServletResponse response) {
        // 二维码的内容
        String content = "二维码存储信息";
        //参数为二维码的内容、长、宽、响应对象
        QRcodeUtil.creatRrCode(content,200,200,response);

    }

4、访问地址:

http://localhost:8080/test

总结:

1、导入maven依赖后,工具类可以复制直接使用!

2、该方法将直接在页面返回一个二维码图片

你可能感兴趣的:(Java生成二维码并输出到页面显示)