Capturing and Saving an image of a running using code in Matlab's Appdesigner

20 visualizaciones (últimos 30 días)
Hello,
I'm working on an app in Matlab Appdesigner. I have an idea to save an image of the app, like a screenshot equivalent, but using code. What I have in mind is to allow the user to press a button that performs a "screen capture" of the current app state.
I am using Matlab version 9.3.0.713579 (R2017b). Thank you in advance for your suggestions and help.

Respuesta aceptada

Ashutosh Prasad
Ashutosh Prasad el 2 de Ag. de 2018
Hello,
In MATLAB Appdesigner, the created apps are displayed in UI figure which is different from the conventional figure in the way that many of the MATLAB functionalities is unsupported with the figures created using the uifigure command like saveas, savefig etc. Please refer to this page to know about the functions which are not supported with uifigure.
Although you can explicitly take the screenshot of your app and save it in your current directory either by using the ScreenCapture – Submission to File Exchange or by using the Java Libraries . Following is the code of a callback function to a pushbutton which captures the App.
% Button pushed function: CaptureButton
function CaptureButtonPushed(app, event)
robot = java.awt.Robot();
temp = app.UIFigure.Position; % returns position as [left bottom width height]
pos = [temp(1) temp(2)+temp(4) temp(3) temp(4)]; % [left top width height]
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
% Convert to an RGB image
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';
imshow(imgData);
end
  3 comentarios
Harsh Trivedi
Harsh Trivedi el 17 de Jul. de 2020
@ Ashutosh Prasad,
How would I call this function in AppDesigner code? Will it be inside button pushed function?
Thea Kristensen
Thea Kristensen el 4 de Dic. de 2020
Hi, I'm having a hard time understanding the three lines with imgData in the conversion to the RGB-image. Can someone explain me why it has to be rgb(3:4:end) in imgData(:,:,:1) and rgb(2:4:end) in imgData(:,:,2) and rgb(3:4:end) in imgData(:,:,3)? And what does these three parameters mean?

Iniciar sesión para comentar.

Más respuestas (2)

Dan Young
Dan Young el 5 de Dic. de 2019
This code worked nearly perfectly, but I found the second input to the java rectangle actually had to be the current monitor height MINUS the upper-left pixel value (which is bottom+height in typical Matlab positions).
So my working code first identifies the monitor the image is on, then gets it's height, and then uses that in the call to the javascript rectangle.
% Button pushed function: CaptureButton
robot = java.awt.Robot();
temp = app.UIFigure.Position; % returns position as [left bottom width height]
allMonPos = get(0,'MonitorPositions');
curMon = find(temp(1)<(allMonPos(:,1)+allMonPos(:,3)),1,'first');
curMonHeight = allMonPos(curMon,4)+1;
pos = [temp(1) curMonHeight-(temp(2)+temp(4)) temp(3)-1 temp(4)]; % [left top width height].... UL X, UL Y, width, height
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
% Convert to an RGB image
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';
imclipboard('copy', imgData)

Adam Danz
Adam Danz el 20 de Oct. de 2020
Staring in Matlab r2020b, the exportapp() function saves an image of your app or uifigure.
See this Community Highlight for a demo.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by