Java 生成二维码并实现展现和下载

Java 生成二维码并实现展现和下载
防止自己忘了怎么用,记录一下
一、前端
接受后端返回的二维码需要用标签接收
例如:

<img style="width: 100%;height: 100%" src="后端请求路径">

下载

function downloadEWM(){
    var name = "下载文件的名称";
    //调用生成二维码的后台代码
    var imgsrc = "request/showQrCodeImg";
    var image = new Image();
   // 解决跨域 Canvas 污染问题
   image.setAttribute("crossOrigin", "anonymous");
   image.onload = function() {
   var canvas = document.createElement("canvas");
   canvas.width = image.width;
   canvas.height = image.height;
   var context = canvas.getContext("2d");
   context.drawImage(image, 0, 0, image.width, image.height);
   var url = canvas.toDataURL("image/jpg");
   //得到图片的base64编码数据
   var a = document.createElement("a");
   // 生成一个a元素
   var event = new MouseEvent("click");
   // 创建一个单击事件
   a.download = name || "photo"; // 设置图片名称
   a.href = url;
   // 将生成的URL设置为a.href属性
   a.dispatchEvent(event);
   // 触发a的单击事件
   };
   image.src = imgsrc;
}
<button type="button" onclick="downloadEWM()">下载</button>

二、后端
1.maven项目引用依赖
我这边使用的是谷歌的

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

2.首先写一下工具类

public class QRCodeUtil {
/**
 *  生成二维码
 *  @param content 二维码的内容
 *  @return BitMatrix对象
 *  */
public static BitMatrix createCode(String content) throws IOException {
   //二维码的宽高
   int width = 200;
   int height = 200;
   //其他参数,如字符集编码
   Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
   hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
   //容错级别为H
   hints.put(EncodeHintType.ERROR_CORRECTION , ErrorCorrectionLevel.H);
   //白边的宽度,可取0~4
   hints.put(EncodeHintType.MARGIN , 0);
   BitMatrix bitMatrix = null;        try {
      //生成矩阵,因为我的业务场景传来的是编码之后的URL,所以先解码
      bitMatrix = new MultiFormatWriter().encode(content,
      BarcodeFormat.QR_CODE, width, height, hints);
      bitMatrix = deleteWhite(bitMatrix);
   } catch (WriterException e) {
      e.printStackTrace();
   }
   return bitMatrix;
}
/**
 *  删除生成的二维码周围的白边,根据审美决定是否删除
 *  @param matrix BitMatrix对象
 *  @return BitMatrix对象
 * * */
private static BitMatrix deleteWhite(BitMatrix matrix) {
   int[] rec = matrix.getEnclosingRectangle();
   int resWidth = rec[2] + 1;
   int resHeight = rec[3] + 1;
   BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
   resMatrix.clear();
   for (int i = 0; i < resWidth; i++) {
      for (int j = 0; j < resHeight; j++) {
         if (matrix.get(i + rec[0], j + rec[1]))
            resMatrix.set(i, j);
      }
   }
   return resMatrix;
}
}

controller使用

@RequestMapping(value = "request/showQrCodeImg", method = RequestMethod.GET)
@ResponseBody
public void showQrCodeImg(HttpServletResponse response)throws IOException{
   //1.配置二维码生成的内容
  String content ="www.baidu.com";
  //2.生成二维码
  BitMatrix bitMatrix = QRCodeUtil.createCode(content);
  //3.以流的形式输出到前端
  MatrixToImageWriter.writeToStream(bitMatrix , "jpg" , stream);

   }

}

你可能感兴趣的:(开发随笔)