如何将BufferedImage存储到系统剪贴板中?
答案 0 :(得分:14)
以下是从here成功测试的工作代码
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;
}
}
}
答案 1 :(得分:2)
Jigar的代码确实将BufferedImage存储到剪贴板中,但具体来说,它将整个屏幕的屏幕截图放入剪贴板。
这可能是也可能不是你所追求的。如果您想复制自己的特定BufferedImage
,为了实现这一点,我使用copyImage()
方法替换了Jigar示例中的构造函数。
public class CopyImagetoClipBoard implements ClipboardOwner
{
public void copyImage(BufferedImage bi)
{
TransferableImage trans = new TransferableImage( bi );
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
c.setContents( trans, this );
}
也删除了他班级中的main()
方法。
然后,您可以使用以下代码复制BufferedImage:
BufferedImage bim;
// set bim to your desired BufferedImage content
// ...
CopyImagetoClipBoard ci = new CopyImagetoClipBoard();
ci.copyImage(bim);
答案 2 :(得分:0)
作为一个静态可访问的工具,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;
}
}