【问题标题】:(Javascript) make a File object with file path(Javascript) 用文件路径创建一个 File 对象
【发布时间】:2013-02-08 20:53:46
【问题描述】:

我正在制作一个基于cordova 的应用程序。 是否可以使用文件路径制作 File 对象?

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

然后我在下面找到了一个插件

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

插件需要文件或 Blob 参数,但我只有文件 URL。 如何从文件 URL 中获取 File 或 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 对象的任何想法

提前致谢

【问题讨论】:

    标签: javascript file url canvas image


    【解决方案1】:

    我自己解决了这个问题。

    如果没有用户的操作,就无法创建 File 对象。 (需要使用<input type='file'>标签)

    我查看了插件源代码。

    我发现参数不一定是文件对象。

    可以使用图像对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多