【问题标题】:save and display image captured from input type=file保存并显示从输入类型=文件捕获的图像
【发布时间】:2017-04-07 13:46:44
【问题描述】:

我有一个具有从可用相机捕获图像 功能的网页。对于网络版本,它只是将视频捕获放在画布上。但是对于手机,我使用<input class="btn btn-info" type="file" accept="image/*" id="cameraCapture" capture="camera"> 来拍照。它要求用户使用手机的相机捕获图像或从其文件系统/图库等上传。单击图像后,它只需将图像的名称放在按钮旁边。 有没有办法访问此图像并将其显示在同一页面中。

提前致谢

【问题讨论】:

标签: javascript jquery html input-type-file


【解决方案1】:

您可以使用 FileReader 对象在 JS 中做到这一点。

看看这个答案:preview-an-image-before-it-is-uploaded

希望对你有帮助

【讨论】:

  • 请不要使用答案字段链接到其他问题的答案。一旦你有足够的声望,你就可以添加 cmets,然后你可以将问题标记为重复。
【解决方案2】:
var input = document.querySelector('input[type=file]'); 

input.onchange = function () {
    var file = input.files[0];


    drawOnCanvas(file);   
};

function drawOnCanvas(file) {
    var reader = new FileReader();

    reader.onload = function (e) {
        var dataURL = e.target.result,
                c = document.querySelector('canvas'), 
                ctx = c.getContext('2d'),
                img = new Image();

        img.onload = function() {
            c.width = img.width;
            c.height = img.height;
            ctx.drawImage(img, 0, 0);
        };

        img.src = dataURL;
    };

    reader.readAsDataURL(file);
}

【讨论】:

  • 不知道回答一个 1 年前的问题,除了 URL.createObjectURL 将是比使用 FileReader 更好的选择...
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
猜你喜欢
  • 1970-01-01
  • 2015-08-30
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
相关资源
最近更新 更多