[英]How to Write Image To Disk From Clipboard in Java?
Clipboard clipboard = Clipboard.getSystemClipboard();
if ( clipboard.hasImage()) {
BufferedImage img = (BufferedImage) clipboard.getImage();
ImageIO.write(img, "jpg", new File("outout.jpg"));
}
ImageIO.write需要BufferedImage,但是剪貼板.getImage()具有Image類型。 我無法將Image轉換為BufferedImage嗎? 如何將圖像從剪貼板寫入文件?
您還可以使用Toolkit.getDefaultToolkit().getSystemClipboard()
來獲取系統剪貼板。 然后通過clipboard.getContents()
獲取java.awt.datatransfer.Transferable
,然后使用content.getTransferData(DataFlavor.imageFlavor)
檢查圖像數據類型。 請參考此鏈接以獲取詳細的代碼save-image-from-clipboard-to-file
程式碼片段:
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.datatransfer.*;
import javax.imageio.*;
public class ClipboardToImageData {
public static void main(String[] args) throws Exception {
System.err.println("usage: java clipimg [filename]");
String outputfile = "/temp/1.png";
if (args.length > 0)
outputfile = args[0];
copyTo(outputfile);
}
static int copyTo(String filename) throws Exception {
Transferable content = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (content == null) {
System.err.println("error: nothing found in clipboard");
return 1;
}
if (!content.isDataFlavorSupported(DataFlavor.imageFlavor)) {
System.err.println("error: no image found in clipbaord");
return 2;
}
BufferedImage img = (BufferedImage) content.getTransferData(DataFlavor.imageFlavor);
String ext = ext(filename);
if (ext == null) {
ext = "png";
filename += "." + ext;
}
File outfile = new File(filename);
ImageIO.write(img, ext, outfile);
System.err.println("image copied to: " + outfile.getAbsolutePath());
return 0;
}
static String ext(String filename) {
int pos = filename.lastIndexOf('.') + 1;
if (pos == 0 || pos >= filename.length())
return null;
return filename.substring(pos);
}
}
此代碼有效, 請點擊
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.datatransfer.*;
import javax.imageio.*;
public class Main {
public static void main(String[] args)
throws Exception {
System.err.println("usage: java clipimg [filename]");
String outputfile = "F:/temp/1.png";
if (args.length > 0) outputfile = args[0];
copyTo(outputfile);
}
static int copyTo(String filename) throws Exception {
Transferable content = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (content == null) {
System.err.println("error: nothing found in clipboard");
return 1;
}
if (!content.isDataFlavorSupported(DataFlavor.imageFlavor)) {
System.err.println("error: no image found in clipbaord");
return 2;
}
BufferedImage img = (BufferedImage) content.getTransferData(DataFlavor.imageFlavor);
String ext = ext(filename);
if (ext == null) {
ext = "png";
filename += "." + ext;
}
File outfile = new File(filename);
ImageIO.write(img, ext, outfile);
System.err.println("image copied to: " + outfile.getAbsolutePath());
return 0;
}
static String ext(String filename) {
int pos = filename.lastIndexOf('.') + 1;
if (pos == 0 || pos >= filename.length()) return null;
return filename.substring(pos);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.