4

I'm trying to copy a image (held in a BufferedImage object) to the clipboard. I'm using the code from this answer.

When trying to paste the image in a program, simply nothing happens. GIMP shows a message saying no image data was found in the clipboard.


I also tried the workaround from this article. Effectively, I changed the constructor to this:

        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 );

This doesn't make it work, but it changes the behavior: Everytime I try to paste, my program displays the following exception in the console:

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)

I'm on Ubuntu 11.10 x64 and tried pasting to various programs, among them 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)

Has anybody run into the same trouble? Did I do something wrong, is it a Java bug, is there a workaround?


EDIT : I'm using a quite dirty workaround at the moment. I took a small Python script I found on stackoverflow, write the Image to file from Java and feed the file to this script which copies it to the clipboard. Of course, this is everything but platform independent. So I'm still hoping for a solution in Java.



Yours, Max Weller

Community
  • 1
  • 1
Mira Weller
  • 2,406
  • 22
  • 27
  • what is the signature of drawImage() - your args don't match any of the signatures I found with a quick look at javadocs. Have you narrowed down exactly which line of the code fails? - your stack trace doesn't go far enough into your code to tell us. – John3136 Mar 17 '12 at 23:57
  • 1. it's [`public abstract boolean drawImage(Image image, int i, int i1, ImageObserver io)`](http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.html#drawImage%28java.awt.Image,%20int,%20int,%20java.awt.image.ImageObserver%29) – Mira Weller Mar 18 '12 at 10:03
  • 2. That's my problem - the stack trace I attached is the whole thing Java tells me. So apparently it is not *directly* in my code. It *doesn't* occur when I *copy* it (-> when I execute the code). But it does occur when I *paste*. The exception is entirely in Java VM's code. Either I give wrong parameters in some place, or the VM has a bug. – Mira Weller Mar 18 '12 at 10:04
  • I also found [this item](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6607163) in Sun's bug tracker, which describes my problem quite good. But it is from last year and I don't really understand what they mean by `State: Closed, Unverified` (is it fixed now?) – Mira Weller Mar 18 '12 at 10:20
  • Sounds like a bug on the receiving end - from the link above, you could try copying a different image format? – John3136 Mar 18 '12 at 10:36
  • I don't understand how to do this? Where can I change the image format? – Mira Weller Mar 18 '12 at 11:42
  • *How could it be on the receiving end?* Pasting doesn't work in any program at all. So I think it is either in my code or in the Java VM. – Mira Weller Apr 01 '12 at 15:09
  • From memory the Sun issue you found was not Java, but something else to do with "something" not handling the "default" image format. I haven't looked at it again, but I remember thinking it is not something easy to fix from your code (assuming it is all related) – John3136 Apr 01 '12 at 21:54

1 Answers1

2

You are probably running OpenJDK, which does not support writing JPEG images. You could try to switch to Sun/Oracle Java.

Witek
  • 6,160
  • 7
  • 43
  • 63