返回顶部

收藏

java操作系统剪贴板内容数据

更多

客户有各种稀奇的需求,现要求在使用平台过程中复制过的内容保存并展示供使用人员参考

ClipboradOperate.java

package cn.net.ssd.common.format;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class ClipboradOperate {
    /**@Description: 
     * @author zhk
     * @createtime 2012-7-12 上午11:33:06   
     * @param args
     */
    public static void main(String[] args) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//获取系统剪贴板

        try {
            ImageViewer im=new ImageViewer(getImageFromClipboard());
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 从指定的剪切板中获取文本内容
     * 本地剪切板使用 Clipborad cp = new Clipboard("clip1"); 来构造
     * 系统剪切板使用  Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
     * 剪切板的内容   getContents(null); 返回Transferable
     */
    protected static String getClipboardText() throws Exception{
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//获取系统剪贴板
        // 获取剪切板中的内容
        Transferable clipT = clip.getContents(null);
        if (clipT != null) {
        // 检查内容是否是文本类型
        if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
        return (String)clipT.getTransferData(DataFlavor.stringFlavor); 
        }
        return null;
    }

     //往剪切板写文本数据
     protected static void setClipboardText(Clipboard clip, String writeMe) {
         Transferable tText = new StringSelection(writeMe);
         clip.setContents(tText, null);
     }

     // 从剪切板读取图像
     public static Image getImageFromClipboard() throws Exception{
         Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
         Transferable cc = sysc.getContents(null);
         if (cc == null)
         return null;
         else if(cc.isDataFlavorSupported(DataFlavor.imageFlavor))
         return (Image)cc.getTransferData(DataFlavor.imageFlavor);
         return null;
     }
     // 写图像到剪切板
     protected static void setClipboardImage2(final Image image) {
         Transferable trans = new Transferable(){

         public DataFlavor[] getTransferDataFlavors() {
         return new DataFlavor[] { DataFlavor.imageFlavor };
     }

     public boolean isDataFlavorSupported(DataFlavor flavor) {
         return DataFlavor.imageFlavor.equals(flavor);
     }

     public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
         if(isDataFlavorSupported(flavor))
         return image;
         throw new UnsupportedFlavorException(flavor);
         }
         };
         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
     }

}

ImageViewer.java

package cn.net.ssd.common.format;

/*
* 查看图形的窗口
*/
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
*  用于显示图形的窗口
*/
public class ImageViewer extends Frame {
    private Image image;
    /**
    * 显示一个图像
    * @param viewMe
    */
    public ImageViewer(Image viewMe) {
    image = viewMe;
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 0);
    try {
    mediaTracker.waitForID(0);
    } catch (InterruptedException ie) {
    ie.printStackTrace();
    System.exit(1);
    }
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    //窗口适应图像大小
    setSize(image.getWidth(null), image.getHeight(null));
    //窗口标题
    setTitle("Viewing Image from Clipboard"); 
    setVisible(true);
    }
    public void paint(Graphics graphics) {
    graphics.drawImage(image, 0, 0, null);
    }
    /**
    * 用于读取图像文件并生成Image对象
    */
    public static Image getImageFromFile(String fileName) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage(fileName);
    return image;
    }
}

标签:java,剪贴板

收藏

0人收藏

支持

0

反对

0

发表评论