Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mage-react-cam

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mage-react-cam

A React component for capturing camera input

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Mage React Cam

Install:

npm i mage-react-cam

or

yarn add mage-react-cam

Usage:

import { MageReactCam } from 'mage-react-cam';
import { MediaSrcObjectProps, TReactCamRef } from 'mage-react-cam/dist/MageReactCam';
import { ReactElement, useCallback, useEffect, useRef, useState } from 'react';

interface MyReactCamComponentProps {
  onUpdate: (arg0: unknown, arg1?: any) => void;
  onError?: (arg0: string | DOMException) => void;
  width?: number | string;
  height?: number | string;
  facingMode?: 'environment' | 'user';
  torch?: boolean;
  delay?: number;
  videoConstraints?: MediaStreamConstraints;
  stopStream?: boolean;
}

const MyReactCamComponent = ({
  onUpdate,
  onError,
  torch,
  delay = 500,
  videoConstraints,
  stopStream,
}: MyReactCamComponentProps): ReactElement => {
  const [timer, setTimer] = useState<boolean>(false);
  const videoRef = useRef<TReactCamRef>(null);

  const handlerSnapshot = () => {
    const snapShot = videoRef?.current?.snapshot;
    if (snapShot) return snapShot();
  };

  const handleZoomIn = () => { // use if need zoom +
    const zoomIn = videoRef?.current?.zoomIn;
    if (zoomIn) zoomIn();
  };

  const handleZoomOut = () => { // use if need zoom -
    const zoomOut = videoRef?.current?.zoomOut;
    if (zoomOut) zoomOut();
  };

  useEffect(() => {
    if (timer)
      setTimeout(() => {
        setTimer(false);
      }, 3000);
  }, [timer]);

  const capture = useCallback(() => {
    const imageSrc = handlerSnapshot();
    if (imageSrc) {
      // use imageSrc like you want
    }
  }, [onUpdate, videoRef]);

  useEffect(() => {
    if (
      typeof torch === 'boolean' &&
      (navigator?.mediaDevices?.getSupportedConstraints() as { torch: any }).torch
    ) {
      const stream = videoRef?.current?.video?.srcObject as MediaSrcObjectProps;
      const track = stream?.getVideoTracks()[0];
      if (
        track &&
        track.getCapabilities().torch &&
        !track.getConstraints().torch
      ) {
        track
          .applyConstraints({
            advanced: [{ torch }],
          })
          .catch((err: any) => onUpdate(err));
      }
    }
  }, [torch, onUpdate]);

  useEffect(() => {
    if (stopStream) {
      let stream = videoRef?.current?.video?.srcObject;
      if (stream) {
        stream.getTracks().forEach((track: any) => {
          stream?.removeTrack(track);
          track.stop();
        });
        stream = undefined;
      }
    }
  }, [stopStream]);

  useEffect(() => {
    const interval = setInterval(capture, delay);
    return () => {
      clearInterval(interval);
    };
  }, []);

  return (
    <div>
        <MageReactCam
          ref={videoRef}
          onUserMediaError={onError}
          videoConstraints={videoConstraints}
          width={500}
          height={500}
          facingMode="environment"
        />
    </div>
  );
};

export default MyReactCamComponent;

Keywords

FAQs

Package last updated on 10 Jul 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc