Copy BufferedImage to clipboard
How do I store a BufferedI开发者_运维知识库mage into the system clipboard?
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;
}
}
}
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);
as a static accessable util, a modified version of RAnders00
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ClipboardUtil {
private static final Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
private static final ClipboardOwner owner = new Owner();
public static void copy(BufferedImage img) {
copy(new TransferableImage(img));
}
public static void copy(Transferable trans) {
c.setContents(trans, owner);
}
}
class Owner implements ClipboardOwner {
@Override
public void lostOwnership(Clipboard arg0, Transferable arg1) {
System.out.println( "Lost Clipboard Ownership" );
}
}
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;
}
}
A bit more minimized/modernized version of the static utility version by @ope for Java 17.
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
public class ClipboardUtil {
private static final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public static void copyToClipboard(BufferedImage img) {
clipboard.setContents(new TransferableImage(img), null);
}
private record TransferableImage(Image i) implements Transferable {
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
if (flavor.equals(DataFlavor.imageFlavor) && i != null) {
return i;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
@Override
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] flavors = new DataFlavor[1];
flavors[0] = DataFlavor.imageFlavor;
return flavors;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
DataFlavor[] flavors = getTransferDataFlavors();
for (DataFlavor dataFlavor : flavors) {
if (flavor.equals(dataFlavor)) {
return true;
}
}
return false;
}
}
}
精彩评论