gpt4 book ai didi

image - 如何从 JavaFX 中的剪贴板正确获取图像;有问题的应用程序和没有问题的应用程序有什么区别?

转载 作者:行者123 更新时间:2023-12-03 07:29:31 25 4
gpt4 key购买 nike

问题

JavaFX 从 Windows 剪贴板获取图像的默认方式,

Clipboard.getSystemClipboard().getImage();

,好像坏了。

图像的透明度似乎有问题。设置在黑色背景上,图像看起来不错,但设置在白色背景上,根本没有任何显示。

Showing the JavaFX image transparency issue by changing the background of the application from dark to light.

您可以使用 this Minimal, Complete, Verifiable example 测试剪贴板。

环境:Windows 7、Java 8 update 202

我知道的

下面,我将描述我已经知道的事情。

还有其他人问过类似的问题:
  • Getting Image from Clipboard Awt vs FX
  • Image from clipboard not correctly displayed in JavaFX 8 application

  • 然而,没有人找到问题的核心或得到答案。

    没有官方错误

    我似乎无法在 Java Bug Database 中找到有关此问题的错误。

    AWT 没问题

    这个图像问题不会出现在 AWT 剪贴板上,但我想要一个使用 JavaFX 剪贴板的解决方案。

    On the right is the image from AWT's clipboard, on the left is the same image from JavaFX's clipboard.

    剪贴板包含多种格式

    我知道 Windows 剪贴板包含同一事物的多个版本,只是格式不同。使用 InsideClipboardFree Clipboard Viewer 很容易看到这一点。

    A screenshot of InsideClipboard, showing the CF_DIB format.

    JavaFX 剪贴板识别某些格式;有时它有不同的名称。 application/x-java-rawimage 是 Java 认为的图像;在代码中,您将其称为 DataFormat.IMAGE

    我怀疑 Windows 中的 DIB 剪贴板格式与 Java 的 application/x-java-rawimage 匹配,但在源代码中找不到证明。

    这个问题很普遍

    问题应用

    对于将图像复制到剪贴板的各种应用程序,JavaFX 似乎也存在同样的透明度问题:
  • Adob​​e Reader(来自a PDF with images)
  • Foxit Reader(来自 a PDF with images)
  • Microsoft Word 365(来自 a .docx file with images)
  • Windows 7 的画图(.png.jpg.gif.bmp)
  • Greenshot(这是一个增强的截图工具)
  • Firefox 65.0.2(复制 Google.com 标志)

  • 没有问题的应用程序

    我还发现了一些将图像复制到剪贴板的应用程序,JavaFX 可以使用默认方法将其拉出,没问题:
  • Paint.net
  • PrtScn 按钮
  • Windows 截图工具
  • Google Chrome 72.0.3626.121(复制 Google.com 标志)

  • 回答问题

    一个适当的答案应该
  • 简单、具体地解释,并举例说明为什么某些应用程序会出现问题,而其他应用程序不会,以及
  • 找出 JDK 实现中出现问题的地方,并具体展示如何为大多数有问题的应用程序修复所述实现,而不会破坏已经运行的应用程序。
  • 如果更改 JDK 的实现不可行,则适当的答案将提供 Minimal, Complete, and Verifiable example 显示 JavaFX 代码,当从 Adob​​e Reader 复制图像时,该代码从 JavaFX 剪贴板生成 Image

  • 如果您无法提供帮助,但认为这是一个 well researched 问题,请考虑投票或与 friend 分享。

    最佳答案

    在您的两种加载方法中,生成的 JavaFx 图像在不应该支持透明时都支持透明。但是,AWT 加载方法使用中间 Transferable,它使用不透明的 DirectColorModel 来创建图像。因此,尽管底层图像仍然支持透明度,但 AWT 图像似乎是正确的。

    不幸的是,这个问题源于一个可能无法修复的更深层次的问题,请参阅: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.



    与其尝试手动操作 Image 数据,更简单的解决方法可能是将 Image 与 Rectangle 混合并使用 Group 而不是 ImageView 来显示它。下面是一个例子:
    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);
    }

    这是您的最小、可验证、完整示例的工作版本: https://pastebin.com/rzhzMui5 .我用许多图像(包括您的 gif 中的图像)对其进行了测试,但尚未针对您包含的每种情况进行明确测试。

    关于image - 如何从 JavaFX 中的剪贴板正确获取图像;有问题的应用程序和没有问题的应用程序有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54995198/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com