将图像设置到剪贴板 - Java

发布于 2024-12-10 20:00:32 字数 581 浏览 0 评论 0原文

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

    //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 之后剪贴板为空;任何想法为什么以及如何解决它?

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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初雪 2024-12-17 20:00:32

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

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);
    }
}

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);
    }
}
染火枫林 2024-12-17 20:00:32

DataHandler 将不会按照您规定的方式运行,因为根据 API:

DataHandler 实现了 Transferable 接口,以便可以在 AWT 数据传输操作中使用数据,例如剪切、粘贴和拖放。 Transferable 接口的实现依赖于已安装的 DataContentHandler 对象的可用性,该对象与 DataHandler 的特定实例中表示的数据的 MIME 类型相对应。

我的理解是,除非您使用其 setDataContentHandlerFactory 方法并实现其中所有必需的接口,否则 DataHandler 本质上只会返回 null。这可能是 DataHandler 无法按您预期运行的原因。尽管它实现了 Transferable 接口,但它的实现方式并不能充分满足您的特定需求。
此功能将由 DataContentHandler 提供,其实现将由您决定。

按照之前的建议直接实现 Transferable 类似乎不那么繁琐。

DataHandler will not function in the way you've prescribed because according to the API:

DataHandler implements the Transferable interface so that data can be used in AWT data transfer operations, such as cut and paste and drag and drop. The implementation of the Transferable interface relies on the availability of an installed DataContentHandler object corresponding to the MIME type of the data represented in the specific instance of the DataHandler.

I understand this to mean that unless you use its setDataContentHandlerFactory method and implement all required interfaces therein, essentially, DataHandler will simply return null. This is likely the reason why DataHandler does not function as you expect. Even though it implements the Transferable interface, it does not implement it in a way that functions adequately for your particular needs.
This functionality would be given by a DataContentHandler whose implementation would be left up to you.

It seems less tedious to directly implement the Transferable class as suggested prior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文