1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        太強大了 | 一鍵生成,太強大了……

        共 2224字,需瀏覽 5分鐘

         ·

        2020-12-30 08:22

        bb1777b10b532aa7972d97b694d5eddb.webp

        hi!我是小小,今天是本周的第二篇,本篇將會著重講解關于Java生成二維碼的功能

        效果圖相關依賴


        com.google.zxing
        javase
        3.3.0

        相關工具類
        package com.bennyrhys.mall.util;

        import com.google.zxing.BarcodeFormat;
        import com.google.zxing.WriterException;
        import com.google.zxing.client.j2se.MatrixToImageWriter;
        import com.google.zxing.common.BitMatrix;
        import com.google.zxing.qrcode.QRCodeWriter;

        import java.io.IOException;
        import java.nio.file.FileSystems;
        import java.nio.file.Path;

        /**
        * 描述:生成二維碼工具
        */
        public class QRCodeGenerator {


        public static void generateQRCodeImage(String text, int width, int height, String filePath)
        throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        }

        public static void main(String[] args) {
        try {
        generateQRCodeImage("Hello World", 350, 350, "E:/JAVA/mall/src/main/resources/images/QRTest.png");
        } catch (WriterException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }


        添加service

        /**
        * 生成二維碼
        * 圖片可解析出訪問的支付對應訂單號的支付連接
        * @param orderNo 訂單號
        * @return 返回圖片地址
        */
        @Override
        public String qrcode(String orderNo) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        String address = ip + ":" + request.getLocalPort();
        String payUrl = "http://" + address + "/pay?orderNo=" + orderNo;
        try {
        QRCodeGenerator.generateQRCodeImage(payUrl, 350, 350, Constant.FILE_UPLOAD_PATH + orderNo + ".png");
        } catch (WriterException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        String pngAddress = "http://" + address + "/images-dev/" + orderNo + ".png";
        return pngAddress;
        }
        關于作者

        我是小小,雙魚座的程序猿,我們下期再見哦~


        END




        「 往期文章 」


        去哪里了?幾部關于IT的劇推薦

        Eclipse 官宣,要干掉 VS Code,你怎么看?

        建議 | 在中國不是程序猿,不建議你用序列化!




        掃描二維碼

        獲取更多精彩

        小明菜市場



        來源:網絡(侵刪)圖片來源:網絡(侵刪)



        瀏覽 72
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产毛片乡下农村妇女 | 岛国Av在线成人 俺去也激情 | 性菲律宾xX×Ⅹ69娇小 | 国产精品久久久D88.XYZ | 黄色片快播 | 91狠狠综合 | 韩国三级hd高清中字 | 4438免费做爱视频 | 做爰免费视频网站 | 黄色片网站 |