前言
- 将开发远控软件的步骤进行分布 , 然后将思路和开发步骤教给大家.
屏幕回收其实就是一直截图被控端的整个屏幕 , 然后通过网络传输显示到控制端界面上.
涉及知识点
- 文件流操作
- Robot(机器人)类库的使用(实现截图功能)
- Toolkit类库的使用(实现计算当前屏幕大小)
成功截图
代码
package Remote;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
/** * 远程控制软件之屏幕截图 * @author Administrator * */
public class RemoteTest {
/**获取屏幕截图**/
public BufferedImage getScreenshot() throws Exception {
BufferedImage shotImg = null;
Robot robot = new Robot();
//获取屏幕大小
Toolkit toolkit = Toolkit.getDefaultToolkit().getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
//设置截图的矩形并截图
shotImg= robot.createScreenCapture(new Rectangle(width , height ) );
return shotImg;
}
/**将屏幕截图保存到指定目录下**/
public boolean saveScreenshot(BufferedImage shotImg ,String ext , String savePath ) throws Exception {
//这里可以指定一些压缩算法对图片进行压缩.
FileOutputStream output = new FileOutputStream(new File(savePath ) );
return ImageIO.write(shotImg, ext , output);
}
public static void main(String[] args) throws Exception {
RemoteTest remote = new RemoteTest();
BufferedImage shotImg = remote.getScreenshot();
boolean flag = remote.saveScreenshot(shotImg , "png" , "F:\\JAVA测试\\test.png" );
if(flag ){
System.out.println("success" );
}else{
System.out.println("error!" );
}
}
}