1.1.0 • Published 2 years ago

react-browsercam v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-browsercam

A fully responsive and customizable camera component for React.

This package was designed to allow you to quickly implement a camera in your webapp.

For a quick implementation you can just copy the code from the usage chapter. If you need more freedom, you can swap out any component with a custom one, just note that the useCapture hook handles all the interaction with the camera stream.

Note:

The camera stream can only be accessed when the webapp runs over https or localhost!

Browser compatibility

Features

  • Fully customizable image capturing hook
  • Fullscreen support
  • Allows for custom camera interface
  • Interface can be integrated into the camera or placed below

Installation

npm install --save react-browsercam

Usage

The following example shows an implementation of a full featured camera.

import { Camera, CameraInterface, ImgDetailPopup, useCapture } from 'react-browsercam';

const WebcamComponent = () => {
  const { image, isAccessingCamera, videoRef, capture, setBeforeCapture } = useCapture({});
  const [displayDetails, setDisplayDetails] = useState(false);

  return (
    <Camera
      videoRef={videoRef}
      isAccessingCamera={isAccessingCamera}
      flash={setBeforeCapture}
      >
      <CameraInterface
        image={image}
        handleCapture={capture}
        openImage={() => setDisplayDetails(true)}
      >
        <ImgDetailPopup image={image} visible={displayDetails} handleClose={() => setDisplayDetails(false)}/>
      </CameraInterface>
    </Camera>
  );
};

Demo

Demo of react-browsercam

Example

Simple project

Components

useCapture Props

valuetypedefaultdescription
MediaStreamConstraints?MediaStreamConstraints \| undefinedaudio: false,video: {width: { ideal: 4096 },height: { ideal: 4096 },facingMode: 'environment }MediaStreamConstraint(s) affecting the displayed and captured images
imageFormat?'image/webp' \| 'image/png' \| 'image/jpeg'Selected image type
imageCompression?number?0.91Selected image compression rate

useCapture Return values

proptypedefaultdescription
imageBlob \| HTMLCanvasElement \| string \| nullnullContains the last captured image
isAccessingCameraboolfalseAllows you to check if the camera go access to the mediastream
videoRefHTMLVideoElementdocument.createElement('video')Allows you to show the mediastream by adding, by adding the videoRef to HTMLVideoElement elements like so ref={videoRef}
isNotSupportedboolfalseInforms you if mediastream isn't supported by your browser
isPermissionDeniedboolfalseInforms you if permission was granted/revoked to use the mediastream
setBeforeCapture() => void() => () => nullAllows you to calculate certain functions before capturing an image. Your function need two arrow functions (like in the default value), otherwise useState doesn't recognize it as a function
setAfterCapture() => void() => () => nullAllows you to calculate certain functions after capturing an image. Your function need two arrow functions (like in the default value), otherwise useState doesn't recognize it as a function

Camera Props

proptypedefaultdescription
videoRefRefObject<HTMLVideoElement>requiredNeeded to display mediastream from useCapture hook
isAccessingCamerabooleanrequiredNeeded to display camera if mediastream can be accessed
objectFit?'cover' \| 'fill' \| 'contain' \| 'none' \| 'scale-down''coverChanges the cropping of the image
fullscreen?boolean?trueAllows you to disable the fullscreen functionality
flashDispatch<SetStateAction<() => void>>requiredYou can pass it the setBeforeCapture value from useCapture to add a flash when capturing an image. Otherwise pass () => () => void
inPicture?boolean?trueAllows you to overlay the camera interface on top of the camera stream or below it
childrenYour camera interfacerequiredYou can use the provided CameraInterface Component as a default

CameraInterface Props

valuetypedefaultdescription
handleCapture() => voidrequiredThis function gets called when the capture button is pressed
imageBlob \| HTMLCanvasElement \| string \| nullrequiredImage is used to be displayed on the image detail button
openImage() => voidrequiredThis function gets called to open the image detail component
childrenYour image detail componentnullIf you place an image detail component in here, it will also be displayed in fullscreen

License

MIT

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago