0

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

在做了一些研究之后,我发现:当从 Java 剪贴板对象作为图像检索时,Outlook 的剪贴板副本总是设置黑色背景 他说使用 image\x-emf 解决了这个问题。但我不知道如何从“ new DataFlavor("image/x-emf") ”中获取 TransferData

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

提前致谢!


4

1 回答 1

0

解决方案:return new DataFlavor[] { transferData } 很抱歉错过了这么明显的东西。这并没有导致我遇到其他问题,也没有让我实现透明图像,所以我提出了一个新问题希望解决这个问题:将剪贴板设置为透明图像

于 2017-06-08T19:57:00.287 回答