首页 > 解决方案 > 从 Google Script 网站访问相机

问题描述

我在从Google 脚本网站访问相机时遇到问题。

我努力了

<input type="file" accept="image/*" id="file-input">

<input type="file" accept="image/*;capture=camera">

但两者都打开文件管理器来选择文件而不是要求相机许可

有没有办法使用 Google Script webapp 捕获图像并上传到谷歌驱动器。

我已经尝试过 Google Picker API,但没有运气,它给了我 origin error

谢谢

标签: javascriptgoogle-apps-script

解决方案


使用 MediaDevices.getUserMedia(constraints) 方法触发授权流程。配置约束参数来描述请求的媒体类型,例如

var constraints = {video: true, audio: true};

添加 <video> 标签以嵌入视频和音频流。getUserMedia() 方法返回一个带有成功和失败处理程序的 Promise,在用户与授权提示交互后得到解决。

<video id="video" width="640" height="480" autoplay></video>

<script>
var video = document.getElementById('video');

navigator.mediaDevices.getUserMedia(constraints)

    .then(function(stream) {
      video.srcObject = stream;
      video.play();
    })
    .catch(function(err) {
      console.log("An error has occured: " + err);
    });
  </script>

有关详细信息,请参阅文档 https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia

我已经在我的 GAS 网络应用程序中测试了上述内容。


推荐阅读