Use desktopCapturer In Electron Applications

Posted By : Ritik Jain | 08-Dec-2020

First, we need to import desktopCapturer given below:-

import { ipcRenderer, webFrame, remote, screen, desktopCapturer } from 'electron';

desktopCapturer: typeof desktopCapturer;

 

Now we set the screen capture to 1 min by default:- 

let frequencyCount = 1;
       if(this.duration%frequencyCount == 0){
              this.captureScreen.emit('fired');
 }

 

Then call the screenCatpure function:-

  captureScreen(cb){
      const screenSize = this.screen.getPrimaryDisplay().workAreaSize;
      let options = { types: ['screen'], thumbnailSize: screenSize };
      this.desktopCapturer.getSources( options, function(error, sources) {
        if(error) cb({ error: error, imgdata: null });
        for (let source of sources) {
          if(source.name === 'Entire screen' || source.name === 'Screen 1'){
            cb({ error: null, imgdata: source.thumbnail.toDataURL() });
          }
        }
      })
  }

 

Also Read: How to use ioHook in electron application

 

The desktopCapturer module has the following methods:

types String[] - An array of Strings that classifies the kind of desktop sources to be captured from any of the following available kinds, screen & window.

thumbnailSize Size (optional) - It refers to the size of the media source thumbnail. The default is 150 x 150. To save the processing time for capturing the content of each window and screen, set width or height to 0. In this way, you won't have thumbnails.

fetchWindowIcons Boolean (optional) - To fetch window icons, set to true. By default, the value is false. When false, the appIcon property of the sources returns null. Also, the value is false when a source has the type screen.

 

The array of source objects is defined as a Source. Each one of them represents a captured screen or individual window, and possess the following properties:

id String - It refers to the ID of the window or screen being captured in the navigator.webkitGetUserMedia. It appears somewhat in the following format: window:XX or screen:XX, where XX is a randomly generated number.

name String - The specified name of the screen or window being captured. When we have the screen as a source, the name will be Entire Screen or Screen <index>; if it is a window, we will have the window's title as the name.

thumbnail NativeImage - A thumbnail image.

Thanks

 

We are a 360-degree software development company that provides complete web and mobile app development solutions for varied project requirements. Our end-to-end SaaS app development services address your mission-critical project requirements through scalable, responsive, and feature-rich software applications that are easy to scale. We carefully analyze your project requirements and formulate effective strategies to build enterprise-grade web and mobile applications for multiple platforms. For more info, contact us at [email protected]

About Author

Author Image
Ritik Jain

Ritik is an accomplished Backend Developer with extensive experience in Mean. He is proficient in Core Java, Spring-Boot, Hibernate, Node.js, Angular 2+, and various relational databases like MySQL and MongoDB. With his expertise in API implementations, webservices, development testing, and deployments, he has contributed to the successful completion of various client projects. Apart from his professional pursuits, Ritik is an enthusiastic gamer and keeps himself updated with the latest advancements in technology.

Request for Proposal

Name is required

Comment is required

Sending message..