MATLAB Answers

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

22 views (last 30 days)
Lukas Nuschele
Lukas Nuschele on 22 Nov 2019
Commented: Thomas Satterly on 26 Nov 2019
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

  0 Comments

Sign in to comment.

Answers (1)

Thomas Satterly
Thomas Satterly on 22 Nov 2019
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 Comments

Show 3 older comments
Thomas Satterly
Thomas Satterly on 26 Nov 2019
That's strange, it should be working. Could you check the class of the webHandle variable?
class(webHandle)
It should be 'com.mathworks.mde.webbrowser.WebBrowser', which has a 'getLocationOnScreen' method. It sound like your webHandle variable isn't an object for some reason, but that's usually symptomatic of running old Matlab versions
Lukas Nuschele
Lukas Nuschele on 26 Nov 2019
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 on 26 Nov 2019
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).

Sign in to comment.

Sign in to answer this question.