Cordova各個插件使用介紹系列(四)—canvas2ImagePlugin保存二維碼到手機本地

詳情鏈接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-4-canvas2imageplugin/
在前面幾篇文章中簡單寫了一下,在項目中怎麼實現掃描的功能和將信息轉化爲二維碼的功能,現在來介紹一下怎麼將生成的二維碼保存到手機的本地,這樣關於二維碼的內容基本上就全面了,好開心~~!

同樣的,我還是想說,首先我這個是做基於ionic+ngCordova+Anjularjs的項目,所以,希望大家在看之前已經瞭解了這三塊內容了,不然,可能看起來會有難度的。

一、下載相關的插件的命令:

 

cordova plugin add https://github.com/devgeeks/Canvas2ImagePlugin.git

  

二、HTML代碼:

 
<div class="col text-center">
    <span>(二維碼)</span>

    <div class="cro">
        <div id="Qrcode">
            <div class="cro_left_top"></div>
            <div class="cro_right_top"></div>
            <div class="cro_left_bottom"></div>
            <div class="cro_right_bottom"></div>
        </div>
        <button class="button button-positive"
                ng-click="saveImageQrcode()">保存到手機
        </button>
    </div>
</div>

  

三、CSS代碼,根據UI實現瞭如下界面的CSS代碼:

<style type="text/css">
    .cro {
        width: 300px;
        height: 360px;
        position: relative;
        text-align: center;
        margin: auto;
        background: white;
    }

    .cro_left_top, .cro_right_top, .cro_left_bottom, .cro_right_bottom {
        position: absolute;
        width: 20px;
        height: 20px;
        z-index: 1;
        background: #212A27;
    }

    .cro_left_top {
        top: -1px;
        left: -1px;
        border-radius: 0px 0px 20px 0px;
    }

    .cro_right_top {
        top: -1px;
        right: -1px;
        border-radius: 0px 0px 0px 20px;
    }

    .cro_left_bottom {
        left: -1px;
        bottom: -1px;
        border-radius: 0px 20px 0px 0px;
    }

    .cro_right_bottom {
        right: -1px;
        bottom: -1px;
        border-radius: 20px 0px 0px 0px;
    }
</style>

 

四、JS代碼如下:

var qrcode = new QRCode(document.getElementById("Qrcode"), {
    width: 200,
    height: 200
});
qrcode.makeCode("123");

var a = document.getElementById("Qrcode");
var canvas = a.children[4];
canvas.id = "myCanvas";
$scope.saveImage = canvas.toDataURL();

//調用保存二維碼圖片的函數
$scope.saveImageQrcode = function () {
    console.log(window.canvas2ImagePlugin);
    window.canvas2ImagePlugin.saveImageDataToLibrary(function (msg) {
            console.log(msg);
            $rootScope.alert('圖片已保存');
        },
        function (err) {
            console.log(err);
        },
        document.getElementById('myCanvas')
    )
};

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章