react-html5-camera-photo vs react-webcam
React Camera Libraries
react-html5-camera-photoreact-webcam

React Camera Libraries

React camera libraries provide developers with tools to easily integrate camera functionalities into their applications. They allow users to capture photos and videos directly from their devices, enhancing user interaction and experience. These libraries abstract the complexities of accessing device cameras, providing a straightforward API for developers to work with. They are particularly useful in applications requiring user-generated content, such as social media apps, e-commerce platforms, and any app that benefits from real-time media capture.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-html5-camera-photo0218246 kB16-MIT
react-webcam01,751168 kB652 years agoMIT

Feature Comparison: react-html5-camera-photo vs react-webcam

Photo Capture

  • react-html5-camera-photo:

    react-html5-camera-photo specializes in providing a simple interface for capturing still images from the user's camera. It is designed to be lightweight and easy to integrate, making it an excellent choice for applications that primarily need photo functionality without additional overhead.

  • react-webcam:

    react-webcam offers robust support for capturing both photos and videos. It allows developers to take snapshots from the video stream, providing more flexibility for applications that require both image and video functionalities.

Video Support

  • react-html5-camera-photo:

    react-html5-camera-photo does not support video capture, focusing solely on still image functionality. This makes it less suitable for applications that need video features.

  • react-webcam:

    react-webcam excels in video support, allowing developers to stream video from the camera and capture video clips. This makes it ideal for applications that require real-time video interaction or recording.

Ease of Use

  • react-html5-camera-photo:

    react-html5-camera-photo is designed for simplicity and ease of use. It provides a straightforward API that allows developers to quickly implement photo capture with minimal setup, making it accessible for beginners and efficient for experienced developers.

  • react-webcam:

    react-webcam offers a more comprehensive API that may require a slightly steeper learning curve due to its additional features. However, it remains user-friendly and provides extensive documentation to help developers navigate its functionalities.

Customization Options

  • react-html5-camera-photo:

    react-html5-camera-photo offers limited customization options, focusing on core functionalities for photo capture. It is suitable for applications that do not require extensive customization of the camera interface.

  • react-webcam:

    react-webcam provides a variety of customization options, allowing developers to manipulate the video feed, apply filters, and adjust settings. This makes it a better choice for applications that require a tailored camera experience.

Community and Support

  • react-html5-camera-photo:

    react-html5-camera-photo has a smaller community compared to react-webcam, which may result in fewer resources and community support. However, it is still actively maintained and has a straightforward implementation process.

  • react-webcam:

    react-webcam has a larger community and more extensive documentation, providing a wealth of resources for troubleshooting and implementation. This can be beneficial for developers seeking support and examples.

How to Choose: react-html5-camera-photo vs react-webcam

  • react-html5-camera-photo:

    Choose react-html5-camera-photo if you need a lightweight solution focused on capturing photos directly from the camera with minimal configuration. It is suitable for applications that require basic photo capture functionality without the need for extensive video support.

  • react-webcam:

    Choose react-webcam if you require a more versatile solution that supports both photo and video capture. It offers additional features like video streaming and the ability to manipulate the video feed, making it ideal for applications that need real-time video processing or effects.

README for react-html5-camera-photo

react-html5-camera-photo

The first objective of this package comes from the need to get the same look and feel of a native mobile camera app but with a react component. For those who want to build with their own css and need an abstraction of getUserMedia() take a look of jslib-html5-camera-photo with react.

Requirement

  • react: >=16.8.0
  • react-dom: >=16.8.0

LiveDemo

alt demo_android

Demo of react-html5-camera-photo

Required Working Environment for getUserMedia()

  • https or localhost : The getUserMedia() method is only available in secure contexts (https or localhost). If a document isn't loaded in a secure context, the navigator.mediaDevices property is undefined, making access to getUserMedia() impossible. Attempting to access getUserMedia() in this situation will result in a TypeError. See developer.mozilla.org

  • iOS >= 11 WebRTC issue with webkit (Chrome & Firefox) : Apple restricts WebRTC to Safari only so it mean that you can't use the getUserMedia() with Firefox and Chrome. So getUserMedia() is not supported yet, for "security reasons". See Stackoverflow

Installation

npm install --save react-html5-camera-photo
yarn add react-html5-camera-photo

TypeScript Definitions

TypeScript definitions are available from Definitely Typed

npm install --save-dev @types/react-html5-camera-photo
yarn add --dev @types/react-html5-camera-photo

Getting started

parameterDescription
onTakePhoto(dataUri):Event function called when a photo is taken. the dataUri is passed as a parameter.

Minimum ES6 example

import React from 'react';
import Camera from 'react-html5-camera-photo';
import 'react-html5-camera-photo/build/css/index.css';

function App (props) {
  function handleTakePhoto (dataUri) {
    // Do stuff with the photo...
    console.log('takePhoto');
  }

  return (
    <Camera
      onTakePhoto = { (dataUri) => { handleTakePhoto(dataUri); } }
    />
  );
}

export default App;

API

PropTypes

