编写java程序实现对屏幕的截屏(监控)

使用nircmd实现

下载地址:https://launcher.nirsoft.net/downloads/index.html

配置环境变量:

java代码

截图的对象

package com.mzw.huaxiarealestatebackend.screen;

import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.entity.Screenshot;
import com.mzw.huaxiarealestatebackend.service.ScreenshotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.UUID;

@Component
public class ScheduledTask {

    //@Value("${nircmd.path}")
    private String nircmdPath = "D:\\\\nircmd\\\\NirSoft\\\\nircmd.exe";

    private String userId;

    @Autowired
    private ScreenshotService screenshotService;

    // 定时截图任务,每5分钟执行一次
    @Scheduled(fixedDelay = 1 * 60 * 1000)
    public void captureScreenshotTask() {
        if (userId != null) {
            try {
                // 生成随机文件名
                String randomFilename = UUID.randomUUID().toString() + ".png";

                // 构建保存截图的文件路径,可以使用时间戳或其他方式生成唯一的文件名
                //String savePath = "E:\\demos\\realty-admin\\huaxia-real-estate-backend\\pictures\\" + randomFilename;

                String savePath = "./pictures\\" + randomFilename;
                // 构建 nircmd 命令

                String nircmdCommand = nircmdPath + " savescreenshot " + savePath;

                // 执行命令
                Process process = Runtime.getRuntime().exec(nircmdCommand);

                // 你可能需要等待命令执行完成
                process.waitFor();

                System.out.println("Screenshot captured successfully. Filename: " + randomFilename);

                // 模拟获取截图信息
                Screenshot screenshotEntity = new Screenshot();
                screenshotEntity.setUserId(userId);
                screenshotEntity.setFilename(randomFilename);
                screenshotEntity.setFilepath(savePath);
                screenshotEntity.setTimestamp(DateFormat.getNowDateFormat());

                // 保存截图信息到数据库
                screenshotService.save(screenshotEntity);

                // 重置用户ID,避免重复截图
                userId = null;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void setUserId(String newUserId) {
        userId = newUserId;
    }

    public void triggerScreenshotTask() {
        captureScreenshotTask();  // 手动触发
    }
}

实体类

package com.mzw.huaxiarealestatebackend.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

/**
 * (Screenshot)实体类
 *
 * @author makejava
 * @since 2024-01-05 14:07:58
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName("screenshot")
public class Screenshot implements Serializable {
    private static final long serialVersionUID = 669632995665592426L;

    @TableId
    private String id;

    private String userId;

    private String filename;

    private String filepath;

    private String timestamp;

}

ServiceImpl

package com.mzw.huaxiarealestatebackend.service.impl;

import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.dao.ScreenshotDao;
import com.mzw.huaxiarealestatebackend.entity.Screenshot;
import com.mzw.huaxiarealestatebackend.screen.ScheduledTask;
import com.mzw.huaxiarealestatebackend.service.ScreenshotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * (Screenshot)表服务实现类
 *
 * @author makejava
 * @since 2024-01-05 14:08:00
 */
@Service("screenshotService")
public class ScreenshotServiceImpl implements ScreenshotService {
    @Resource
    private ScreenshotDao screenshotDao;

    /**
     * 添加信息
     * @param screenshot
     * @return
     */
    @Override
    public boolean save(Screenshot screenshot) {
        int insert = this.screenshotDao.insert(screenshot);
        return insert > 0;
    }

}

Controller:

package com.mzw.huaxiarealestatebackend.controller;

import com.mzw.huaxiarealestatebackend.screen.ScheduledTask;
import com.mzw.huaxiarealestatebackend.service.ScreenshotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * (Screenshot)表控制层
 *
 * @author makejava
 * @since 2024-01-05 14:07:58
 */
@RestController
@RequestMapping("screenshot")
public class ScreenshotController {
    /**
     * 服务对象
     */
    @Autowired
    private ScreenshotService screenshotService;

    @Autowired
    private ScheduledTask screenshotTask;

    @PostMapping("/startTask")
    public String startScreenshotTask(@RequestParam("userId") String userId) {
        // TODO: 根据实际情况获取用户ID

        // 启动定时截图任务
        screenshotTask.setUserId(userId);
        screenshotTask.triggerScreenshotTask();
        return "Screenshot task started successfully!";
    }

}

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值