2010-05-09

利用 Java 採取系統剪貼板的圖像 (Use Java to capture image from system clipboard)

import java.awt.Toolkit;
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 static BufferedImage getSystemImageClipboard() throws IOException, UnsupportedFlavorException{
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)){
        return (BufferedImage)t.getTransferData(DataFlavor.imageFlavor);
    } else {
        return null;
    }
}

沒有留言 :

張貼留言