[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> ) } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-12-31 [GraphQL] Use GraphQLNonNull for Required Fields
2016-12-31 [GraphQL] Use Arguments in a GraphQL Query
2016-12-31 [GraphQL] Write a GraphQL Schema in JavaScript
2015-12-31 [React] Extracting Private React Components