0

ここに画像の説明を入力

これは、スクリーンショットを取得してベクターに保存するスレッドの最終出力です。
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;
}  

カーソルが記録範囲外に出る場合は、描画してはなりません。つまり、あなたは要点を正しく理解していますか?

フルスクリーン解像度で出力

ここに画像の説明を入力

4

2 に答える 2

1

ポインタの位置が記録範囲内にあるかどうかを確認することはないため、実行ボタンと同じ位置にポインタを描画しています。また、記録範囲に合わせてポインタの位置をスケーリングすることもできます。

たとえば、次のようにすることで、ポインタが表示長方形から左または上にあるかどうかを確認できます(オフセット変数は基本的に、記録を開始した左右からどれだけ離れているかです)。

p = m.getPointerInfo();
Point location = p.getLocation();
image = bot.createScreenCapture(recordingArea);
//Check location here.
if( location.x < recordingArea.x + <<offset x variable>> || 
    location.y < recordingArea.y + <<offset y variable>>){
     //Code to change cursor position to outside of viewing rectangle here.
}
if(cursor!=null){
      Graphics g = image.getGraphics();
      g.drawImage(((ImageIcon)cursor).getImage(), location.x,location.y,null);
}
于 2012-12-23T21:33:13.247 に答える
1

もちろん、グローバルではなく、相対的な位置にカーソルを描画する必要があります。

    p = m.getPointerInfo();
    Point location = p.getLocation();

画面上のマウスの位置を示します (グローバル)。画像座標(相対)でのマウスの位置は次のようになります。

    int x = (int) (p.x - recordingArea.getX()); //for int y similar
    if(x < 0 || y < 0 || y >= maxHeight || x >= maxWidth) { // don't draw cursor }
于 2012-12-23T21:22:07.320 に答える