从自定义 DataFlavor 获取 DataFlavor[]
Get DataFlavor[] from a custom DataFlavor
我想创建一个程序,将文件夹中的图像复制到剪贴板,但图像变黑了。
经过一些研究,我发现了这个:Clipboard copy from outlook always has black background set when retrieved as image from Java clipboard object
他在那里说使用 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());
提前致谢!
解决方法:
return new DataFlavor[] { transferData }
很抱歉遗漏了如此明显的东西。
这并没有导致我遇到其他问题,也没有让我获得透明图像,所以我提出了一个新问题希望能解决这个问题:
我想创建一个程序,将文件夹中的图像复制到剪贴板,但图像变黑了。
经过一些研究,我发现了这个:Clipboard copy from outlook always has black background set when retrieved as image from Java clipboard object 他在那里说使用 image\x-emf 解决了这个问题。但我不知道如何从“ new DataFlavor("image/x-emf") ”
获取 TransferDatapackage 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());
提前致谢!
解决方法:
return new DataFlavor[] { transferData }
很抱歉遗漏了如此明显的东西。
这并没有导致我遇到其他问题,也没有让我获得透明图像,所以我提出了一个新问题希望能解决这个问题: