participate


Java Programming [Archive] - BufferedImage to clipboard GIF to PowerPoint
<<   Back to Forum  |   Give us Feedback
5 Duke Stars rewarded for this thread
This topic has 8 replies on 1 page.
drewa2
Posts:4
Registered: 12/1/04
BufferedImage to clipboard GIF to PowerPoint   
Dec 1, 2004 12:16 PM

 
Hello All:

I know there has been a lot of discussion about copying images to the clipboard, and believe me, I've read them all about 20 times each. Believe it or not, I still haven't found an answer that helps. I'm hoping someone will take the time to help me out. My actual applet is more complex than the one I provide here, but I've simplified it down to make it easier to post and discuss.

I have an applet that reads in an image, stores it to a BufferedImage and an array of pixel values. Now I want to put that image on the System clipboard so that I can switch windows to PowerPoint, and paste the image in the PPT document. When I do this, I get a ClassCastException thrown at the clipboard.setContents(...) call. I can copy a string to the clipboard just fine (the code to do so is in the applet below, currently commented out), but the image always results in the ClassCastException.

One of the posts I've read indicated that if the data format doesn't match the data flavor, this exception will be thrown. I actually created an array of bytes that exactly represented a .BMP image, and set the DataFlavor to "image/bmp" but the exact same problem occurred. I could actually write the array of bytes to a file and get a BMP file that was viewable in image viewers. I'm out of things to try at this point, so any help would be very appreciated.

Some notes of interest:
I am NOT using any Swing components, and I'm hoping to keep it that way.
When I copy the string, or when I do no copying at all, the image displays perfectly, so I know it is read in correctly, and that the security policies are set to allow that.
I write and compile the code on a Sun Solaris machine, then run it via Internet Explorer and a Windows 2000 maching, running Java(TM) Plug-in: Version 1.4.2_05.

Here's the simplified applet source:

import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;
import java.awt.datatransfer.*; //for clipboard ops
 
public class cbTest extends Applet implements ClipboardOwner
{
  public static DataFlavor myFlavor = new DataFlavor("image/gif", "GIF Image");
  BufferedImage bufImg;
  int imgWid;
  int imgHt;
  int pix[];
 
  public void init()
  {
    Image img;
    URL imgURL;
    PixelGrabber pixGrab;
 
    imgURL = null;
    try
    {
      imgURL = new URL("file:/v:\\temp\\java\\radar.jpg");
      imgURL.openConnection().getInputStream(); //test for open...
    }
    catch (MalformedURLException e)
    {
      System.out.println("Malformed URL exception caught");
    }
    catch (Throwable t)
    {
      System.out.println("Couldn't open image");
    }
 
    img = getImage(imgURL);
    while (img.getWidth(this) < 0)
    {
      try
      {
        Thread.sleep(100); //could throw InterruptedException
      }
      catch (InterruptedException t)
      {
        System.out.println("Interrupted Exception Caught");
      }
    }
    imgWid = img.getWidth(this);
    imgHt = img.getHeight(this);
    pix = new int[imgWid * imgHt];
    pixGrab = new PixelGrabber(img, 0, 0, imgWid, imgHt, pix, 0, imgWid);
    try
    {
      pixGrab.grabPixels();  //could throw an InterruptedException
    }
    catch (InterruptedException e)
    {
      System.out.println("Caught interrupted exception");
    }
 
    bufImg = new BufferedImage(imgWid, imgHt, BufferedImage.TYPE_INT_ARGB);
    bufImg.setRGB(0, 0, imgWid, imgHt, pix, 0, imgWid);
 
    //copyStringToClipboard(); //This works fine!
    copyToClipboard();         //This causes a ClassCastException at the
                               //clipboard.setContents(...) call.
  }
 
  public void paint(Graphics g)
  {
    g.drawImage(bufImg, 0, 0, this);
  }
 
  public void copyStringToClipboard()
  {
    Clipboard clipboard;
    StringSelection selec;
 
    clipboard = getToolkit().getSystemClipboard();
    selec = new StringSelection("Copy This String");
    clipboard.setContents(selec, this);
  }
 
  public void copyToClipboard()
  {
    Clipboard clipboard;
    MyImageSelection selec;
 
    clipboard = getToolkit().getSystemClipboard();
    selec = new MyImageSelection();
    clipboard.setContents(selec, this);
  }
 
  public void lostOwnership(Clipboard cb, Transferable trans)
  {
    System.out.println("Lost ownership of system clipboard");
  }
 
  public class MyImageSelection implements Transferable
  {
    public synchronized DataFlavor[] getTransferDataFlavors()
    {
      return new DataFlavor[]{myFlavor};
    }
 
    public boolean isDataFlavorSupported(DataFlavor flavor)
    {
      return myFlavor.equals(flavor);
    }
 
