Thursday, November 17, 2016

Modify Clipboard content using Java

You can modify the text or images in clipboard using Java.

Software Features:

1) It can place any custom text on your clipboard
2) It can place any custom image to your clipboard

Language:
Java

Git Location:
https://github.com/csanuragjain/extra/tree/master/Clipboard/ModifyClipboard

Program:

Main method:
      public static void main(String[] args) throws Exception {  
           Toolkit toolkit = Toolkit.getDefaultToolkit();  
           Clipboard clipboard = toolkit.getSystemClipboard();  
           Scanner s=new Scanner(System.in);  
           System.out.println("Press 1 to copy text and 2 for copying image to clipboard");  
           int ch=s.nextInt();  
           if(ch==1)  
           {  
           setText(clipboard,"Text placed to clipboard by Java");  
           System.out.println("If you press Ctrl+v then you will see the text \"Text placed to clipboard by Java\"");  
           }  
           else if(ch==2)  
           {  
                Robot robot = new Robot();  
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
          Rectangle screen = new Rectangle( screenSize );  
          BufferedImage image = robot.createScreenCapture( screen );  
                setImageFromClipboard(clipboard, image);  
                System.out.println("Copied your desktop screenshot to your clipboard.");  
                System.out.println("Press Ctrl+v on paint and you will see your screenshot");  
           }  
      }  


How it works:
1) We access the clipboard object by using Toolkit.getDefaultToolkit().getSystemClipboard()
2) We ask user if he want to copy text or image on clipboard
3) If user choose to copy text, it calls setText method which will copy the text "Text placed to clipboard by Java" to your clipboard
4) If user choose to copy image then we take screenshot of the screen and call setImageFromClipboard method passing the image we took

setText method:
      public static void setText(Clipboard clipboard, String text)  
      {  
           StringSelection selection = new StringSelection(text);  
           clipboard.setContents(selection, selection);  
      }  


How it works:
1) Make object of StringSelection with the text to be copiedFile
2) we use setContent to set the text in the clipboard

setImageFromClipboard method:
      public static void setImageFromClipboard(Clipboard clipboard, BufferedImage image)  
                throws Exception  
                {  
     ImageSelection imgSel = new ImageSelection(image);  
     clipboard.setContents(imgSel, null );  
                }  


How it works:
1) We created a class ImageSelection since we dont have it predefined.
2) We make an object of ImageSelection and pass the image to be copied
3) we use setcontent and pass the ImageSelection object which copies the image to clipboard

ImageSelection class:
 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;  
  }  

How it works:
1) This class implements Transferable so that we can use it to set image in clipboard
2) The contructor sets the image passed by user
3) getTransferData returns the image which will be placed in clipboard

Sample Output:
Press 1 to copy text and 2 for copying image to clipboard
2
Copied your desktop screenshot to your clipboard.
Press Ctrl+v on paint and you will see your screenshot

Hope it helps :)

No comments:

Post a Comment