Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy BufferedImage to clipboard

Tags:

java

How do I store a BufferedImage into the system clipboard?

like image 521
Rendicahya Avatar asked Dec 29 '10 06:12

Rendicahya


People also ask

How to clone a bufferedimage image?

Class BufferedImage does not implement the Cloneable interface. Thus the clone method is not overriden. Another way is to use the Graphics2D class to draw the image onto a new blank image. This doesn't really clone the image, but it results in a copy of the image being produced.

Does Jigar store bufferedimage in the clipboard?

Show activity on this post. Jigar's code does indeed store a BufferedImage into the clipboard, although to be specific, it puts a screen-capture of the entire screen into the clipboard. This may or may not be what you were after.

Does the clone method overriden the deep copy?

Thus the clone method is not overriden. Here's an alternative for a deep copy technique: Java Tip 76: An alternative to the deep copy technique Show activity on this post.


2 Answers

Here is working code taken from here tested successfully

package org.life.java.so.questions;

/**
 * @author Jigar
 */
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.io.*;

public class CopyImagetoClipBoard implements ClipboardOwner {
    public CopyImagetoClipBoard() {
        try {
            Robot robot = new Robot();
            Dimension screenSize  = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screen = new Rectangle( screenSize );
            BufferedImage i = robot.createScreenCapture( screen );
            TransferableImage trans = new TransferableImage( i );
            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
            c.setContents( trans, this );
        }
        catch ( AWTException x ) {
            x.printStackTrace();
            System.exit( 1 );
        }
    }

    public static void main( String[] arg ) {
        CopyImagetoClipBoard ci = new CopyImagetoClipBoard();
    }

    public void lostOwnership( Clipboard clip, Transferable trans ) {
        System.out.println( "Lost Clipboard Ownership" );
    }

    private class TransferableImage implements Transferable {

        Image i;

        public TransferableImage( Image i ) {
            this.i = i;
        }

        public Object getTransferData( DataFlavor flavor )
        throws UnsupportedFlavorException, IOException {
            if ( flavor.equals( DataFlavor.imageFlavor ) && i != null ) {
                return i;
            }
            else {
                throw new UnsupportedFlavorException( flavor );
            }
        }

        public DataFlavor[] getTransferDataFlavors() {
            DataFlavor[] flavors = new DataFlavor[ 1 ];
            flavors[ 0 ] = DataFlavor.imageFlavor;
            return flavors;
        }

        public boolean isDataFlavorSupported( DataFlavor flavor ) {
            DataFlavor[] flavors = getTransferDataFlavors();
            for ( int i = 0; i < flavors.length; i++ ) {
                if ( flavor.equals( flavors[ i ] ) ) {
                    return true;
                }
            }

            return false;
        }
    }
}
like image 167
jmj Avatar answered Oct 02 '22 08:10

jmj


Jigar's code does indeed store a BufferedImage into the clipboard, although to be specific, it puts a screen-capture of the entire screen into the clipboard.

This may or may not be what you were after. In case you wanted to copy your own specific BufferedImage, in order to accomplish this, I replaced the constructor from Jigar's example with a copyImage() method.

public class CopyImagetoClipBoard implements ClipboardOwner
{
    public void copyImage(BufferedImage bi)
    {
        TransferableImage trans = new TransferableImage( bi );
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        c.setContents( trans, this );
    }

Remove the main() method within his class too.

You can then copy your BufferedImage with code such as this:

    BufferedImage bim;
    // set bim to your desired BufferedImage content
    // ...
    CopyImagetoClipBoard ci = new CopyImagetoClipBoard();
    ci.copyImage(bim);
like image 40
Gurce Avatar answered Oct 02 '22 06:10

Gurce