Java BufferedImage to clipboard gives IIOException

I am trying to copy an image (residing in a BufferedImage object) to the clipboard. I am using the code from this answer .

When I try to insert an image into the program, nothing happens. The GIMP shows a message that no image data was found in the clipboard.


I have also tried the workaround from this article . In fact, 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: every time I try to insert, 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 am on Ubuntu 11.10 x64 and have tried pasting in various programs, including 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)

      


Does anyone face the same problems? I did something wrong, this is a Java bug, is there a workaround?


EDIT: I am using a rather messy workaround for now. I took a little Python script I found on stackoverflow, write Image to a file from Java, and load the file into this script, which copies it to the clipboard. Of course, this is everything except the platform independent. So I'm still hoping for a Java solution.



Yours, Max Weller

+3


source to share


1 answer


You are probably using OpenJDK which does not support JPEG image writing. You can try switching to Sun / Oracle Java.



0


source







All Articles