使用 xuggler 使用鼠标指针捕获屏幕

发布于 2024-11-04 02:31:34 字数 3541 浏览 4 评论 0原文

以下是使用 xuggler 捕获屏幕并从中制作视频的代码,它工作正常,但生成的视频中没有鼠标光标。我还想捕获鼠标指针以显示鼠标活动。

有人可以指导我吗?

package com.test.video;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;


import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.ICodec;

public class ScreenRecordingExample {

    private static final double FRAME_RATE = 5;

    private static final int SECONDS_TO_RUN_FOR = 120;

    private static final String outputFilename = "c:/mydesktop.mp4";

    private static Dimension screenBounds;

    private static Graphics2D imageGraphics;

    public static Image m_MouseIcon = null;

    public static void main(String[] args) {

        // let's make a IMediaWriter to write the file.
        final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);

        screenBounds = Toolkit.getDefaultToolkit().getScreenSize();

        // We tell it we're going to add one video stream, with id 0,
        // at position 0, and that it will have a fixed frame rate of FRAME_RATE.
        writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, screenBounds.width/2, screenBounds.height/2);

        long startTime = System.nanoTime();

        try {
            m_MouseIcon = ImageIO.read(ScreenRecordingExample.class.getResource("resource/captionMouseIcon.png"));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++) {

            // take the screen shot
            BufferedImage screen = getDesktopScreenshot();

            // convert to the right image type
            BufferedImage bgrScreen = convertToType(screen, BufferedImage.TYPE_3BYTE_BGR);

            // encode the image to stream #0
            writer.encodeVideo(0, bgrScreen, System.nanoTime() - startTime, TimeUnit.NANOSECONDS);

            // sleep for frame rate milliseconds
            try {
                Thread.sleep((long) (1000 / FRAME_RATE));
            } 
            catch (InterruptedException e) {
                // ignore
            }

        }

        // tell the writer to close and write the trailer if  needed
        writer.close();

    }

    public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {

        BufferedImage image;

        // if the source image is already the target type, return the source image
        if (sourceImage.getType() == targetType) {
            image = sourceImage;
        }
        // otherwise create a new image of the target type and draw the new image
        else {
            image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType);
            image.getGraphics().drawImage(sourceImage, 0, 0, null);
        }

        return image;

    }

    private static BufferedImage getDesktopScreenshot() {
        try {
            Robot robot = new Robot();
            Rectangle captureSize = new Rectangle(screenBounds);
            return robot.createScreenCapture(captureSize);
        } 
        catch (AWTException e) {
            e.printStackTrace();
            return null;
        }

    }

}

谢谢,

following is the code to capture screen and make video from it using xuggler and it is working fine, but the generated video does not have mouse cursor in it. I also want to capture mouse pointer to show the mouse activity.

Can some one guide me on this.

package com.test.video;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;


import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.ICodec;

public class ScreenRecordingExample {

    private static final double FRAME_RATE = 5;

    private static final int SECONDS_TO_RUN_FOR = 120;

    private static final String outputFilename = "c:/mydesktop.mp4";

    private static Dimension screenBounds;

    private static Graphics2D imageGraphics;

    public static Image m_MouseIcon = null;

    public static void main(String[] args) {

        // let's make a IMediaWriter to write the file.
        final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);

        screenBounds = Toolkit.getDefaultToolkit().getScreenSize();

        // We tell it we're going to add one video stream, with id 0,
        // at position 0, and that it will have a fixed frame rate of FRAME_RATE.
        writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, screenBounds.width/2, screenBounds.height/2);

        long startTime = System.nanoTime();

        try {
            m_MouseIcon = ImageIO.read(ScreenRecordingExample.class.getResource("resource/captionMouseIcon.png"));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++) {

            // take the screen shot
            BufferedImage screen = getDesktopScreenshot();

            // convert to the right image type
            BufferedImage bgrScreen = convertToType(screen, BufferedImage.TYPE_3BYTE_BGR);

            // encode the image to stream #0
            writer.encodeVideo(0, bgrScreen, System.nanoTime() - startTime, TimeUnit.NANOSECONDS);

            // sleep for frame rate milliseconds
            try {
                Thread.sleep((long) (1000 / FRAME_RATE));
            } 
            catch (InterruptedException e) {
                // ignore
            }

        }

        // tell the writer to close and write the trailer if  needed
        writer.close();

    }

    public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {

        BufferedImage image;

        // if the source image is already the target type, return the source image
        if (sourceImage.getType() == targetType) {
            image = sourceImage;
        }
        // otherwise create a new image of the target type and draw the new image
        else {
            image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType);
            image.getGraphics().drawImage(sourceImage, 0, 0, null);
        }

        return image;

    }

    private static BufferedImage getDesktopScreenshot() {
        try {
            Robot robot = new Robot();
            Rectangle captureSize = new Rectangle(screenBounds);
            return robot.createScreenCapture(captureSize);
        } 
        catch (AWTException e) {
            e.printStackTrace();
            return null;
        }

    }

}

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥醉 2024-11-11 02:31:35

使用 MouseInfo.getPointerInfo().getLocation() 获取指针的当前位置。在 BufferedImage 上的 Point 处绘制一个“闭合传真”(1) 指针。


1) 对于 Screenshooter,我使用指针的 GIF 图像。

Use MouseInfo.getPointerInfo().getLocation() to obtain the current position of the pointer. Draw a 'close facsimile of'(1) the pointer at that Point on the BufferedImage.


1) For Screenshooter, I use a GIF image of a pointer.

捶死心动 2024-11-11 02:31:35

您可以使用 Java Native Access (JNA) 实时检索操作系统特定的鼠标光标类型,然后从系统文件(Windows 中的 user32.dll)中提取其图像并覆盖在屏幕截图上。

You can use Java Native Access(JNA) in order to retrieve OS specific mouse cursor type in real time and then extract its image from system file(user32.dll in windows) and overlay on the screen shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文