Is it possible to take a Screenshot of a website with Matlab?

조회 수: 8 (최근 30일)
Lukas Nuschele
Lukas Nuschele 2019년 11월 22일
댓글: Thomas Satterly 2019년 11월 26일
Hey there,
I have to do Matlab project for a Digital Image project at university, the task of our project is to get a screenshot of a website and use this screenshot for analyzing it.
My question is how is it possible to open a website, take a screenshot and store the Image somewhere on my Computer?
Thank you for your help,
greets Lukas

답변 (1개)

Thomas Satterly
Thomas Satterly 2019년 11월 22일
Here's one way to do it using some java from withing Matlab. If you want to save the resulting image, see imwrite.
% Open up a webpage
url = 'https://www.mathworks.com/matlabcentral/answers/492597-is-it-possible-to-take-a-screenshot-of-a-website-with-matlab'; % Look familiar?
[~, webHandle] = web(url);
% Wait until the web page is loaded. There may be a way to do this
% programatically, I haven't looked into this
pause(5);
% Get the web page location in the screen space
location = webHandle.getLocationOnScreen();
% Create and set the java rectangle object dimensions
rect = java.awt.Rectangle();
rect.x = location.x;
rect.y = location.y;
rect.width = webHandle.getWidth();
rect.height = webHandle.getHeight();
% Get a screen capture of the window space using a java robot
robot = java.awt.Robot();
capture = robot.createScreenCapture(rect);
% Data time!
rawData = reshape(capture.getRaster().getDataBuffer().getData(), rect.width, rect.height); % Returns an int32 where RGB is store in 8 bytes of each int
rgb = zeros(rect.height, rect.width, 3); % Buffer for RGB triplets so Matlab can plot it. Note how the dimensions are seemingly reversed
rgb(:, :, 1) = bitshift(bitand(rawData', hex2dec('ff0000')), -16); % Red
rgb(:, :, 2) = bitshift(bitand(rawData', hex2dec('ff00')), -8); % Green
rgb(:, :, 3) = bitand(rawData', hex2dec('ff')); % Blue
% Scale from 0-255 to 0-1, because Matlab is just weird
rgb = rgb / 255;
% Plot the image
figure;
image(rgb);
  댓글 수: 6
Lukas Nuschele
Lukas Nuschele 2019년 11월 26일
Hey, I checked it but Matlab doesn´t know this command at all...
>> class(webHandle)
Unrecognized function or variable 'webHandle'.
Do you have an idea what could be the problem? Thx again for your help :)
Thomas Satterly
Thomas Satterly 2019년 11월 26일
I think you've redefined the web function somewhere in your code or are not evaluating the class of the webHandle variable in the same context as the function in which webHandle is created. The second argument returned by the web function should be a handle to the web browser object (webHandle in the code I provided).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by