Code to open image by pasting URL in ImageJ

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Code to open image by pasting URL in ImageJ

Josh Doe-2
I've modified Clipboard.java to support pasting a URL and using
URLOpener to open it if it is an image. I think this would be useful
to others.

I would also suggest reordering URLOpener.java so that URLS are
treated the same regardless of whether they're entered in the
"File>Import>URL..." dialog box or called via the URLOpener.run()
method. For example, right now pasting an image URL works, but pasting
a text URL does not.

Below is the full code for showSystemClipboard() and below that is the diff.

-Josh D

<----- BEGIN CODE ----->
        void showSystemClipboard() {
                if (!setup()) return;
                IJ.showStatus("Opening system clipboard...");
                try {
                        Transferable transferable = clipboard.getContents(null);
                        boolean imageSupported =
transferable.isDataFlavorSupported(DataFlavor.imageFlavor);
                        boolean textSupported =
transferable.isDataFlavorSupported(DataFlavor.stringFlavor);

                        if (!imageSupported && IJ.isMacOSX() && displayMacImage(transferable))
                                return;
                        if (imageSupported) {
                                Image img = (Image)transferable.getTransferData(DataFlavor.imageFlavor);
                                if (img==null) {
                                        IJ.error("Unable to convert image on system clipboard");
                                        IJ.showStatus("");
                                        return;
                                }
                                int width = img.getWidth(null);
                                int height = img.getHeight(null);
                                BufferedImage   bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
                                Graphics g = bi.createGraphics();
                                g.drawImage(img, 0, 0, null);
                                g.dispose();
                                WindowManager.checkForDuplicateName = true;
                                new ImagePlus("Clipboard", bi).show();
                        } else if (textSupported) {
                                String text = (String)transferable.getTransferData(DataFlavor.stringFlavor);
                                if(text.toUpperCase().startsWith("HTTP://")) {
                                        IJ.runPlugIn("ij.plugin.URLOpener",text);
                                }
                                IJ.showStatus("");
                        } else
                                IJ.error("Unable to find an image on the system clipboard");
                } catch (Throwable t) {
                        IJ.showStatus(""+t);
                }
        }
<----- END CODE ----->

<----- BEGIN DIFF ----->
--- C:/Temp/source/ij/plugin/Clipboard.java Thu Oct 19 10:20:44 2006
+++ X:/ImageJ/source/ij/plugin/Clipboard.java Fri Jan 19 10:46:22 2007
@@ -29,7 +29,7 @@
  copy(true);
    else if (arg.equals("scopy"))
  copyToSystem();
- else if (arg.equals("showsys"))
+ else if (arg.equals("spaste"))
  showSystemClipboard();
  else if (arg.equals("show"))
  showInternalClipboard();
@@ -80,10 +80,12 @@
  IJ.showStatus("Opening system clipboard...");
  try {
  Transferable transferable = clipboard.getContents(null);
- boolean supported =
transferable.isDataFlavorSupported(DataFlavor.imageFlavor);
- if (!supported && IJ.isMacOSX() && displayMacImage(transferable))
+ boolean imageSupported =
transferable.isDataFlavorSupported(DataFlavor.imageFlavor);
+ boolean textSupported =
transferable.isDataFlavorSupported(DataFlavor.stringFlavor);
+
+ if (!imageSupported && IJ.isMacOSX() && displayMacImage(transferable))
  return;
- if (supported) {
+ if (imageSupported) {
  Image img = (Image)transferable.getTransferData(DataFlavor.imageFlavor);
  if (img==null) {
  IJ.error("Unable to convert image on system clipboard");
@@ -98,6 +100,12 @@
  g.dispose();
  WindowManager.checkForDuplicateName = true;
  new ImagePlus("Clipboard", bi).show();
+ } else if (textSupported) {
+ String text =
(String)transferable.getTransferData(DataFlavor.stringFlavor);
+ if(text.toUpperCase().startsWith("HTTP://")) {
+ IJ.runPlugIn("ij.plugin.URLOpener",text);
+ }
+ IJ.showStatus("");
  } else
  IJ.error("Unable to find an image on the system clipboard");
  } catch (Throwable t) {
<----- END DIFF ----->