从自定义DataFlavor

时间:2017-06-07 22:31:07

标签: java clipboard transferable

我想创建一个程序,将文件夹中的图像复制到剪贴板中,但图像变黑。

在做了一些研究后,我发现了这个:Clipboard copy from outlook always has black background set when retrieved as image from Java clipboard object 在那里,他说使用image \ x-emf解决了这个问题。但我无法弄清楚如何从"新的DataFlavor(" image / x-emf")"

package Package1;

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

import javax.swing.ImageIcon;

/** Transferable image */
public class imageSelection implements Transferable {
    private Image image;

    /** Creates a transferable object that is an image. 
     *  <p>imageSelection(Image)
     * */
    public imageSelection(Image image) {
        this.image = image;
    }

    public DataFlavor[] getTransferDataFlavors() {
        //DataFlavor[] BlackBackgroundImage = new DataFlavor[] { DataFlavor.imageFlavor }; // <--- Gives me a black background instead of transparent

        DataFlavor[] transferData = null;
        try {
            transferData =  new DataFlavor("image/x-emf"); // <---- How to get TransferData from this
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            error.displayError(e.getStackTrace(), "Error creating DataFlavor (mime type: image/x-emf)");
        }
        return transferData;
    }

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

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (!DataFlavor.imageFlavor.equals(flavor)) {
            throw new UnsupportedFlavorException(flavor);
        }
        return image;
    }
}

方法调用(其中emojiLocation是图像的路径):

imageSelection imgSel = new imageSelection(new ImageIcon(emojiLocation).getImage());

提前致谢!

1 个答案:

答案 0 :(得分:0)

解决方案:     返回新的DataFlavor [] {transferData} 很抱歉错过了如此明显的事情。 这并没有引起我的其他问题,也没有让我实现透明的形象,所以我提出了一个新的问题,希望能解决这个问题:Set clipboard to transparent image