What is react-webcam?
The react-webcam package is a React component that allows you to capture images and videos from your webcam. It provides a simple interface for accessing the webcam and capturing media, making it useful for applications that require user media input.
What are react-webcam's main functionalities?
Capture Image
This feature allows you to capture an image from the webcam. The `getScreenshot` method is used to take a snapshot and return it as a base64-encoded image string.
import React, { useRef, useCallback } from 'react';
import Webcam from 'react-webcam';
const WebcamCapture = () => {
const webcamRef = useRef(null);
const capture = useCallback(() => {
const imageSrc = webcamRef.current.getScreenshot();
console.log(imageSrc);
}, [webcamRef]);
return (
<div>
<Webcam
audio={false}
ref={webcamRef}
screenshotFormat="image/jpeg"
/>
<button onClick={capture}>Capture photo</button>
</div>
);
};
export default WebcamCapture;
Capture Video
This feature allows you to capture video from the webcam. The `MediaRecorder` API is used to record the video stream and save it as a Blob, which can then be played back using a video element.
import React, { useRef, useState } from 'react';
import Webcam from 'react-webcam';
const WebcamVideoCapture = () => {
const webcamRef = useRef(null);
const [recording, setRecording] = useState(false);
const [mediaRecorder, setMediaRecorder] = useState(null);
const [videoUrl, setVideoUrl] = useState(null);
const startRecording = () => {
const stream = webcamRef.current.stream;
const mediaRecorder = new MediaRecorder(stream);
setMediaRecorder(mediaRecorder);
mediaRecorder.start();
setRecording(true);
mediaRecorder.ondataavailable = (event) => {
const videoBlob = new Blob([event.data], { type: 'video/webm' });
const videoUrl = URL.createObjectURL(videoBlob);
setVideoUrl(videoUrl);
};
};
const stopRecording = () => {
mediaRecorder.stop();
setRecording(false);
};
return (
<div>
<Webcam audio={true} ref={webcamRef} />
{recording ? (
<button onClick={stopRecording}>Stop Recording</button>
) : (
<button onClick={startRecording}>Start Recording</button>
)}
{videoUrl && <video src={videoUrl} controls />}
</div>
);
};
export default WebcamVideoCapture;
Mirror Mode
This feature allows you to enable mirror mode for the webcam feed. Setting the `mirrored` prop to `true` flips the video horizontally, which can be useful for applications like video conferencing.
import React from 'react';
import Webcam from 'react-webcam';
const WebcamMirror = () => {
return (
<div>
<Webcam
audio={false}
screenshotFormat="image/jpeg"
mirrored={true}
/>
</div>
);
};
export default WebcamMirror;
Other packages similar to react-webcam
react-camera-pro
react-camera-pro is another React component for capturing images and videos from the webcam. It offers similar functionalities to react-webcam but with a more modern API and additional features like camera switching and video recording with pause/resume capabilities.
react-html5-camera-photo
react-html5-camera-photo is a React component that provides a simple interface for capturing photos using the HTML5 getUserMedia API. It is more focused on photo capture and offers fewer features compared to react-webcam, but it is lightweight and easy to use.
react-webcam-onfido
react-webcam-onfido is a React component specifically designed for capturing webcam images for identity verification purposes. It offers additional features like face detection and image quality checks, making it more specialized compared to the general-purpose react-webcam.
react-webcam
Webcam component for React. See this
for browser compatibility.
Installation
npm install react-webcam
Demo
https://www.mozmorris.com/react-webcam/examples/ (if demo doesn't work, check browser compatibility and verify browser is using https)
Usage
import React from 'react';
import Webcam from 'react-webcam';
class Component extends React.Component {
render() {
return <Webcam/>;
}
}
Props
prop | type | default | notes |
---|
className | string | '' | CSS class of video element |
audio | boolean | true | enable/disable audio |
height | number | 480 | height of video element |
width | number | 640 | width of video element |
style | object | | style prop passed to video element |
screenshotFormat | string | 'image/webp' | format of screenshot |
onUserMedia | function | noop | callback when component receives a media stream |
audioSource | string | | an array or single ConstrainDOMString(s) specifying the device id |
videoSource | string | | an array or single ConstrainDOMString(s) specifying the device id |
Methods
getScreenshot
- Returns a base64 encoded string of the current webcam image. Example:
class WebcamCapture extends React.Component {
setRef = (webcam) => {
this.webcam = webcam;
}
capture = () => {
const imageSrc = this.webcam.getScreenshot();
};
render() {
return (
<div>
<Webcam
audio={false}
height={350}
ref={this.setRef}
screenshotFormat="image/jpeg"
width={350}
/>
<button onClick={this.capture}>Capture photo</button>
</div>
);
}
}
License
MIT
Credits
Many thanks to @cezary for his work on this component.