二维码图片生成-及前台显示

    @GetMapping("/orCode")
    public String orCode(Model model){
        /*
         * 图片的宽度和高度
         */
        int width = 300;
        int height = 300;
        // 图片的格式
        String format = "jpg";
        // 定义二维码的参数
        HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        // 定义字符集编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 纠错的等级 L > M > Q > H 纠错的能力越高可存储的越少,一般使用M
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 设置图片边距
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            // 最终生成 参数列表 (1.内容 2.格式 3.宽度 4.高度 5.二维码参数) //1.内容如果是个超链接扫后直接跳转
            BitMatrix bitMatrix = new MultiFormatWriter().encode("https://www.baidu.com/", BarcodeFormat.QR_CODE, width, height, hints);
            // 写入到本地
//            String uuid=UUID.randomUUID().toString().replaceAll("-","");
//            log.info("!!!!!:{}",uuid);
//            Path file = new File("C:\\Users\\jiang\\Desktop\\"+uuid+".jpg").toPath();
//            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            //写到页面对应的img以BASE64
            MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig();
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig);
            ByteArrayOutputStream os = new ByteArrayOutputStream();//新建流。
            ImageIO.write(image, format, os);//利用ImageIO类提供的write方法,将bi以png图片的数据模式写入流。
            byte b[] = os.toByteArray();//从流中获取数据数组。
            String str = new BASE64Encoder().encode(b);//base64
            model.addAttribute("srcUrl","data:image/jpg;base64,"+str);//前端img的src识别需要前缀data:image/jpg;base64,
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "test";//此test为页面
    }

test.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>[[${srcUrl}]]</div>
<img th:src='${srcUrl}'/>
</body>
</html>

解析二维码

try (InputStream is = new FileInputStream("C:\\Users\\jiang\\Desktop\\捕获.PNG");){
    BufferedImage image = ImageIO.read(is);
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(source));
    HashMap<DecodeHintType, String> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8");// 解码设置编码方式为:utf-8
    hints.put(DecodeHintType.TRY_HARDER, "true");//优化精度
    hints.put(DecodeHintType.PURE_BARCODE, "true");//复杂模式,开启PURE_BARCODE模式
    Result result = new MultiFormatReader().decode(bb, hints);
    System.out.println("二维码格式类型:" + result.getBarcodeFormat());
    System.out.println("二维码文本内容:" + result.getText());
} catch (IOException e) {
    e.printStackTrace();
} catch (NotFoundException e) {
    e.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值