It's possible to access some, but not all, of the native device features from a PWA. One that we canaccess, is the camera and camera roll. We'll use two different methods to allow users to upload images to our application.

First, we'll add a file input that accepts images. We can specify a capture param, which will tell PWAs on mobile devices to prompt the user to take a picture with their camera.

Next, we'll access the camera feed directly by rendering a video tag, and accessing navigator.mediaDevices.getUserMedia directly - which will allow us to show a live video feed from the camera. Then, we can use a canvas tag to grab a frame from that video, and render the image.

  1. class Profile extends Component {
  2.  
  3. state = {
  4. image: null,
  5. supportsCamera: 'mediaDevices' in navigator
  6. }
  7.  
  8. changeImage = (e) => {
  9. this.setState({
  10. image: URL.createObjectURL(e.target.files[0])
  11. })
  12. }
  13.  
  14. startChangeImage = () => {
  15. this.setState({ enableCamera: !this.state.enableCamera })
  16. }
  17.  
  18. takeImage = () => {
  19. this._canvas.width = this._video.videoWidth
  20. this._canvas.height = this._video.videoHeight
  21.  
  22. this._canvas.getContext('2d').drawImage(
  23. this._video,
  24. 0, 0,
  25. this._video.videoWidth,
  26. this._video.videoHeight
  27. )
  28.  
  29. this._video.srcObject.getVideoTracks().forEach(track => {
  30. track.stop()
  31. })
  32.  
  33. this.setState({
  34. image: this._canvas.toDataURL(),
  35. enableCamera: false
  36. })
  37. }
  38.  
  39. render() {
  40. return (
  41. <div>
  42. <nav className="navbar navbar-light bg-light">
  43. <span className="navbar-brand mb-0 h1">
  44. <Link to="/">
  45. <img src={Back} alt="logo" style={{ height: 30 }} />
  46. </Link>
  47. Profile
  48. </span>
  49. </nav>
  50.  
  51. <div style={{ textAlign: 'center' }}>
  52. <img
  53. src={this.state.image || GreyProfile} alt="profile"
  54. style={{ height: 200, marginTop: 50 }}
  55. />
  56. <p style={{ color: '#888', fontSize: 20 }}>username</p>
  57.  
  58. {
  59. this.state.enableCamera &&
  60. <div>
  61. <video
  62. ref={c => {
  63. this._video = c
  64. if(this._video) {
  65. navigator.mediaDevices.getUserMedia({ video: true })
  66. .then(stream => this._video.srcObject = stream)
  67. }
  68. }}
  69. controls={false} autoPlay
  70. style={{ width: '100%', maxWidth: 300 }}
  71. ></video>
  72.  
  73. <br />
  74.  
  75. <button
  76. onClick={this.takeImage}
  77. >Take Image</button>
  78.  
  79. <canvas
  80. ref={c => this._canvas = c}
  81. style={{ display: 'none' }}
  82. />
  83.  
  84. </div>
  85. }
  86.  
  87. <br />
  88. {
  89. this.state.supportsCamera &&
  90. <button
  91. onClick={this.startChangeImage}
  92. >
  93. Toggle Camera
  94. </button>
  95. }
  96. </div>
  97.  
  98. </div>
  99. )
  100. }
  101.  
  102. }

Video, Code

[PWA] Access the Camera in a PWA built with React的更多相关文章

  1. [PWA] Add Push Notifications to a PWA with React in Chrome and on Android

    On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'l ...

  2. 前端性能和加载体验优化实践(附:PWA、离线包、内存优化、预渲染)

    一.背景:页面为何会卡? 1.1 等待时间长(性能) 项目本身包/第三方脚本比较大. JavaScript 执行阻塞页面加载. 图片体积大且多. 特别是对于首屏资源加载中的白屏时间,用户等待的时间就越 ...

  3. Team Foundation Server (TFS)与Project Server集成,使用DNS(友好地址)地址注册PWA

    问题描述: 当Team Foundation Server(TFS 2010/2012/2013)与Project Server高可用性的环境集成时,必然会使用Project Server (PWA) ...

  4. PWA(Progressive Web App)入门系列:(一)PWA简单介绍

    前言 PWA做为一门Google推出的WEB端的新技术,长处不言而喻.但眼下对于相关方面的知识不是非常丰富.这里我推出一下这方面的新手教程系列.提供PWA方面学习. 什么是PWA PWA全称Progr ...

  5. PWA web应用模型

    2018年的第一篇博客,最近都去挤图书馆了,希望新年新气象... 简介 PWA 是一门Google推出的web前端新技术,全称是Progressive Web App,是Google在2015年提出, ...

  6. 一个知乎日报pwa

    前几天写了一篇文章关于如何实现一个简单版的pwa应用,端午撸了一个简易版知乎日报pwa. 关于如何写一个pwa,这里就不多介绍了,请移步这里.应用使用vue+vuex+axios,API这里,这里做了 ...

  7. PWA 基础学习

    1.PWA 是什么? PWA 是 Progressive Web App 的缩写,从字面翻译过来就是 渐进式 Web App. 渐进式翻译过来就是慢慢的,不是一蹴而就的.这里的指的是 Wab App ...

  8. PWA入门:手把手教你制作一个PWA应用

    摘要: PWA图文教程 原文:PWA入门:手把手教你制作一个PWA应用 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 简介 Web前端的同学是否想过学习app开发,以弥补自 ...

  9. PWA 推送实践

    PWA 推送实践 最近公司内录任务的系统总是忘记录任务,而那个系统又没有通知,所以想要实现一个浏览器的通知功能,免得自己忘记录入任务. 前端实现通知的几种方式 想要实现通知,我们就需要有个客户端,对于 ...

随机推荐

  1. SpringBoot设置事务管理

    关于事务就不介绍了,前面在研究spring的时候就已经研究过了,参考:https://www.cnblogs.com/qlqwjy/p/7296493.html 这里直接研究springboot中事务 ...

  2. Mysql 连接池

    数据库连接池的作用: 数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个:释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接 ...

  3. log4j2配置文件动态指定日志文件名称

    按照习惯性思维,log4j2中xml中取系统属性应该和log4j一样,但是结果却并不是我们想的这样,存在一定的差别,log4j中的配置可参考 log4j配置文件动态指定日志文件名称 . 现在就来看看到 ...

  4. ectouch 微信支付成功后订单状态未改变的解决办法 (转载)

    原文地址: 微信支付支付成功后,返回到mobile/wx_native_callback.php 之前代码 define('IN_ECS', true); require(dirname(__FILE ...

  5. 【linux高级程序设计】(第十三章)Linux Socket网络编程基础 3

    使用之前的函数实现的简单聊天程序 TCP协议 双方实时发送/接收消息 实现后的问题: 可能是我虚拟机的IP地址配得有问题吧.在一台电脑上面开两个终端,用127.0.0.1的IP收发可以互通.但是两个虚 ...

  6. tkinter 表格

    import tkinter from tkinter import ttk root = tkinter.Tk() tree = ttk.Treeview(root, show="head ...

  7. Centos的APK解包打包签名

    http://www.v5b7.com/other/apk.html vi /etc/profile PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:/u ...

  8. 部署站点支持Https访问的方法

    1.申请公钥和私钥,放到服务器 2.编辑default配置文件 改为 加上证书路径 ps:泛域名支持admin.xxx.com.demo.xxx.com等等,而免费的Let's Encrypt仅支持w ...

  9. 51nod 1137.矩阵乘法-矩阵乘法

    1137 矩阵乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   给出2个N * N的矩阵M1和M2,输出2个矩阵相乘后的结果.   Input 第1行:1个数N, ...

  10. C++米勒拉宾算法模板

    //我也忘了从哪找来的板子,不过对于2^63级的数据请考虑使用java内置的米勒拉宾算法. 1 #include <iostream> #include <string> #i ...