Code Monkey home page Code Monkey logo

react-camera-pro's Introduction

npm downloads

react-camera-pro

Universal Camera component for React.

Designed with focus on Android and iOS cameras. Works with standard webcams as well.

See this for browser compatibility.

Note: WebRTC is only supported on secure connections. So you need to serve it from https. You can test and debug in Chrome from localhost though (this doesn't work in Safari).

Features

  • mobile friendly camera solution (tested on iOS and Android)
  • video element is fully responsive
    • you can setup parameter to cover your container
    • you can define aspectRatio of view: 16/9, 4/3, 1/1, ...
  • taking photo to base64 jpeg file - with same aspect Ratio as view, with FullHD resolution (or maximum supported by camera).
  • working with standard webcams or other video input devices
  • supports autofocus
  • switching facing/environment cameras (with your own button)
  • detect number of cameras
  • facing camera is mirrored, environment is not
  • controlled by react Ref
  • public functions to take photo, to switch camera and to get number of cameras
  • typescript library

Installation

npm install --save react-camera-pro

Demo

https://purple-technology.github.io/react-camera-pro/

Example

https://github.com/purple-technology/react-camera-pro/blob/master/example/src/App.tsx

Usage

import React, { useState, useRef } from "react";
import {Camera} from "react-camera-pro";

const Component = () => {
  const camera = useRef(null);
  const [image, setImage] = useState(null);

  return (
    <div>
      <Camera ref={camera} />
      <button onClick={() => setImage(camera.current.takePhoto())}>Take photo</button>
      <img src={image} alt='Taken photo'/>
    </div>
  );
}

export Component;

Props

prop type default notes
facingMode 'user'|'environment' 'user' default camera - 'user' or 'environment'
aspectRatio 'cover'|number 'cover' aspect ratio of video (16/9, 4/3);
numberOfCamerasCallback (numberOfCameras: number):void () => null callback is called if number of cameras change
errorMessages object? see below see below Error messages object (optional)

Error messages (prop errorMessages)

Type:

errorMessages: {
  noCameraAccessible?: string;
  permissionDenied?: string;
  switchCamera?: string;
  canvas?: string;
};

Default:

  {
    noCameraAccessible: 'No camera device accessible. Please connect your camera or try a different browser.',
    permissionDenied: 'Permission denied. Please refresh and give camera permission.',
    switchCamera:
    'It is not possible to switch camera to different one because there is only one video device accessible.',
    canvas: 'Canvas is not supported.'
  }

Methods

  • takePhoto(): string - Returns a base64 encoded string of the taken image.
  • switchCamera(): 'user'|'environment' - Switches the camera - user to environment or environment to user. Returns the new value 'user' or 'environment'.
  • getNumberOfCameras(): number - Returns number of available cameras.

See demo

See example code

const Component = () => {
  const camera = useRef(null);
  const [numberOfCameras, setNumberOfCameras] = useState(0);
  const [image, setImage] = useState(null);

  //...

  return (
    <Camera ref={camera} numberOfCamerasCallback={setNumberOfCameras} />
      <img src={image} alt='Image preview' />
      <button
        onClick={() => {
            const photo = camera.current.takePhoto();
            setImage(photo);
        }}
      />
      <button
        hidden={numberOfCameras <= 1}
        onClick={() => {
          camera.current.switchCamera();
        }}
      />
  )

Camera options

User/Enviroment camera

  const Cam = () => <Camera ref={camera} facingMode='environment'} />

Aspect ratio

const Cam = () => <Camera ref={camera} aspectRatio={16 / 9} />;

Using within an iframe

<iframe src="https://example.com/camera-pro-iframe" allow="camera;"/>

Credits

License

MIT

react-camera-pro's People

Contributors

aiddun avatar screamz avatar xurban42 avatar

Forkers

adg29

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.