这是我的线程的最终输出,它截取屏幕截图并将它们存储在向量中。
java.awt.Robot
用于截取屏幕截图,这些截图基本上是屏幕的光栅,不包含光标位置。作为一种解决方法,我使用MouseInfo
类来获取PointerInfo
然后获取Point
. 然后在该点绘制图像。
如果录制区域设置为全屏分辨率,一切都很酷。但是,如果我更改记录区域,光标会被绘制在错误的位置。
这个黑色光标应该位于 Eclipse IDE 顶部的Play按钮上。然而它在错误的位置。
如何在正确的位置绘制它?
代码:
package demo;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
public class ScreenCapturingThread extends Thread{
public ScreenCapturingThread(Vector<BufferedImage> screenShots,
int frameRate,
Icon cursor,
Rectangle recordingArea){
this.screenShots = screenShots;
this.frameRate = frameRate;
this.cursor = cursor;
this.recordingArea = recordingArea;
try{
bot = new Robot();
}catch(Exception e){
System.out.println(e);
}
calculateSleepTime();
}
@Override
public void run(){
while(keepCapturing == true){
try{
screenShots.insertElementAt(takeScreenShot(), 0);
sleep(sleepTime);
keepCapturing = false; //take only one shot
System.out.println("here");
JFrame frame = new JFrame("blah");
frame.setVisible(true);
JLabel label = new JLabel(new ImageIcon(screenShots.firstElement()));
frame.setSize(recordingArea.width, recordingArea.height);
frame.add(label);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public BufferedImage takeScreenShot(){
p = m.getPointerInfo();
Point location = p.getLocation();
image = bot.createScreenCapture(recordingArea);
if(cursor!=null){
Graphics g = image.getGraphics();
g.drawImage(((ImageIcon)cursor).getImage(), location.x,location.y,null);
}
return image;
}
public void stopIt(){
keepCapturing = false;
}
public void calculateSleepTime(){
sleepTime = 1/frameRate;
}
public static void main(String[] args) {
Vector<BufferedImage> bufferedImages = new Vector<>(100);
int frameRate = 10;
Icon cursor = (Icon) new ImageIcon("src/images/blackCursor.png");
Rectangle r = new Rectangle(1280,800);
r.x = 200;
r.y = 200;
ScreenCapturingThread sc = new ScreenCapturingThread(bufferedImages,frameRate,cursor,r);
sc.start();
}
Vector<BufferedImage> screenShots;
int frameRate;
long sleepTime;
boolean keepCapturing = true;
Icon cursor;
Rectangle recordingArea;
Robot bot;
MouseInfo m;
PointerInfo p;
BufferedImage image;
}
如果光标超出记录范围,则不得绘制。我的意思是,你明白了吗?