JavaFX's default way of getting an image from the Windows clipboard,
Clipboard.getSystemClipboard().getImage();
, appears to be broken.
Something seems to go wrong with the transparency of the image. Set on a black background, the image appears fine, but set on a white background, nothing shows at all.
You can test the clipboard using this Minimal, Complete, Verifiable example.
Environment: Windows 7, Java 8 update 202
Below, I'll describe the things I already know.
Yet, no one has gotten at the heart of the problem or received an answer.
I can't seem to find a bug regarding this issue in the Java Bug Database.
This problem with images doesn't occur on the AWT clipboard, but I want a solution that uses the JavaFX clipboard.
I know that the Windows clipboard contains multiple versions of the same thing, just in different formats. This is easy to see using InsideClipboard or Free Clipboard Viewer.
The JavaFX Clipboard recognizes certain formats; sometimes it has different names for them. application/x-java-rawimage
is what Java considers an image; in code you refer to this as DataFormat.IMAGE
.
I suspect that the DIB
clipboard format in Windows matches up with Java's application/x-java-rawimage
, but can't find proof of that in the source code.
JavaFX seems to have this same transparency problem with various applications that copy an image to the clipboard:
.docx
file with images).png
, .jpg
, .gif
, .bmp
)I've also found some applications that copy an image to the clipboard and JavaFX can pull it out using the default method no problem:
An adequate answer should
Image
from the JavaFX clipboard when an image is copied from Adobe Reader.If you can't help, but think this is a well researched question, consider voting, or sharing it with a friend.
In both of your loading methods, the resulting JavaFx Image supports transparency when it shouldn't. However, the AWT loading method uses an intermediate Transferable that uses a non-transparent DirectColorModel to create the Image. As a result the AWT image appears to be correct despite the underlying image still supporting transparency.
Unfortunately this issue stems from a deeper issue that will probably not be fixed, see: https://bugs.openjdk.java.net/browse/JDK-8041459
When java saves image with alpha channel it encode image as YCbCr plus 4th alpha channel. The problem that other applications recognize 4 channels jpeg as RGB or CMYK image, that's why we have wrong color in image. The best solution is to convert image to other color type without alpha channel and only then save it it can't be fixed, the jpeg's spec doesn't specify this moment, it says that color space is application dependent, java decided to code alpha channel as YCbCrA, that's it.
Rather than trying to manually manipulate Image data, an easier fix is probably to blend the Image with a Rectangle and use a Group instead of an ImageView to display it. Here is an example:
private void loadImageFromJavaFXClipboard(final Group group) { System.out.println("Adding an image from the JavaFX Clipboard..."); final Clipboard clipboard = Clipboard.getSystemClipboard(); if (clipboard.hasImage()) { final Image image = clipboard.getImage(); setupImageFixingGroup(group, image); } else { new Alert(Alert.AlertType.INFORMATION, "No image detected on the Clipboard!").show(); group.getChildren().clear(); } } private void setupImageFixingGroup(Group group, Image image) { final ImageView view = new ImageView(image); view.setBlendMode(BlendMode.LIGHTEN); final Rectangle blend = new Rectangle(image.getWidth(), image.getHeight(), Color.BLACK); blend.widthProperty().bind(image.widthProperty()); blend.heightProperty().bind(image.heightProperty()); group.getChildren().clear(); group.getChildren().addAll(blend, view); }
Here is a working version of your Minimal, Verifiable, Complete example: https://pastebin.com/rzhzMui5. I tested it with a number of images including the one in your gif, but have not explicitly tested it against every case you included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With