필터 지우기
필터 지우기

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

조회 수: 11 (최근 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