如何解决截屏应用程序中的问题? [英] How do I solve problems in my screenshot-taking application?

查看:118
本文介绍了如何解决截屏应用程序中的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个基于GUI的Java程序,每秒需要25个屏幕截图,并将它们保存在用户定义的位置。

除了有两个问题外,它的效果非常好:

  • 图像中缺少鼠标光标,我知道这是因为 BufferedImage s中不包含光标信息。它们必须以编程方式添加。


  • 截屏的线程是一个守护程序线程。因此,如果我关闭应用程序,线程将被终止并且正在写入的PNG图像被破坏。我想避免这种情况。


  • 以下是我的应用程序的图像:

    屡获殊荣的直观GUI

    I wrote a GUI-based Java program that takes 25 screenshots per second and saves them at a user-defined location.
    It works pretty well except that it has two problems:

  • The mouse cursor is missing from the images and I know it will be because BufferedImages do not contain cursor information in them. They have to be added programatically.

  • The thread that takes screenshots is a daemon thread. So if I close the application, the thread is killed and the PNG image that was being written gets corrupted. I want to avoid that.

  • Here are the images of my application:
    The award-winning, intuitive GUI:

    高清晰度捕获的图像看起来像这样



    从图像中可以看到,使用 MouseInfo 的静态方法在控制台中显示光标信息。




    请让我知道如何解决上述两个问题。

    解决问题后,图片现在看起来像这样:

    The high-definition images captured look like this:

    As you can see from the image the cursor information is being displayed in the console using MouseInfo's static methods.


    Please let me know how to solve the above mentioned two problems.
    After solving the problem, the images now look like this:

    推荐答案


    图像中缺少鼠标光标,我知道它将是
    ,因为BufferedImages不包含光标信息。他们
    必须以编程方式添加。

    The mouse cursor is missing from the images and I know it will be because BufferedImages do not contain cursor information in them. They have to be added programatically.

    这是正确的,你必须在之后添加光标。原因是使用Robot类截取的屏幕截图永远不会包含光标。不是因为BufferedImage不包含鼠标信息。 BufferedImage是一个包含像素栅格的类。

    That is correct, you have to add the cursor afterwards. The reason for this is that a screenshot taken with the Robot class never contains the cursor. Not really because "the BufferedImage doesn't contain mouse info". A BufferedImage is a class that contains a raster of pixels.


    截屏的线程是一个守护程序线程。因此,如果我关闭
    应用程序,线程将被终止并且写入
    的PNG图像被破坏。我想避免这种情况。

    The thread that takes screenshots is a daemon thread. So if I close the application, the thread is killed and the PNG image that was being written gets corrupted. I want to avoid that.

    简单地说,在屏幕截图线程中,使用一个标志,表明它应该继续与否。只要该布尔值设置为true,就继续截取屏幕截图。一定要让它成为非deamon。因此,当您关闭应用程序时,将标志设置为false。
    可能最简单的方法是添加一个WindowListener:

    Simply, in the screenshot thread, use a flag that indicates wether it should continue or not. Keep taking screenshots as long as that boolean is set to true. Make sure to make it non-deamon. So, when you close the application, set the flag to false. Probably the most easy way to do this is to add a WindowListener:

    yourFrame.addWindowListener(new WindowAdapter()
    {
        public void windowClosed(WindowEvent e)
        {
            screenshotThread.stopTakingScreenshots(); // This methods set the flag
        }
    }
    






    另请注意,您没有花时间制作并保存屏幕截图。你使用40毫秒的固定睡眠,但是假设需要4毫秒才能获取并保存屏幕截图,那么你应该只睡36毫秒。要花多长时间才能完成截图,在 takeShot()方法之前和之后使用 System.currentTimeMillis(); 并做出改变。


    Also notice that you are not taking the time it takes to make and save a screenshot in count. You use a fixed sleep of 40 milliseconds, but let's say that it takes 4 milliseconds to take and save the screenshot, then you should sleep for only 36 milliseconds. To time how long it takes to make the screenshot, use System.currentTimeMillis(); before and after your takeShot() method and make the difference.

    这篇关于如何解决截屏应用程序中的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