为什么我必须在Base64中用HTML编码QR码才能显示QR码?

时间:2019-07-01 15:49:29

标签: java spring-boot qr-code zxing

我试图在Web浏览器中显示QR码,但是我注意到,如果我不以png格式编码QR码,则Web浏览器将不会显示照片,而是显示灰色图像。

此链接中的代码正好在此处。 Node

https://www.callicoder.com/generate-qr-code-in-java-using-zxing/ html文件

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>This is your QR Code</title>
</head>
<body>
  <img src="/Users/John/Desktop/MyQRCode.png" alt="QR Code">
  </br>
  <img th:src="|data:image/png;base64,${dataUrl}|" />
</body>
</html>

控制器

private static final String QR_CODE_IMAGE_PATH = "/Users/John/Desktop/MyQRCode.png";

@GetMapping(path = "/qrCode")
public String qrCode(Model model) {

    try {
        generateQRCodeImage("www.facebook.com", 350, 350, QR_CODE_IMAGE_PATH);
    } catch (WriterException e) {
        System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
    }

    try {
        byte[] qr = getQRCodeImage("www.facebook.com", 350, 350);
        String encodedStr64 = Base64.encodeBase64String(qr);
        model.addAttribute("dataUrl", encodedStr64);
        model.addAttribute("data", qr);
    } catch (WriterException e) {
        System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
    }
}

0 个答案:

没有答案