Is it possible to take a Screenshot of a website with Matlab?
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Thomas Satterly
il 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 Commenti
Thomas Satterly
il 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).
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!