使用PhoneGap从img标签本地保存图像

时间:2014-03-24 18:12:02

标签: cordova android-camera

我对phonegap很新。

我正在使用带有相机功能的3.1版本。 我用camera.getPicture将图像文件保存到设备存储中,但现在我有了一个新任务: 在使用相机功能加载后,我使用canvas标签直接操作图像,等等..

如何将图像从img标签(包含受控图像)本地保存到设备存储?

提前致谢, 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我可以想办法,但你需要搜索它们,我可以给你一个粗略的想法。

Idea是base64图片字符串>发送到本机代码>再次转换为图像>保存它

您可以将图像放入canvas并将图像转换为base64编码的字符串。

function convertImgToBase64(url, callback, outputFormat){
                var canvas = document.createElement('CANVAS');
                var ctx = canvas.getContext('2d');
                var img = new Image;
                img.crossOrigin = 'Anonymous';
                img.onload = function(){
                    canvas.height = img.height;
                    canvas.width = img.width;
                    ctx.drawImage(img,0,0);
                    var dataURL = canvas.toDataURL(outputFormat || 'image/png');
                    callback.call(this, dataURL);
                    // Clean up
                    canvas = null; 
                };
                img.src = url;
            }

convertImgToBase64(imageUrl, function(base64Img){  //base64Img )

将此图片字符串发送到本机代码。怎么样!在Google中搜索。有一种叫appView的方法,或者你可以编写自定义插件。

接下来将图像字符串转换为图像

FileOutputStream fos = null;
try {

if (base64ImageData != null) {
    fos = context.openFileOutput("imageName.png", Context.MODE_PRIVATE);
    byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT);
    fos.write(decodedString);

    fos.flush();
    fos.close();

    }

} catch (Exception e) {

} finally {
    if (fos != null) {
        fos = null;
    }
}

Convert a string in base64 to an image and save the image

并使用本机代码保存。

相关问题