【问题标题】:Save image locally from img tag with PhoneGap使用 PhoneGap 从 img 标签本地保存图像
【发布时间】:2014-05-02 06:41:33
【问题描述】:

我对 phonegap 很陌生。

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

如何将图像从 img 标签(包含经过处理的图像)本地保存到设备存储中?

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

【问题讨论】:

    标签: cordova android-camera


    【解决方案1】:

    我可以想办法,但是你需要自己去搜索一下,我可以给你一个大概的思路。

    想法是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 )
    

    将此图像字符串发送到本机代码。如何!在谷歌中搜索。有一种方法叫做 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

    并使用本机代码保存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多