2010-12-29 46 views

Trả lời

13

Dưới đây là mã làm việc lấy từ here thử nghiệm thành công

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; 
     } 
    } 
} 
+0

Bạn có thể nhận được ngay với truyền null thay vì một thực hiện ClipboardOwner. –

2

đang Jigar của cửa hàng thực hiện một BufferedImage vào clipboard, mặc dù được cụ thể, nó đặt một màn hình-chụp toàn bộ màn hình vào clipboard.

Điều này có thể hoặc không thể là những gì bạn đã làm sau. Trong trường hợp bạn muốn sao chép BufferedImage cụ thể của riêng bạn, để thực hiện điều này, tôi đã thay thế hàm tạo từ ví dụ của Jigar bằng phương thức copyImage().

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

Xóa phương thức main() trong lớp học của anh ấy.

Sau đó bạn có thể sao chép BufferedImage của bạn với mã như thế này:

BufferedImage bim; 
    // set bim to your desired BufferedImage content 
    // ... 
    CopyImagetoClipBoard ci = new CopyImagetoClipBoard(); 
    ci.copyImage(bim); 
+1

Thứ tốt, nhưng không cần phải tạo ra một lớp hoàn toàn mới ở đó. –

+0

Ồ, đợi đã. Đừng bận tâm. Nếu bất cứ ai khác nghĩ như tôi, lưu ý rằng 'CopyImagetoClipBoard' là một' ClipboardOwner'. –

Các vấn đề liên quan