Capture screenshot on second monitor HDMI input source

Hi,
I have dual monitors connected to the desktop via DP connections. I also have video feed that I would like to take periodic screenshots of. I can connect my video feed to one monitor via HDMI input. When I attempt to take a screenshot of the video, the screenshot returns what is displayed on the DP connection, not the HDMI connection. Is there a way to capture screenshots of the HDMI input on the second monitor while operating MATLAB through the DP on the first monitor?
Below is my code for screencapture. This code works fine when taking screenshot of DP monitor input.
% Take a screenshot and focus on desired location
robot = java.awt.Robot();
pos = [200 150 500 400]; % [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,[])';
% Show or save to file
% Uncomment below to show image
imshow(imgData)
imwrite(imgData,'out.png')
% Read the image
I = imread("out.png");

2 comentarios

Are you using
get(0,'MonitorPositions')
ans = 1×4
1 1 3840 2160
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
to get the logical positions of the monitors, so that you can figure out the correct relative coordinates to capture? Such as
monpos = get(0,'MonitorPositions');
monpos = monpos(end,:); %last monitor
pos = [200 150 500 400]; % [left top width height]
rect = pos;
rect(1:2) = rect(1:2) + monpos(1:2); %might need to fudge it by +/- 1
cap = robot.createScreenCapture(rect);
Thomas
Thomas el 21 de Jul. de 2025
I gave that a try but it is still capturing the screen showing the DP input from the desktop. I believe I need to get an adapter to read the HDMI input straight into the desktop computer. Thank you for your help!

Iniciar sesión para comentar.

Respuestas (0)

Productos

Versión

R2023b

Etiquetas

Preguntada:

el 15 de Jul. de 2025

Comentada:

el 21 de Jul. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by