将图像设置为剪贴板

将图像设置为剪贴板

本文介绍了将图像设置为剪贴板-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java编写一个程序,该程序通过套接字发送剪贴板内容;我设法使它可以与字符串一起使用,但在处理图像时遇到了一些麻烦。下面是代码:

I'm making a program using java that sends the clipboard contents over sockets; I managed to make it work with strings but I'm having some troubles with images. Here is the code:

    //get Image
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Image imageContents = (Image)clipboard.getData(DataFlavor.imageFlavor);
    ImageIcon image = new ImageIcon(imageContents);

    //sent over sockets

    //set Image
    String mime = DataFlavor.imageFlavor.getMimeType();
    DataHandler contents = new DataHandler(image,mime);

    //set clipboard
    clipboard.setContents(contents, null);

在setContents之后剪贴板为空;任何想法为什么以及如何解决?

After setContents the clipboard is empty; Any ideas why, and how to fix it?

推荐答案

下面是一些我用来从中写入/读取图像的代码剪贴板。从来没有尝试过使用套接字,所以我不确定它是否会帮助:

Here is some code I've used to write/read an Image from the clipboard. Never tried it with sockets so I'm not sure it will help:

import java.awt.*;
import java.awt.datatransfer.*;

public class ClipboardImage
{
    /**
     *  Retrieve an image from the system clipboard.
     *
     *  @return the image from the clipboard or null if no image is found
     */
    public static Image read()
    {
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents( null );

        try
        {
            if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor))
            {
                Image image = (Image)t.getTransferData(DataFlavor.imageFlavor);
                return image;
            }
        }
        catch (Exception e) {}

        return null;
    }

    /**
     *  Place an image on the system clipboard.
     *
     *  @param  image - the image to be added to the system clipboard
     */
    public static void write(Image image)
    {
        if (image == null)
            throw new IllegalArgumentException ("Image can't be null");

        ImageTransferable transferable = new ImageTransferable( image );
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
    }

    static class ImageTransferable implements Transferable
    {
        private Image image;

        public ImageTransferable (Image image)
        {
            this.image = image;
        }

        public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException
        {
            if (isDataFlavorSupported(flavor))
            {
                return image;
            }
            else
            {
                throw new UnsupportedFlavorException(flavor);
            }
        }

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

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

    public static void main(String[] args)
    {
        Image image = Toolkit.getDefaultToolkit ().createImage("???.jpg");
        ClipboardImage.write( image );

        javax.swing.ImageIcon icon = new javax.swing.ImageIcon( ClipboardImage.read() );
        javax.swing.JLabel label = new javax.swing.JLabel( icon );

        javax.swing.JFrame frame = new javax.swing.JFrame();
        frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( label );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

这篇关于将图像设置为剪贴板-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:26