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

16 次查看(过去 30 天)
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.

采纳的回答

Ashutosh Prasad
Ashutosh Prasad 2018-8-2
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 个评论
Harsh Trivedi
Harsh Trivedi 2020-7-17
@ Ashutosh Prasad,
How would I call this function in AppDesigner code? Will it be inside button pushed function?
Thea Kristensen
Thea Kristensen 2020-12-4
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?

请先登录,再进行评论。

更多回答(2 个)

Dan Young
Dan Young 2019-12-5
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 2020-10-20
Staring in Matlab r2020b, the exportapp() function saves an image of your app or uifigure.
See this Community Highlight for a demo.

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by