save image from clipboard to file

jython script to save image from system clipboard to a file

from java.io                import File
from java.awt               import Toolkit
from java.awt.datatransfer  import DataFlavor
from javax.imageio          import ImageIO

content=Toolkit.getDefaultToolkit().getSystemClipboard().getContents(None)
if content and content.isDataFlavorSupported(DataFlavor.imageFlavor):
    img = content.getTransferData(DataFlavor.imageFlavor)
    ImageIO.write(img,'png',File('c:/temp/1.png'))

java code

import java.io.*;
import java.awt.*;
import java.awt.image.*; 
import java.awt.datatransfer.*;
import javax.imageio.*;

public class clipimg
{
    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);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s