简体   繁体   中英

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 requires BufferedImage, however clipboard.getImage() has Image type. I cannot cast Image to BufferedImage? How can I write images to a file from clipboard?

You can also use Toolkit.getDefaultToolkit().getSystemClipboard() to get the system clipboard. Then get java.awt.datatransfer.Transferable by clipboard.getContents() and then check for image data type using content.getTransferData(DataFlavor.imageFlavor) . Please refer this link for the detailed code save-image-from-clipboard-to-file

Code snippet:

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

This code works 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM