convert canvas to imageURI

徘徊边缘 提交于 2020-01-06 12:51:51

问题


I develop application using Phonegap (Cordova) and I have a problem trying to convert canvas to imageURI and to save it into sdcard.

Here is my code:

function saveCanvasAsImage(imageURI) {

            var gotFileEntry = function(fileEntry) {
                var gotFileSystem = function(fileSystem) {
                    var d = new Date();
                    var n = d.getTime();
                    // copy the file
                    fileEntry.moveTo(fileSystem.root.feelathome, n + ".jpg", null, null);
                };
                // get file system to copy or move image file to
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, fsFail);
                alert("Image Capture Success");
            };
            //resolve file system for image
            window.resolveLocalFileSystemURI(document.getElementById("canvas").toDataURL("image/png").replace(/^data:image\/(png|jpg);base64,/, ""), gotFileEntry, fsFail);

    }
//file system fail
function fsFail(error) {
    alert("failed with error code: " + error.code);
}

and i'm got error failed 5 where i'm wrong?


回答1:


Canvas doesn't work correctly in most versions of android's webview.

To be able to convert correctly a canvas to a file in any version of android, you can use the Canvas2ImagePlugin : https://github.com/devgeeks/Canvas2ImagePlugin



来源:https://stackoverflow.com/questions/23043014/convert-canvas-to-imageuri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!