建立如下函数
function printscreen(picname)
robo = java.awt.Robot;
t = java.awt.Toolkit.getDefaultToolkit();
rectangle = java.awt.Rectangle(t.getScreenSize());
name1=strcat(picname,'.png');
image = robo.createScreenCapture(rectangle);
filehandle = java.io.File(name1);
javax.imageio.ImageIO.write(image,'png',filehandle);
end
输入参数为要保存的文件名称
如
printscreen('屏幕截图')
如果后面要把MATLAB编译为Exe的话,需要引入对应的java包,如下
function printscreen(picname)
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.File;
import javax.imageio.ImageIO;
robo = java.awt.Robot;
t = java.awt.Toolkit.getDefaultToolkit();
rectangle = java.awt.Rectangle(t.getScreenSize());
name1=strcat(picname,'.png');
image = robo.createScreenCapture(rectangle);
filehandle = java.io.File(name1);
javax.imageio.ImageIO.write(image,'png',filehandle);
end