Socket
Socket
Sign inDemoInstall

easy-js-camera

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    easy-js-camera

A package which helps with adding camera support to a web application


Version published
Weekly downloads
250
increased by26.26%
Maintainers
1
Install size
15.5 kB
Created
Weekly downloads
 

Readme

Source

Intro

This package helps you to add camera support to your web application easily and support front and rear camera out of the box.

npm install easy-js-camera

Usage

Add your video and canvas as follow,

<video autoplay playsinline></video>
<canvas></canvas>

Import the Camera class as follow,

import Camera from 'easy-js-camera';

To be able to access to all Video Inputs you first need to get the permission from the user.
Note: On some devices it is not needed to first get the permission but it is better to do that first since on some devices if the permission is not granted it doesn't return all the devices.

var video = document.getElementsByTagName('video')[0];
var canvas = document.getElementsByTagName('canvas')[0];
Camera
    .tryInvokePermission(video, canvas)
    .then(camera => {
        camera.start()
    })
    .catch(error => {
        // Mostly happens if the user blocks the camera or the media devices are not supported
    });

Note: In order to make sure that the browser supports MediaDevices you can do as follow.

import Camera from 'easy-js-camera';

Camera.isCameraSupported();

Props

  • devices: Contains an array of available VideoInput devices
  • stream: Contains the current stream object. Gets initialized after the first camera start

Methods

  • getDevices: Returns a promise which if is successfull will deliver an array of CameraModels and if not logs an error
  • setVideoConstraints: By default only facingMode key of the video constraints is set. With this method you can expand the constraints and add your own costraints but please NOTICE that the facingMode is being used in switch method.
  • snap: This method will take a picture and will return the canvasElement so you can extract the picture as whatever format you like.
  • snapAsDataUrl: This method will take a picture and will return it as a base64 string.
  • snapAsBlob: This method will take a picture and will return a promise which on resolve will deliver a blob.
  • start: This method starts the camera and will return a promise for the result of the action.
  • stop: This method is responsible for stoping the stream.
  • switch: This method switches the camera between front and rear. This method returns a promise. It is better that you disable the switch camera button while it is doing its job because on some phones with motorized selfie camera if the user press the button multiple times the camera will hang and will just stickout and no longer works unless he/she restarts the phone. Please Note that this method accept a boolean which is called tryAgain and it basically tries to access the rear camera. Sometime the rear camera is not accessible and on catch you can do something like below
function switchCamera(tryAgain = false) {
    this.camera.switch(tryAgain)
        .catch(() => {
            if(tryAgain) return; // This line prevents loops. Because the tryAgain may also fail
            this.camera.switch(true);
        });
}

Keywords

FAQs

Last updated on 26 Nov 2020

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc