javascript - 从 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。
谢谢
解决方案
使用 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 网络应用程序中测试了上述内容。
推荐阅读
- openedge - Lighttpd 和 CGI 超时问题
- regex - 正则表达式匹配某些符号前后的空格
- javascript - 为什么我的 CSS 变量不会影响关键帧动画?
- java - 在数组中查找 MAX 个偶数和奇数
- glsl - 刷新着色器,试图让它在处理中刷新
- python - 使用 Firefox 的 Tensorboard 在 Google Colab 中出现错误 403
- swift - 如何在 swift Eureka Form 中为具有多个文本字段的自定义单元格创建验证规则?
- r - 如何在 R 中使用 Plotly 将平均值添加到 BoxPlot?
- sql - 从列到这些列的串联的最佳连接?
- c++ - Visual Studio C++ 后生成命令不起作用