Taking a Screenshot (Screen Capture) using MATLAB Code

How you take a screenshot of your computer's screen till now?
With the "Print Screen" key and then pasting the shot in Paint or Other software?
Well, that's an old way now! You can use your MATLAB code to take a screenshot of the current screen.
How?
Using the Java's Robot Class function which can be imported in MATLAB.

MATLAB with a figure window showing the screenshot of the current screen
MATLAB with a figure window showing the screenshot of the current screen

Here is the MATLAB code for doing a Screen Capture (Comments Bolded).....

clc
clear
close all

import java.awt.Robot;
vks = Robot; %Creating a Robot object

import java.awt.Rectangle;
rect = Rectangle; %Creating a Rectangle object 

import java.awt.Toolkit.getDefaultToolkit;
tool = getDefaultToolkit; %Creating a Toolkit object

screenDim = tool.getScreenSize; %Getting the screen size

width = screenDim.width; %Width of screen
height = screenDim.height; %Height of screen

rect.setRect(0, 0, width, height); %Setting the size of screen capture

screen = vks.createScreenCapture(rect); %capturing the screen

pixelsData = reshape(typecast(screen.getData.getDataStorage, 'uint8'), 4, width, height);
%Converting to 8 Bit data

imgData = zeros([height,width,3],'uint8'); %Create the image variable
imgData(:,:,1) = transpose(reshape(pixelsData(3, :, :), width, height));
imgData(:,:,2) = transpose(reshape(pixelsData(2, :, :), width, height));
imgData(:,:,3) = transpose(reshape(pixelsData(1, :, :), width, height));
%Save image data B-G-R Plane wise

imshow(imgData) %Show the captured screenshot
imwrite(imgData, 'ScreenCaptureMatlab.jpg'); %Save the captured screenshot
%End of code

Further, you can control the size of the screenshot with a slide modification in the above code! You can also introduce a finite delay between the start of the code and the screen capture step so that you can minimize MATLAB and open the window/screen of your choice.

Have you got a great idea with this code or the Java Robot class in MATLAB? You can share in the comments!


Author: Vibhutesh Kumar Singh
An active & prominent author at Digital iVision Labs! Like to write about MATLAB, C++, Arduino, OpenCV, NI Labview, Web Designing & other Electronics stuffs! Finished up M.Tech. From IIIT Delhi, now doing PhD in wireless communication with prominent researchers University College Dublin (CONNECT CENTRE). Drop a mail: vibhutesh[at]gmail.com to ask something about the article or Follow him at....

0 comments:

Access MATLAB in your Android/iOS based Mobile Phones/Tablets

MATLAB's  Mobile application is available on Google Play and iOS App store. And thus it is now very easy to access the MATLAB interface on the go!

A screen grab from the configuring step of MATLAB Mobile app

It gives 2 option to work with.
1. Connect remotely to your PC's MATLAB installation (Which would be more convenient if you want to access your files, perform heavy computations, and inside a private network like while roaming inside your university).
2. Connect to Mathworks MATLAB Cloud service ( Convenient if you got a stable working internet connection and perform soft computation tasks-If you run computation extensive script you have to wait very long to see any output, this would be annoying in most of the cases). For this, you need a MathWorks account, not necessary to get a licence for using MATLAB functions.

So what are the Features of this app?
1. You can use the command window like interface to perform quick calculations.
2. You can create, edit, save and run MATLAB script. Using MATLAB cloud option will let you save the files online!
3. You will get some online file storage facility.

In case you got a MATLAB's licence, then you can add that in the App which will give you an option to acquire and save the data from Mobile Phone's/Tablet's sensors! Also some additional space for use in MATLAB Cloud.

Connecting to MATLAB's Cloud:
2 Step Process:
1. Click on "Connect to MathWorks Cloud" button.
2. Login with your Mathworks.com user id and password.

Connecting to Your PC's MATLAB:
Assuming you are on a local network.
Configuring PC's MATLAB.
1. Type the command in your PC's MATLAB Command Window: connector on
2. If it's the first run of this command then you need to specify an alphanumeric password for it.
3. After specifying the password, press enter. You will get a Message like this.

DNS name: DESKTOP-XYZ
IP address: 192.168.xxx.xxx
Use this link to test the MATLAB Connector:
http://DESKTOP-XYZ:31415/

If the test is successful, but MATLAB Mobile cannot connect,
your computer might have multiple IP addresses.  To determine
the correct one, see Determining the DNS Name or IP Address of a Computer.
4. In a web browser open the link: http://DESKTOP-XYZ:31415/
If the connector service is successfully started. It will show the message:

The connector service is running.

 Configuring MATLAB Mobile to access your PC's MATLAB Installation:
1. Click on "Connect to Your Computer" button.
2. Enter The IP-Address, Port as: 31415 and Password which you have specified.
3. Enter a nick name of the PC. (Any name will work).
3. Click on Connect.
The connection will be done and you can now access your PC's MATLAB files as well as the files in the work folder.


While running MATLAB App your device will not go automatically to sleep, unless you do it!

Disonnecting to Your PC's MATLAB:
If you want to turn off the connection service type in the command: connector off

With some limitations relating to  MATLAB App installation, MATLAB Figure editing, this is indeed a handy tool for MATLAB users to begin with!

Author: Vibhutesh Kumar Singh
An active & prominent author at Digital iVision Labs! Like to write about MATLAB, C++, Arduino, OpenCV, NI Labview, Web Designing & other Electronics stuffs! Finished up M.Tech. From IIIT Delhi, now doing PhD in wireless communication with prominent researchers University College Dublin (CONNECT CENTRE). Drop a mail: vibhutesh[at]gmail.com to ask something about the article or Follow him at....

0 comments: