我正在使用Applet从剪贴板保存图像。图像被保存,但其格式发生了一些变化。它变暗,失去了颜色。使用小应用程序从Mac OSX上的剪贴板抓取图像
这里就是我正在做它:
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
//create clipboard object
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//Get data from clipboard and assign it to an image.
//clipboard.getData() returns an object, so we need to cast it to a BufferdImage.
BufferedImage image = (BufferedImage) clipboard.getData(DataFlavor.imageFlavor);
//file that we'll save to disk.
File file = new File("/tmp/clipboard.jpg");
//class to write image to disk. You specify the image to be saved, its type,
// and then the file in which to write the image data.
ImageIO.write(image, "jpg", file);
//getData throws this.
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
return "Não tem imagem na área de transferência";
} catch (Exception ioe){
ioe.printStackTrace();
}
return null;
}
}
);
,我读了Mac使用不同的图像格式,但我没有找到如何将其转化为一种格式,我可以节省。我想到java应该照顾好这一点。
那么,我该如何将图像从剪贴板转换为jpg?
PS。我尝试使用PNG而不是JPG,得到了更糟的结果:黑色图像
当您尝试访问系统剪贴板时,请注意这只能用于JDK 1.6。截至2013年10月6日,JDK 1.7中存在一个错误:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8019465 – 2013-10-05 11:33:18