如何在Java中从剪贴板写入映像到磁盘?

时间:2018-04-10 19:22:00

标签: java image

 Clipboard clipboard = Clipboard.getSystemClipboard();
 if ( clipboard.hasImage()) {
        BufferedImage img = (BufferedImage) clipboard.getImage();
        ImageIO.write(img, "jpg", new File("outout.jpg"));
 }

ImageIO.write需要BufferedImage,但clipboard.getImage()具有Image类型。我无法将Image转换为BufferedImage? 如何从剪贴板将图像写入文件?

2 个答案:

答案 0 :(得分:1)

您还可以使用Toolkit.getDefaultToolkit().getSystemClipboard()获取系统剪贴板。然后按java.awt.datatransfer.Transferable获取clipboard.getContents(),然后使用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);
    }
}

答案 1 :(得分:0)

此代码有效click

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