Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
react-webcam
Advanced tools
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.
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;
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 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 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.
Webcam component for React. See this for browser compatibility.
Note: Browsers will throw an error if the page is loaded from insecure origin. I.e. Use https.
npm install react-webcam
https://codepen.io/mozmorris/pen/JLZdoP
import React from "react";
import Webcam from "react-webcam";
const WebcamComponent = () => <Webcam />;
The props here are specific to this component but one can pass any prop to the underlying video tag eg className
or style
prop | type | default | notes |
---|---|---|---|
audio | boolean | true | enable/disable audio |
audioConstraints | object | MediaStreamConstraint(s) for the audio | |
imageSmoothing | boolean | true | pixel smoothing of the screenshot taken å |
minScreenshotHeight | number | min height of screenshot | |
minScreenshotWidth | number | min width of screenshot | |
onUserMedia | function | noop | callback for when component receives a media stream |
onUserMediaError | function | noop | callback for when component can't receive a media stream with MediaStreamError param |
screenshotFormat | string | 'image/webp' | format of screenshot |
screenshotQuality | number | 0.92 | quality of screenshot(0 to 1) |
videoConstraints | object | MediaStreamConstraints(s) for the video |
getScreenshot
- Returns a base64 encoded string of the current webcam image. Example:
const videoConstraints = {
width: 1280,
height: 720,
facingMode: "user"
};
const WebcamCapture = () => {
const webcamRef = React.useRef(null);
const capture = React.useCallback(
() => {
const imageSrc = webcamRef.current.getScreenshot();
},
[webcamRef]
);
return (
<>
<Webcam
audio={false}
height={720}
ref={webcamRef}
screenshotFormat="image/jpeg"
width={1280}
videoConstraints={videoConstraints}
/>
<button onClick={capture}>Capture photo</button>
</>
);
};
class WebcamCapture extends React.Component {
render() {
const videoConstraints = {
facingMode: "user"
};
return <Webcam videoConstraints={videoConstraints} />;
}
}
class WebcamCapture extends React.Component {
render() {
const videoConstraints = {
facingMode: { exact: "environment" }
};
return <Webcam videoConstraints={videoConstraints} />;
}
}
For more information on facingMode
, please see the MDN web docs https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/facingMode
The Webcam component will fail to load when used inside a cross-origin iframe in newer version of Chrome (> 64). In order to overcome this security restriction a special allow
attribute needs to be added to the iframe tag specifying microphone
and camera
as the required permissions like in the below example:
<iframe src="https://my-website.com/page-with-webcam" allow="camera; microphone;"/>
MIT
FAQs
React webcam component
The npm package react-webcam receives a total of 134,350 weekly downloads. As such, react-webcam popularity was classified as popular.
We found that react-webcam demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.