All Projects → mozmorris → React Webcam

mozmorris / React Webcam

Licence: mit
Webcam component

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Webcam

getting-started
List of ideas for getting started with TimVideos projects
Stars: ✭ 50 (-94.7%)
Mutual labels:  webcam
Qbr
A webcam-based 3x3x3 rubik's cube solver written in Python 3 and OpenCV.
Stars: ✭ 122 (-87.06%)
Mutual labels:  webcam
Cam2ip
Turn any webcam into an IP camera
Stars: ✭ 587 (-37.75%)
Mutual labels:  webcam
libuvc-rs
Rust wrapper around libuvc
Stars: ✭ 31 (-96.71%)
Mutual labels:  webcam
webcam
OpenCV and UDP webcam streaming
Stars: ✭ 43 (-95.44%)
Mutual labels:  webcam
Clight
A C daemon that turns your webcam into a light sensor. It will adjust screen backlight based on ambient brightness.
Stars: ✭ 371 (-60.66%)
Mutual labels:  webcam
jeelizPupillometry
Real-time pupillometry in the web browser using a 4K webcam video feed processed by this WebGL/Javascript library. 2 demo experiments are included.
Stars: ✭ 78 (-91.73%)
Mutual labels:  webcam
Jeelizweboji
JavaScript/WebGL real-time face tracking and expression detection library. Build your own emoticons animated in real time in the browser! SVG and THREE.js integration demos are provided.
Stars: ✭ 835 (-11.45%)
Mutual labels:  webcam
Amazing Python Scripts
🚀 Curated collection of Amazing Python scripts from Basics to Advance with automation task scripts.
Stars: ✭ 229 (-75.72%)
Mutual labels:  webcam
Showmewebcam
Raspberry Pi + High Quality Camera = High-quality USB Webcam!
Stars: ✭ 531 (-43.69%)
Mutual labels:  webcam
simple-login-qrcode-webcam-php
🐱‍💻 Just simple login mechanism using QR Code Scanner with Webcam in PHP
Stars: ✭ 66 (-93%)
Mutual labels:  webcam
WebcamFX
Open the camera, take pictures 📷 & save them using Javafx.
Stars: ✭ 39 (-95.86%)
Mutual labels:  webcam
Ssd.pytorch
A PyTorch Implementation of Single Shot MultiBox Detector
Stars: ✭ 4,499 (+377.09%)
Mutual labels:  webcam
PhpWebcam
This is a PHP library to capture webcam frames
Stars: ✭ 33 (-96.5%)
Mutual labels:  webcam
React Qr Reader
React component for reading QR codes from webcam.
Stars: ✭ 716 (-24.07%)
Mutual labels:  webcam
asciisciit
ASCII Art, Video, and Plotting Toolbox
Stars: ✭ 71 (-92.47%)
Mutual labels:  webcam
Webcam
Golang webcam library for Linux
Stars: ✭ 301 (-68.08%)
Mutual labels:  webcam
Webrtc
Pure Go implementation of the WebRTC API
Stars: ✭ 8,399 (+790.67%)
Mutual labels:  webcam
Stealing Ur Feelings
Winner of Mozilla's $50,000 prize for art and advocacy exploring AI
Stars: ✭ 784 (-16.86%)
Mutual labels:  webcam
Lolcommits
📷 git-based selfies for software developers
Stars: ✭ 4,468 (+373.81%)
Mutual labels:  webcam

react-webcam

Build Status downloads

DEMO: https://codepen.io/mozmorris/pen/JLZdoP

https://www.npmjs.com/package/react-webcam

Webcam component for React. See http://caniuse.com/#feat=stream for browser compatibility.

Note: Browsers will throw an error if the page is loaded from insecure origin. I.e. Use https.

Installation

// with npm
npm install react-webcam

// with yarn
yarn add react-webcam

Demo

https://codepen.io/mozmorris/pen/JLZdoP

Usage

import React from "react";
import Webcam from "react-webcam";

const WebcamComponent = () => <Webcam />;

Props

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
forceScreenshotSourceSize boolean false uses size of underlying source video stream (and thus ignores other size related props)
imageSmoothing boolean true pixel smoothing of the screenshot taken
mirrored boolean false show camera preview and get the screenshot mirrored
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

Methods

getScreenshot - Returns a base64 encoded string of the current webcam image. Example:

https://codepen.io/mozmorris/pen/gOOoqpw

You may also pass in an optional dimensions object:

getScreenshot({width: 1920, height: 1080});

The Constraints

We can build a constraints object by passing it to the videoConstraints prop. This gets passed into getUserMedia method. Please take a look at the MDN docs to get an understanding how this works.

https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia https://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API/Constraints

As an example take a look at this CodePen demo https://codepen.io/mozmorris/pen/GRpEQwK?editors=0010 which shows how to build a custom aspect ratio for the video.

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>
    </>
  );
};

Capturing video

It is posible to capture video with <Webcam /> using the MediaStream Recording API.

You can find an example https://codepen.io/mozmorris/pen/yLYKzyp?editors=0010.

Choosing a camera

User/Selfie/forward facing camera

class WebcamCapture extends React.Component {
  render() {
    const videoConstraints = {
      facingMode: "user"
    };

    return <Webcam videoConstraints={videoConstraints} />;
  }
}

Environment/Facing-Out camera

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

Show all cameras by deviceId

const WebcamCapture = () => {
  const [deviceId, setDeviceId] = React.useState({});
  const [devices, setDevices] = React.useState([]);

  const handleDevices = React.useCallback(
    mediaDevices =>
      setDevices(mediaDevices.filter(({ kind }) => kind === "videoinput")),
    [setDevices]
  );

  React.useEffect(
    () => {
      navigator.mediaDevices.enumerateDevices().then(handleDevices);
    },
    [handleDevices]
  );

  return (
    <>
      {devices.map((device, key) => (
          <div>
            <Webcam audio={false} videoConstraints={{ deviceId: device.deviceId }} />
            {device.label || `Device ${key + 1}`}
          </div>

        ))}
    </>
  );
};

Recording a stream

https://codepen.io/mozmorris/pen/yLYKzyp?editors=0011

Using within an iframe

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;"/>

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].