gpt4 book ai didi

Java BufferedImage 到剪贴板给出 IIOException

转载 作者:太空狗 更新时间:2023-10-29 11:20:13 25 4
gpt4 key购买 nike

我正在尝试将图像(保存在 BufferedImage 对象中)复制到剪贴板。我正在使用 this answer 中的代码.

当试图将图像粘贴到程序中时,什么也没有发生。 GIMP 显示一条消息,指出在剪贴板中未找到图像数据。


我还尝试了 this article 中的解决方法.实际上,我将构造函数更改为:

        Robot robot = new Robot();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screen = new Rectangle( screenSize );
BufferedImage i = robot.createScreenCapture( screen );

// ----- start of changes from workaround -----
// Work around a Sun bug that causes a hang in "sun.awt.image.ImageRepresentation.reconstruct".
new javax.swing.ImageIcon(i); // Force load.
BufferedImage newImage = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB);
newImage.createGraphics().drawImage(i, 0, 0, null);
i = newImage;
// ----- end of changes from workaround -----

TransferableImage trans = new TransferableImage( i );
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
c.setContents( trans, this );

这不会使其工作,但会改变行为:每次我尝试粘贴时,我的程序都会在控制台中显示以下异常:

javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1055)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:357)
at javax.imageio.ImageWriter.write(ImageWriter.java:615)
at sun.awt.datatransfer.DataTransferer.imageToStandardBytesImpl(DataTransferer.java:2107)
at sun.awt.datatransfer.DataTransferer.imageToStandardBytes(DataTransferer.java:2037)
at sun.awt.X11.XDataTransferer.imageToPlatformBytes(XDataTransferer.java:165)
at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1277)
at sun.awt.datatransfer.DataTransferer$6.run(DataTransferer.java:2208)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

我在 Ubuntu 11.10 x64 上尝试粘贴到各种程序,其中包括 LibreOffice Draw、LibreOffice Writer、GIMP、InkScape。

mw@nb999:~$ java -version
java version "1.6.0_23"
OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.2)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

有人遇到过同样的问题吗?我是不是做错了什么,是否是 Java 错误,是否有解决方法?


编辑: 目前我正在使用一个非常肮脏的解决方法。我使用了一个在 stackoverflow 上找到的小 Python 脚本,将图像从 Java 写入文件并将文件提供给该脚本,该脚本将其复制到剪贴板。当然,这与平台无关。所以我仍然希望在 Java 中找到解决方案。



您的,麦克斯韦勒

最佳答案

您可能正在运行 OpenJDK,它不支持写入 JPEG 图像。您可以尝试切换到 Sun/Oracle Java。

关于Java BufferedImage 到剪贴板给出 IIOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9754693/

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