フィルターのクリア

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

11 ビュー (過去 30 日間)
Dolu Obatusin
Dolu Obatusin 2018 年 7 月 18 日
コメント済み: goc3 2021 年 9 月 24 日
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 Center および File ExchangeDevelop 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