JAVA通過JS調用生成二維碼

此文是通過QRcode來生成二維碼。
需要引入的jar包是 Qrcode_swetake.jar qrcode.jar

很多時候需要通過前端傳入一些參數到後臺,然後後臺生成這些參數的二維碼圖片發送到前端展示在div中。

前端代碼

//這個是需要展示二維碼的div
<div class="QRcode">

</div>
<button type="button" οnclick="showQRCode()">生成二維碼</button>

function showQRCode() {
    //obj爲需要傳遞的參數的內容。
    var obj = "12";
    //爲div畫入img圖片。src爲地址。會調用action,然後生成圖片。
    $(".QRcode").html("<img src='showQRCode?obj=" + obj + "'/>");
}

JAVA代碼

    //生成圖片的action
    @RequestMapping(value="/showQRCode")
    public void showQRCode(HttpServletResponse response, String obj) throws Exception{
        //生成BufferedImage用來存放二維碼
        BufferedImage qrcodeImg = CreateQRCodeImg.getQrcodeImg("http://localhost:8080/show.action?qrcodeId="+obj);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        ImageIO.write(qrcodeImg, "gif", baos);  
        ByteArrayInputStream is = new ByteArrayInputStream(baos.toByteArray());  

        int size =is.available(); //得到文件大小   
        byte data[]=new byte[size];   
        is.read(data);  //讀數據   
        is.close();   
        response.setContentType("image/gif"); //設置返回的文件類型   
        OutputStream os = response.getOutputStream();  
        os.write(data);  
        os.flush();  
        os.close();    
    }

    //生成二維碼的方法
    public static BufferedImage getQrcodeImg(String content) {
        int width = 140;
        int height = 140;
        // 實例化Qrcode
        Qrcode qrcode = new Qrcode();
        // 設置二維碼的排錯率L(7%) M(15%) Q(25%) H(35%)
        qrcode.setQrcodeErrorCorrect('M');
        qrcode.setQrcodeEncodeMode('B');
        // 設置二維碼尺寸(1~49)
        qrcode.setQrcodeVersion(7);
        // 設置圖片尺寸
        BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

        // 繪製二維碼圖片
        Graphics2D gs = bufImg.createGraphics();
        // 設置二維碼背景顏色
        gs.setBackground(Color.WHITE);
        // 創建一個矩形區域
        gs.clearRect(0, 0, width, height);
        // 設置二維碼的圖片顏色值 黑色
        gs.setColor(Color.BLACK);

        // 獲取內容的字節數組,設置編碼集
        try {
            byte[] contentBytes = content.getBytes("utf-8");
            int pixoff = 2;
            // 輸出二維碼
            if (contentBytes.length > 0 && contentBytes.length < 125) {
                boolean[][] codeOut = qrcode.calQrcode(contentBytes);
                for (int i = 0; i < codeOut.length; i++) {
                    for (int j = 0; j < codeOut.length; j++) {
                        if (codeOut[j][i]) {
                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                        }
                    }
                }
            }
            gs.dispose();
            bufImg.flush();
            return bufImg;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

上面 getQrcodeImg() 方法中,如果傳入的參數 content 長度過大的話,就需要改變相應的 qrcode.setQrcodeVersion(7);這個方法的參數。參數越大,能生成的二維碼字符串長度越長。

但是個人覺得如果字符串過長的話,生成的二維碼也是不好看,個人建議,不如創建一張sql表,用來存字符串內容,二維碼就存簡單的url和sql表的 id 即可。

發佈了62 篇原創文章 · 獲贊 80 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章