阅读 66

spring boot zxing 生成 二维码 base64 图片

直接上代码

引入jar包

<!-- 二维码支持包 -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.2.0</version>
        </dependency>
复制代码

 

控制层支持访问

package com.supermap.qrcode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

/**
 *  获取二维码
 * @author yushen
 *
 */
@RestController
@RequestMapping("/qrcode")
public class QRCodeController {

	@Autowired
	QRCodeService qrCodeService;

	/**
	 * 传入二维码要生成的内容 返回base64图片
	 * @param req
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/getQRCode")
	@ResponseBody
	public String getQRCode(HttpServletRequest req) throws IOException {
		String qrCodeValue = req.getParameter("value") == null ? "nul" : req.getParameter("value");
		return qrCodeService.crateQRCode(qrCodeValue, 200, 200);
	}

}
复制代码

 

服务层提供生成图片base64

package com.supermap.qrcode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * 生成二维码
 * @author yushen
 *
 */
@Service
public class QRCodeService {

	public String crateQRCode(String content, int width, int height) throws IOException {

		String resultImage = "";
		if (!StringUtils.isEmpty(content)) {
			ServletOutputStream stream = null;
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			@SuppressWarnings("rawtypes")
			HashMap<EncodeHintType, Comparable> hints = new HashMap<>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 指定字符编码为“utf-8”
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 指定二维码的纠错等级为中级
			hints.put(EncodeHintType.MARGIN, 2); // 设置图片的边距

			try {
				QRCodeWriter writer = new QRCodeWriter();
				BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

				BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
				ImageIO.write(bufferedImage, "png", os);
				/**
				 * 原生转码前面没有 data:image/png;base64 这些字段,返回给前端是无法被解析,可以让前端加,也可以在下面加上
				 */
				resultImage = new String("data:image/png;base64," + Base64.encode(os.toByteArray()));

				return resultImage;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (stream != null) {
					stream.flush();
					stream.close();
				}
			}
		}
		return null;
	}
}
复制代码

 

 

测试:传入参数value=xx

访问地址:http://localhost/qrcode/getQRCode?value=123

 

生成图片base64

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQAAAACFI5MzAAAAqElEQVR42u2YURJAMAwF3/0vHYy8 pJR/21GjpOsn03YFxVvTT75BdLbjZg87wpJMTNV7mEqkHHeuy5BYiAxrEU6sDI0Rl6Qb8nhwCIu0 u/XmeBRxjuH9FX1BkjrVD4FJLr/qcjrZ5Jy3kWFJvZWs8QESyVUUNjqXlLsjJlMgSX843DOFEleo bXUtQHp3xS1TJinvzTUsjFRJVCYPMnGlOmeKJP8/oQ+TDVt3TF1nTP0RAAAAAElFTkSuQmCC
复制代码

复制上边内容必须点击复制按钮或粘贴出来,吧后边自带出来的介绍去掉

将上边的base64码放到浏览器中就可以看到二维码了

扫描的结构是123

 

 

 

 

 

文章分类
后端
文章标签