Customers have various unique needs and now require that the content copied during the use of the platform be saved and displayed for users' reference.
- 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 AM
- * @param args
- */
- public static void main(String[] args) {
- Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//Get the system clipboard
-
- try {
- ImageViewer im=new ImageViewer(getImageFromClipboard( ));
- }catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Get the text content from the specified clipboard
- * The local clipboard uses Clipborad cp = new Clipboard("clip1"); to construct
- * The system clipboard uses Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard( );
- * Clipboard content getContents(null); Return Transferable
- */
- protected static String getClipboardText() throws Exception{
- Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); // Get the system clipboard
- // Get the contents of the clipboard
- Transferable clipT = clip.getContents(null);
- if (clipT != null) {
- // Check whether the content is text type
- if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
- return (String)clipT.getTransferData(DataFlavor.stringFlavor);
- }
- return null;
- }
-
- //Go to cut Board write text data
- protected static void setClipboardText(Clipboard clip, String writeMe) {
- Transferable tText = new StringSelection(writeMe);
- clip.setContents(tText, null);
- }
-
- // Read images from the clipboard
- 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;
- }
- // Write image to clipboard
- 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.getDefaultTool kit( ).getSystemClipboard().setContents(trans, null);
- }
-
-
- }
-
Copy code
- 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;
- /**
- * Window used to display graphics
- */
- public class ImageViewer extends Frame {
- private Image image;
- /**
- * Show an 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);
- }
- /**
- * Used to read image files and generate Image objects
- */
- public static Image getImageFromFile(String fileName) {
- Toolkit toolkit = Toolkit.getDefaultToolkit();
- Image image = toolkit.getImage(fileName);
- return image;
- }
- }
复制代码
|