【发布时间】:2021-10-02 23:31:35
【问题描述】:
我正在学习教程并尝试使用网络摄像头作为 jpeg 捕获图像,我想将其上传到 Firebase 存储,它需要我将其作为文件或 blob 上传。当我使用<input type = 'file'> 格式时,我可以上传图像,但我想直接从网络摄像头捕获上传图像。无论如何将jpeg转换为文件或blob?
这是代码
import React, { useState } from 'react'
import Webcam from 'react-webcam'
import firebase from 'firebase'
const WebcamComponent = () => <Webcam/>
const videoConstraints = {
width: 220,
height: 200,
facingMode: 'user'
}
const WebcamCapture = () => {
const webcamRef = React.useRef(null)
const [image, setImage] = useState('')
const capture = React.useCallback(
() => {
const imageSrc = webcamRef.current.getScreenshot()
setImage(imageSrc)
const file = image
var storage = firebase.storage()
storage.ref('FormPhotos/' + file.name).put(file).on('state_changed', alert('success'), alert)
console.log(image)
},
[webcamRef]
)
return (
<div className = 'webcam-container'>
<div className = 'webcam-img'>
{
image == '' ? <Webcam
audio = {false}
height = {200}
ref = {webcamRef}
screenshotFormat = 'image/jpeg'
width = {220}
videoConstraints = {videoConstraints}
/> : <img src = {image}/>}
</div>
<div>
{
image != ''
? <button onClick = {(e) => {
e.preventDefault()
setImage('')
}} className = 'webcam-btn'>
Retake Image
</button>
: <button onClick = {(e) => {
e.preventDefault()
capture()
}}>Capture</button>
}
</div>
</div>
)
}
export default WebcamCapture
【问题讨论】:
标签: javascript reactjs firebase firebase-storage