Java - copy and paste image

Viewed 35

Fairly simple, I would like to copy an image into the system clipboard using java and be able to past what is currently in the clipboard into another application, such as word, google docs, etc. etc.

At the moment I've found the common

public static void setClipboard(Image image)
{
   ImageTransferable imgSel = new ImageTransferable(image);
   Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
   c.setContents(imgSel, null);
}

static class ImageTransferable implements Transferable
{
    private Image image;

    public ImageTransferable (Image image)
    {
        this.image = image;
    }

    public Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException
    {
        if (isDataFlavorSupported(flavor))
        {
            return image;
        }
        else
        {
            throw new UnsupportedFlavorException(flavor);
        }
    }

    public boolean isDataFlavorSupported (DataFlavor flavor)
    {
        return flavor == DataFlavor.imageFlavor;
    }

    public DataFlavor[] getTransferDataFlavors ()
    {
        return new DataFlavor[] { DataFlavor.imageFlavor };
    }
}

Unfortunately this does not put the image into a clipboard in a paste-able state outside of the java program itself. I can get it to successfully pull from the clipboard (c) to paste the image programmatically in the java program itself but not into another window or application.

1 Answers

I did not test the pasted code but i tried to test another one, with some modification it works correctly for me and copy past an image in a given URL to the SystemClipboard :

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;

import javax.imageio.ImageIO;

public class Test {
    public static void setClipboard(Image image)
    {
       ImageSelection imgSel = new ImageSelection(image);
       Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
    }
    // This class is used to hold an image while on the clipboard.
    static class ImageSelection implements Transferable
    {
      private Image image;

      public ImageSelection(Image image)
      {
        this.image = image;
      }
      // Returns supported flavors
      public DataFlavor[] getTransferDataFlavors()
      {
        return new DataFlavor[] { DataFlavor.imageFlavor };
      }
      // Returns true if flavor is supported
      public boolean isDataFlavorSupported(DataFlavor flavor)
      {
        return DataFlavor.imageFlavor.equals(flavor);
      }

      // Returns image
      public Object getTransferData(DataFlavor flavor)
          throws UnsupportedFlavorException, IOException
      {
        if (!DataFlavor.imageFlavor.equals(flavor))
        {
          throw new UnsupportedFlavorException(flavor);
        }
        return image;
      }
    }
    
    
    
    public static void main(String[] args) throws IOException {

        // put the url of your image 
        URL url = Paths.get("C:\\Users\\CTW\\Desktop\\oussama _pic.png").toUri().toURL();

        // read an image from url
        BufferedImage image = ImageIO.read(url);

        // resize image to 300x150
        Image scaledImage = image.getScaledInstance(300, 150, Image.SCALE_DEFAULT);
        // set the image to the system clipboard
          setClipboard(scaledImage);
          
          // here open your Paint editor and click ctrl+v and you will see your image pasted there 
    }
}

Some code copied from here

Related