これは、スクリーンショットを取得してベクターに保存するスレッドの最終出力です。
java.awt.Robot
基本的に画面のラスターであり、カーソル位置を含まないスクリーンショットを撮るために使用されます。MouseInfo
回避策として、クラスを使用してを取得PointerInfo
してから取得しPoint
ます。次に、その時点でイメージを描きます。
録画領域が全画面解像度に設定されている場合、すべてがクールです。ただし、記録領域を変更すると、カーソルが間違った位置に描画されます。この黒いカーソルは、Eclipse IDE の上部にある [再生
] ボタンに
あるはずです。しかし、それは間違った位置にあります。
正しい位置に描画するにはどうすればよいですか?
コード:
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;
}
カーソルが記録範囲外に出る場合は、描画してはなりません。つまり、あなたは要点を正しく理解していますか?