ismail josh
ismail josh

Reputation: 31

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?

Upvotes: 2

Views: 2106

Answers (2)

Subash J
Subash J

Reputation: 2048

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

Upvotes: 4

Bogdan Lukiyanchuk
Bogdan Lukiyanchuk

Reputation: 767

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

Upvotes: 1

Related Questions