Disable Screenshot of my Swing Window

2019-02-25 20:02发布

问题:

I have a Swing window that has sensitive information in it and I would like to prevent users from taking screenshots, or making by window invisible when they do take the screenshots.

How can I achieve this?

回答1:

One clever approach I have seen in the past takes advantage of human's persistance of vision by display only a portion of the image at a time.

As a simple example, imagine that instead of rendering the password 'p455w0rd' in a textbox, you had a special text box that renders three different frames very quickly:

  1. p ␣ ␣ 5 ␣ ␣ r ␣
  2. ␣ 4 ␣ ␣ w ␣ ␣ d
  3. ␣ ␣ 5 ␣ ␣ 0 ␣ ␣

Any screenshot would only capture a portion (1/3) of the data and would require that the capturer take many screenshots, select three that encompass all of the information and then combine them in an image editor to recover the data.

A better solution would use more frames (though flicker will increase as the number of frames increases) or finer granularity (pixels, say, instead of characters).

Remember, though, that this solution just makes it harder, not impossible to store the data. You would be hard pressed to stop the user whipping out a pen or camera. Unless this is for some government or call-centre application that is attempting to protect customer data, I would seriously consider not adding such a feature.



回答2:

If someone tried to do this I would run their program in a VM, and then take a screenshot of the VM screen.

So there is no way you could prevent something like this... and why should you?



回答3:

Ubnfortunatly, you won't be able to do so in an efficient fashion.

First, operating system don't seems th send event when they take screenshots (which could have given you a handle - or an event).

Sceond, user can always run your application in a VM (preventing the vent from being sent) or, both worse and easier, take a picture with a cameraphone or any other device.

Finally, what is the point of showing informations that the user can see but not memorize ? If you want your informations to be hidden, don't show them to the user, otherwise he will always be able to copy them (think about the entertainment industry and the issues about DVD screeners, as an example).

Finally, if you want to give your boss some positive information, take a look at jintellitype, which will allow you to register a listener for the "Print screen" key.



回答4:

I have an idea of how this can be done. I have never done this. But I will definitely try this out today.

  1. When you display your window, save all data from clipboard to some variable. (This is not as easy as it sounds.) I am not sure if this is advisable. It would be better if you contact your system admin before implementing this.
  2. Capture KeyEvent. If the key pressed is VK_PRINTSCREEN, clear the system clipboard. (If you have not implemented Step 1, this will lead to loss of all data on clipboard.)
  3. Before you quit the application, transfer all the data you had saved to clipboard.

Here are a few sites that will help you for this.

http://www.javaworld.com/javaworld/javatips/jw-javatip61.html?page=1 http://www.java2s.com/Code/Java/Development-Class/CommunicatingwiththeSystemClipboard.htm



回答5:

save any string to the system clipboard. the screenshoot will be deleted.

public void handleKey(KeyEvent ke) {
switch(ke.getCode()) {
    case PRINTSCREEN : 
            System.out.println("print screen");
            StringSelection ss = new StringSelection("??????");
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
            break;

    default: 
        //System.out.println(ke.getCode());
}

ke.consume();
}