(Javascript)使用文件路径创建一个File对象

时间:2013-02-23 06:15:24

标签: javascript file url canvas image

我正在制作基于cordova的应用程序。 是否可以使用filepath生成File对象?

我要做的是拍照并在画布上显示图片。 在android中,它工作正常,但在ios6中,由于ios6上的mega-fixel问题,在画布上绘图时图片已损坏。

然后我在

下面找到了一个插件

https://github.com/stomita/ios-imagefile-megapixel

插件需要File或Blob参数,但我只有文件URL。 如何从文件URL获取文件或Blob对象?

我的来源是

takePicture : function(onSuccess, onError) {
    options = {
        quality : 50,
        destinationType : navigator.camera.DestinationType.FILE_URI, //device returns File URL
        correctOrientation : true
    };
    navigator.camera.getPicture(onSuccess, onError, options);
}
onSuccess : function (photo_src) {
    $('#photo_hidden').attr('src', photo_src);
    setTimeout(function() {
    copyImage('photo_hidden', 'a04_image');
    }, 500);
}
copyImage : function (sourceId, targetId) {
    try{
    var source = document.getElementById(sourceId);
    var source_w = source.width;
    var source_h = source.height;
    var target = document.getElementById(targetId);
    var target_w = target.width;
    var target_h = target.height;
    target_h = Math.round(target_w / source_w * source_h);
    /////////////////////
    // I want to make a File object from source.src HERE!!
    /////////////////////
    //TODO this part will be changed to use plugin
    if (target.getContext) {
    var context = target.getContext('2d');
    context.drawImage(source, 0, 0, source_w, source_h, 0, 0, target_w, target_h);
    }
    //
    }catch (e) {
        console.log(e);
    }
}

如果不可能,那么创建文件或Blob对象的任何想法都是欢迎

提前致谢

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。

没有用户的操作就无法创建File对象。 (需要使用标签)

我查看了插件源代码。

我发现这个论点不一定是文件对象。

可以使用图像对象。