PropertiesTypeDefaultDescription
onCameraStart(): (optional)EventCallback called when the camera is started.
onCameraStop(): (optional)EventCallback called when the camera is stopped.
onCameraError(error): (Optional)EventCallback called with the error object as parameter when error occur while opening the camera. Often the permission.
onTakePhoto(dataUri): (Optional)EventThe function called when a photo is taken. the dataUri is passed as a parameter.
onTakePhotoAnimationDone(dataUri): (Optional)EventThe function called when a photo is taken and the animation is done. the dataUri is passed as a parameter.
idealFacingMode: (Optional) (Dynamic)StringBrowser defaultThe ideal facing mode of the camera, environment or user. Use FACING_MODES constant to get the right string. Example :. FACING_MODES.ENVIRONMENT or FACING_MODES.USER
idealResolution: (Optional) (Dynamic)ObjectBrowser defaultObject of the ideal resolution of the camera, {width: Integer, height: Integer}.
isMaxResolution: (Optional) (Dynamic)BooleanfalseIf is true, the camera will start with his own maximum resolution.
isImageMirror: (Optional)BooleantrueIf is true, the camera image will be mirror.
isSilentMode:(Optional)BooleanfalseIf is true, the camera do not play click sound when the photo was taken.
isFullscreen: (Optional)BooleanfalseIf is true, the camera image will be set fullscreen to force the maximum width and height of the viewport.
isDisplayStartCameraError: (Optional)BooleantrueIf is true, if the camera start with error, it will show the error between h1 tag on the top of the component. Useful to notify the user about permission error.
sizeFactor: (Optional)Number1Number of the factor resolution. Example, a sizeFactor of 1 get the same resolution of the camera while sizeFactor of 0.5 get the half resolution of the camera. The sizeFactor can be between range of ]0, 1].
imageType:: (Optional)StringpngString used to get the desired image type between jpg or png. to specify the imageType use the constant IMAGE_TYPES, for example to specify jpg format use IMAGE_TYPES.JPG. Use IMAGE_TYPES constant to get the right image type Example:. IMAGE_TYPES.JPG or IMAGE_TYPES.PNG
imageCompression:: (Optional)Number0.92Number used to get the desired compression when jpg is selected. choose a compression between [0, 1], 1 is maximum, 0 is minimum.

Dynamic : If the prop is dynamic, it mean that you can change that prop dynamically without umount the component (removing it). You can do it by a setState() inside the parent component. Checkout the demo example: ./src/demo/AppWithDynamicProperties.js

Example of closing the camera and image preview after take a photo

Probably the typical usage of using this component is to preview the image and close the camera after take a photo. You can take a look of all the code including the ImagePreview component here : ./src/demo/AppWithImagePreview

import React, { useState } from 'react';
import Camera from 'react-html5-camera-photo';
import 'react-html5-camera-photo/build/css/index.css';

import ImagePreview from './ImagePreview'; // source code : ./src/demo/AppWithImagePreview/ImagePreview

function App (props) {
  const [dataUri, setDataUri] = useState('');

  function handleTakePhotoAnimationDone (dataUri) {
    console.log('takePhoto');
    setDataUri(dataUri);
  }

  const isFullscreen = false;
  return (
    <div>
      {
        (dataUri)
          ? <ImagePreview dataUri={dataUri}
            isFullscreen={isFullscreen}
          />
          : <Camera onTakePhotoAnimationDone = {handleTakePhotoAnimationDone}
            isFullscreen={isFullscreen}
          />
      }
    </div>
  );
}

export default App;

Example with all props used

import React from 'react';
import Camera, { FACING_MODES, IMAGE_TYPES } from 'react-html5-camera-photo';
import 'react-html5-camera-photo/build/css/index.css';

function App (props) {
  function handleTakePhoto (dataUri) {
    // Do stuff with the photo...
    console.log('takePhoto');
  }

  function handleTakePhotoAnimationDone (dataUri) {
    // Do stuff with the photo...
    console.log('takePhoto');
  }

  function handleCameraError (error) {
    console.log('handleCameraError', error);
  }

  function handleCameraStart (stream) {
    console.log('handleCameraStart');
  }

  function handleCameraStop () {
    console.log('handleCameraStop');
  }

  return (
    <Camera
      onTakePhoto = { (dataUri) => { handleTakePhoto(dataUri); } }
      onTakePhotoAnimationDone = { (dataUri) => { handleTakePhotoAnimationDone(dataUri); } }
      onCameraError = { (error) => { handleCameraError(error); } }
      idealFacingMode = {FACING_MODES.ENVIRONMENT}
      idealResolution = {{width: 640, height: 480}}
      imageType = {IMAGE_TYPES.JPG}
      imageCompression = {0.97}
      isMaxResolution = {true}
      isImageMirror = {false}
      isSilentMode = {false}
      isDisplayStartCameraError = {true}
      isFullscreen = {false}
      sizeFactor = {1}
      onCameraStart = { (stream) => { handleCameraStart(stream); } }
      onCameraStop = { () => { handleCameraStop(); } }
    />
  );
}

export default App;

Bug report (issues)

Before sending a bug report of camera error, make sure that getUserMedia() is supported by your browser. Please test your camera on : DetectRTC | Is WebRTC Supported In Your Browser? If the System has Webcam is supported, please send the screenshot of the first 7 first rows of the table.

FAQ

  1. What if i want to improve the code or add functionalities?