    public synchronized Object getTransferData(DataFlavor flavor)
                                 throws UnsupportedFlavorException, IOException
    {
      if (!myFlavor.equals(flavor))
      {
        throw new UnsupportedFlavorException(flavor);
      }
      return (pix);
    }
  }
}
 
 
alvareze
Posts:22
Registered: 9/15/97
Re: BufferedImage to clipboard GIF to PowerPoint   
Dec 1, 2004 2:48 PM (reply 1 of 8)  (In reply to original post )

 
Hi,

Try replacing the following line:
public static DataFlavor myFlavor = new DataFlavor("image/gif", "GIF Image");

With this one:
public static DataFlavor myFlavor = java.awt.datatransfer.DataFlavor.imageFlavor;

HTH,
Eugenio
 
drewa2
Posts:4
Registered: 12/1/04
Re: BufferedImage to clipboard GIF to PowerPoint   
Dec 2, 2004 5:51 AM (reply 2 of 8)  (In reply to #1 )

 
I have tried this and it results in the exact same problem. If anyone has any other suggestions, I'd be very appreciative...
 
alvareze
Posts:22
Registered: 9/15/97
Re: BufferedImage to clipboard GIF to PowerPoint      
Dec 2, 2004 6:33 AM (reply 3 of 8)  (In reply to original post )

 
Ah, I didn't notice it before. In addition to setting the DataFlavor to DataFlavor.imageFlavor you need to return a java.awt.Image from the getTransferData() method.

Eugenio
 
drewa2
Posts:4
Registered: 12/1/04
Re: BufferedImage to clipboard GIF to PowerPoint   
Dec 2, 2004 7:15 AM (reply 4 of 8)  (In reply to #3 )

 
Thank you! It actually works now.. I was under the impression that the imageFlavor was only used for local clipboards when copying and pasting within Java apps.

I do have one follow-up question though.. In order to get this to work, I had to download the latest Java SDK and compile on the Windows machine, as my Solaris javac complains that:
No variable imageFlavor defined in class java.awt.datatransfer.DataFlavor.

So it looks like my Solaris compiler is out of date. I'll look into getting the newest version for Solaris, but just out of curiosity, is there a way of doing what I want without using Java's imageFlavor? A lot of the examples I saw on the forums and elsewhere returned an array of pixel values like I originally had, but obviously this wasn't working for me.

Thanks again for you help and your follow-up, this problem was very frustrating..
 
alvareze
Posts:22
Registered: 9/15/97
Re: BufferedImage to clipboard GIF to PowerPoint   
Dec 2, 2004 8:46 AM (reply 5 of 8)  (In reply to #4 )

 
Nice to hear its working for you.

The ability to copy an image to the system clipboard was introducted with JDK1.4.

Image transfer didn't work with JDK1.3 and older.
You had to use JNI to build a custom dll or so to enable the transfer.

I am not aware of another way to get around using imageFlavor.

If you want to compile with JDK1.3 you can try the following.
  private static DataFlavor imageFlavor;
 
  static {
      try {
          java.lang.reflect.Field imageFlavorField = DataFlavor.class.getField("imageFlavor");
          imageFlavor = (DataFlavor)imageFlavorField.get(null);
      } catch (Exception e) { }
  } // end static{}

I haven't tried it but this might work too.

new DataFlavor("image/x-java-image; class=java.awt.Image", "Image");


Look in the src.jar for the source code to DataFlavor for details.

Eugenio
 
drewa2
Posts:4
Registered: 12/1/04
Re: BufferedImage to clipboard GIF to PowerPoint   
Dec 2, 2004 10:17 AM (reply 6 of 8)  (In reply to #5 )

 
Eugenio:

Thanks so much for your prompt help! Your second suggestion above worked as well, so I can compile with the old javac and the copy operation still functions. I didn't attempt the first one, since the straight-forward second example worked great.. I'm installing the newer compilers this afternoon, so hopefully the follow-up was a non-issue anyway, but I'm happy to know what I was doing wrong with 1.3. Thanks again, now I can get back to doing some real development instead of banging my head against this same problem over and over.. :-)
 
lsllcm
Posts:3
Registered: 4/24/00
Re: BufferedImage to clipboard GIF to PowerPoint   
Dec 8, 2004 7:25 PM (reply 7 of 8)  (In reply to #6 )

 
I have a need to copy applet image to system clipboard, but the applet runs in IE and NS7.0. Is it feasible?

Thanks in advance.
 
lsllcm
Posts:3
Registered: 4/24/00
Re: BufferedImage to clipboard GIF to PowerPoint   
Dec 8, 2004 7:27 PM (reply 8 of 8)  (In reply to #7 )

 
I have a need to copy applet image to system clipboard, but the applet runs in IE and NS7.0. Is it feasible? Is there a solution.

Thanks in advance.
 
This topic has 8 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 63
  • Guests : 118

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums