[PWA] Access the Camera in a PWA built with React
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.
class Profile extends Component {
state = {
image: null,
supportsCamera: 'mediaDevices' in navigator
}
changeImage = (e) => {
this.setState({
image: URL.createObjectURL(e.target.files[0])
})
}
startChangeImage = () => {
this.setState({ enableCamera: !this.state.enableCamera })
}
takeImage = () => {
this._canvas.width = this._video.videoWidth
this._canvas.height = this._video.videoHeight
this._canvas.getContext('2d').drawImage(
this._video,
0, 0,
this._video.videoWidth,
this._video.videoHeight
)
this._video.srcObject.getVideoTracks().forEach(track => {
track.stop()
})
this.setState({
image: this._canvas.toDataURL(),
enableCamera: false
})
}
render() {
return (
<div>
<nav className="navbar navbar-light bg-light">
<span className="navbar-brand mb-0 h1">
<Link to="/">
<img src={Back} alt="logo" style={{ height: 30 }} />
</Link>
Profile
</span>
</nav>
<div style={{ textAlign: 'center' }}>
<img
src={this.state.image || GreyProfile} alt="profile"
style={{ height: 200, marginTop: 50 }}
/>
<p style={{ color: '#888', fontSize: 20 }}>username</p>
{
this.state.enableCamera &&
<div>
<video
ref={c => {
this._video = c
if(this._video) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => this._video.srcObject = stream)
}
}}
controls={false} autoPlay
style={{ width: '100%', maxWidth: 300 }}
></video>
<br />
<button
onClick={this.takeImage}
>Take Image</button>
<canvas
ref={c => this._canvas = c}
style={{ display: 'none' }}
/>
</div>
}
<br />
{
this.state.supportsCamera &&
<button
onClick={this.startChangeImage}
>
Toggle Camera
</button>
}
</div>
</div>
)
}
}
[PWA] Access the Camera in a PWA built with React的更多相关文章
- [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 ...
- 前端性能和加载体验优化实践(附:PWA、离线包、内存优化、预渲染)
一.背景:页面为何会卡? 1.1 等待时间长(性能) 项目本身包/第三方脚本比较大. JavaScript 执行阻塞页面加载. 图片体积大且多. 特别是对于首屏资源加载中的白屏时间,用户等待的时间就越 ...
- Team Foundation Server (TFS)与Project Server集成,使用DNS(友好地址)地址注册PWA
问题描述: 当Team Foundation Server(TFS 2010/2012/2013)与Project Server高可用性的环境集成时,必然会使用Project Server (PWA) ...
- PWA(Progressive Web App)入门系列:(一)PWA简单介绍
前言 PWA做为一门Google推出的WEB端的新技术,长处不言而喻.但眼下对于相关方面的知识不是非常丰富.这里我推出一下这方面的新手教程系列.提供PWA方面学习. 什么是PWA PWA全称Progr ...
- PWA web应用模型
2018年的第一篇博客,最近都去挤图书馆了,希望新年新气象... 简介 PWA 是一门Google推出的web前端新技术,全称是Progressive Web App,是Google在2015年提出, ...
- 一个知乎日报pwa
前几天写了一篇文章关于如何实现一个简单版的pwa应用,端午撸了一个简易版知乎日报pwa. 关于如何写一个pwa,这里就不多介绍了,请移步这里.应用使用vue+vuex+axios,API这里,这里做了 ...
- PWA 基础学习
1.PWA 是什么? PWA 是 Progressive Web App 的缩写,从字面翻译过来就是 渐进式 Web App. 渐进式翻译过来就是慢慢的,不是一蹴而就的.这里的指的是 Wab App ...
- PWA入门:手把手教你制作一个PWA应用
摘要: PWA图文教程 原文:PWA入门:手把手教你制作一个PWA应用 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 简介 Web前端的同学是否想过学习app开发,以弥补自 ...
- PWA 推送实践
PWA 推送实践 最近公司内录任务的系统总是忘记录任务,而那个系统又没有通知,所以想要实现一个浏览器的通知功能,免得自己忘记录入任务. 前端实现通知的几种方式 想要实现通知,我们就需要有个客户端,对于 ...
随机推荐
- ftp无法连接的原因
1.需求 记录碰到的ftp无法连接的原因 2.解决方案 .确认ftp服务开启. .确认21端口没有被占用. .确认有目录的执行权限. .确认配置文件中的目录读写权限正确. .关闭SELinux 修改/ ...
- MFC 屏幕截图(libjpeg bmp转jpg)
项目中需要用到的功能. void Screenshot() { CDC *pDC; pDC = CDC::FromHandle(GetDC(GetDesktopWindow())); if(pDC = ...
- 7月14号day6总结
今天学习过程和总结 IOC和DIO IOC相当于一个容器,在容器中加注解.接口存在意义依赖注入.4个注解都行,依赖注入只能发生在IOC容器里, pring IOC 容器可以管理Bean 的生命周期,S ...
- 解决jquery与zepto等其它库冲突兼容的问题
解决jquery与zepto等其它库冲突兼容的问题;(function ($) { }) (jQuery); ;(function ($) { }) (Zepto); 在Bootstrap ...
- 华为上机测试题(及格分数线-java)
PS:自己写的,自测试OK,供大家参考. /* 描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:(1) 及格线是10的倍数:(2) 保证至少有60%的学生及格:(3) 如果所有 ...
- 开始gentoo之旅
买了一台i7笔记本,dell 15游戏匣7559,老笔记本dell n4030装了gentoo. 用了两天,先用着,再慢慢学内核编译.
- python每日一类(2):platform
根据官方文档的解释(https://docs.python.org/3.5/library/platform.html#module-platform): 学习其他人的代码如下: # python p ...
- 将ros中suscriber和publisher写入class中
相比于笨拙的全局变量和全局函数,将suscriber和publisher成一个class,形式更加简洁和容易管理,一个节点就是一个类 参考资料 http://answers.ros.org/quest ...
- CSS浮动属性,知道原理就很简单,灵活控制块级元素在一行内显示
在页面布局中,有两个非常常用的CSS属性.它们巧妙的控制着块级元素们之间的位置,灵活的让块级元素在一行内显示或者另起一行.说到这里,相信大家已经猜出来了,这两个属性就是控制块级元素浮动的属性.整个页面 ...
- 解析Java的volatile关键字
众所周知,无限制下多线程操作共享变量是危险的,为了保证线程安全语义,一般的建议是在操作共享变量时加锁,比方说在用synchronized关键字修饰的方法内读写共享变量. 但是synchronized开 ...