使用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!";
}
}