5.0

5.0 | 1 rating Rate this file 156 downloads (last 30 days) File Size: 6.3 KB File ID: #24323

ScreenCapture - get a screen-capture of a figure frame or component

by Yair Altman

 

01 Jun 2009 (Updated 03 Jun 2009)

Code covered by BSD License  

ScreenCapture gets a screen-capture of any Matlab GUI handle, or specified screen area rectangle

Download Now | Watch this File

File Information
Description

ScreenCapture gets a screen-capture of any Matlab GUI handle (including desktop, figure, axes or uicontrol), or a specified area rectangle located relative to the specified handle. Screen area capture is possible by specifying the root (desktop) handle (=0). The output can be either to an image file or to a Matlab matrix (useful for displaying via imshow() or for further processing). This utility also enables adding a toolbar button for easy interactive screen-capture.

Syntax:
     imageData = screencapture(handle, position, filename, 'PropName',PropValue, ...)
 
Input Parameters:
   handle - optional handle to be used for screen-capture origin.
                  If empty/unsupplied then current figure (gcf) will be used.

   position - optional position array in pixels: [x,y,width,height].
                  If empty/unsupplied then the handle's position vector will be used.
                  If both handle and position are empty/unsupplied then the position
                    will be retrieved via interactive mouse-selection.

   filename - optional filename for storing the screen-capture.
                  If empty/unsupplied then no output to file will be done.
                  The file format will be determined from the filename (JPG/PNG/...).
                  Supported formats are those supported by the imwrite function.
                  If neither filename nor imageData were specified, the user will be asked to
                    interactively specify the output filename.

   'PropName',PropValue -
                  optional list of property pairs
                    e.g., screencapture('filename','sc.png','pos',[10,20,30,40],'handle',gca)
                  PropNames may be abbreviated and are case-insensitive.
                  PropNames may also be given in whichever order.
                  Supported PropNames are:
                    - 'handle' (default: gcf handle)
                    - 'position' (default: gcf position array)
                    - 'filename' (default: '')
                    - 'toolbar' (figure handle; default: gcf)
                    this adds a screen-capture button to the figure's toolbar
                    If this parameter is sspecified, then no screen-capture
                    will take place and the returned imageData will be [].

Output parameters:
  imageData - image data in a format acceptable by the imshow function.
                    If neither filename nor imageData were specified,
                    the user will be asked to interactively specify the output filename.
 
Examples:
   imageData = screencapture; % interactively select screen-capture rectangle
   imageData = screencapture(hListbox); % capture image of a uicontrol
   imageData = screencapture(0, [20,30,40,50]); % select a small desktop region
   imageData = screencapture(gcf,[20,30,40,50]); % select a small figure region
   imageData = screencapture(gca,[10,20,30,40]); % select a small axes region
     imshow(imageData); % display the captured image in a matlab figure
     imwrite(imageData,'myImage.png'); % save the captured image to file
   screencapture(gcf,[],'myFigure.jpg'); % capture the entire figure into file
   screencapture('handle',gcf,'filename','myFigure.jpg'); % same as previous
   screencapture('toolbar',gcf); % adds a screen-capture button to gcf's toolbar
   screencapture('toolbar',[],'file','sc.bmp'); % same with default output filename
 
Bugs and suggestions:
   Please send to Yair Altman (altmany at gmail dot com)

MATLAB release MATLAB 7.5 (R2007b)
Other requirements Requires Java engine, but as far as I can tell this utility does NOT use any undocumented/unsupported Matlab features. Developed on Matlab R2007b but should also work on other Matlab versions (untested - please report any issues)
Zip File Content  
Other Files license.txt,
screencapture.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
02 Jun 2009 us

a very nice robot, which the FEX community has been looking for a long time...
two thoughts:
1) in interactive mode, the user looses the data if no output arg is selected, which may be a bit frustrating:
a) a warning(?)
b) a new figure displaying the result by default(?)
2) we've been using this approach, which may be a bit faster (depending on the size of the capture) by converting the planes in one shot using a macro...

% input
% xoff/yoff/xlen/ylen
% assign after loading java stuff...
     rob=Robot;
% macro
     conv=@(a,b,c) double(bitand(a,255^b))./255^c;
% capture screen contents at this pos
     rec=Rectangle(xoff,yoff,xlen,ylen);
     cap=rob.createScreenCapture(rec);
     dat=cap.getData;
     buf=dat.getDataStorage;
     buf=typecast(buf(:),'uint32');
     buf=reshape(buf,dat.getWidth,dat.getHeight).';
     mm=zeros([size(buf),3]);
     mm(:,:,3)=conv(buf,1,0); % bitand(buf,255^1)./255^0;
     mm(:,:,2)=conv(buf,2,1); % bitand(buf,255^2)./255^1;
     mm(:,:,1)=conv(buf,3,2); % bitand(buf,255^3)./255^2;
     mm=mm./255;
% ...for display in a regular figure
     figure;
     set(gca,'position',[0,0,1,1]);
     image(mm);
     axis image;
     axis off;

3) in principle, the robot itself can be used on any screen location; it would be nice - however, admittedly cumbersome - to add this feature

us

Please login to add a comment or rating.
Updates
03 Jun 2009

Handle missing output format; performance boost (thanks to Urs); fix minor root-handle bug; added toolbar button option

03 Jun 2009

updated attached screenshot with explanatory notes

Tag Activity for this File
Tag Applied By Date/Time
java Yair Altman 02 Jun 2009 09:45:58
gui Yair Altman 02 Jun 2009 09:45:58
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com