截图生成图片并保存到相册

    // 保存到相册
    $("#content").on("click", "#savepic", function () {
        $("#waitingupload").removeClass("heisebghid").addClass("heisebg");
        // 调用html2canvas生成截图
        html2canvas($("#orderInfo")[0], {
            allowTaint: true,
            taintTest: false,
            onrendered: function (canvas) {
                canvas.id = "mycanvas";
                //var newImg = document.createElement("img");
                //newImg.crossOrigin = "*";
                //var ctx = canvas.getContext("2d");
                //ctx.drawImage(newImg, 0, 0);

                ////生成base64图片数据
                //var dataUrl = canvas.toDataURL();
                //newImg.src = dataUrl;
                //document.body.appendChild(newImg);

                var dataUrl = canvas.toDataURL("image/png", 1);
                // 调用接口解码,获取图片
                getimgByBase64(dataUrl);
            }
        });

    });
// Base64生成图片
function getimgByBase64(base64code) {
    console.log(base64code);
    $.ajax({
        type: "POST",
        url: configManager.RequstUrl + "api/common/base64decode",
        async: true,
        data: { "base64code": base64code }
    }).done(function (data) {
        if ("success" != data.state) { console.log(data.message); plus.nativeUI.alert("保存失败!"); return; }
        // 保存到相册
        saveGallery(data.url);
    });
}
function saveGallery(url) {
    var dtask = plus.downloader.createDownload(url, {}, function (d, status) {
        // 下载完成
        if (status == 200) {
            console.log(d.filename);
            var filepath = plus.io.convertLocalFileSystemURL(d.filename);
            console.log(filepath);
            // 保存相册
            plus.gallery.save(filepath);
            plus.nativeUI.alert("保存成功");
            // 删除
            plus.io.resolveLocalFileSystemURL(filepath, function (entry) {
                entry.remove(function (e) { }, function (e) { });
            }, function (e) { });

        } else {
            plus.nativeUI.alert("Download failed: " + status);
        }

        $("#waitingupload").removeClass("heisebg").addClass("heisebghid");
    });
    //dtask.addEventListener( "statechanged", onStateChanged, false );
    dtask.start();
}

html2canvas截图js的介绍及下载地址如下:http://www.oschina.net/p/html2canvas

posted @ 2015-07-27 22:13  nd  阅读(2726)  评论(0编辑  收藏  举报