java访问windows剪贴板中的图片

 http://forums.sun.com/thread.jspa?threadID=576069

 

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:

  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.applet.*;
  4. import java.io.*;
  5. import java.lang.*;
  6. import java.util.*;
  7. import java.net.*;
  8. import java.awt.datatransfer.*; //for clipboard ops
  9. public class cbTest extends Applet implements ClipboardOwner
  10. {
  11.   public static DataFlavor myFlavor = new DataFlavor("image/gif""GIF Image");
  12.   BufferedImage bufImg;
  13.   int imgWid;
  14.   int imgHt;
  15.   int pix[];
  16.   public void init()
  17.   {
  18.     Image img;
  19.     URL imgURL;
  20.     PixelGrabber pixGrab;
  21.     imgURL = null;
  22.     try
  23.     {
  24.       imgURL = new URL("file:/v://temp//java//radar.jpg");
  25.       imgURL.openConnection().getInputStream(); //test for open...
  26.     }
  27.     catch (MalformedURLException e)
  28.     {
  29.       System.out.println("Malformed URL exception caught");
  30.     }
  31.     catch (Throwable t)
  32.     {
  33.       System.out.println("Couldn't open image");
  34.     }
  35.     img = getImage(imgURL);
  36.     while (img.getWidth(this) < 0)
  37.     {
  38.       try
  39.       {
  40.         Thread.sleep(100); //could throw InterruptedException
  41.       }
  42.       catch (InterruptedException t)
  43.       {
  44.         System.out.println("Interrupted Exception Caught");
  45.       }
  46.     }
  47.     imgWid = img.getWidth(this);
  48.     imgHt = img.getHeight(this);
  49.     pix = new int[imgWid * imgHt];
  50.     pixGrab = new PixelGrabber(img, 00, imgWid, imgHt, pix, 0, imgWid);
  51.     try
  52.     {
  53.       pixGrab.grabPixels();  //could throw an InterruptedException
  54.     }
  55.     catch (InterruptedException e)
  56.     {
  57.       System.out.println("Caught interrupted exception");
  58.     }
  59.     bufImg = new BufferedImage(imgWid, imgHt, BufferedImage.TYPE_INT_ARGB);
  60.     bufImg.setRGB(00, imgWid, imgHt, pix, 0, imgWid);
  61.     //copyStringToClipboard(); //This works fine!
  62.     copyToClipboard();         //This causes a ClassCastException at the
  63.                                //clipboard.setContents(...) call.
  64.   }
  65.   public void paint(Graphics g)
  66.   {
  67.     g.drawImage(bufImg, 00this);
  68.   }
  69.   public void copyStringToClipboard()
  70.   {
  71.     Clipboard clipboard;
  72.     StringSelection selec;
  73.     clipboard = getToolkit().getSystemClipboard();
  74.     selec = new StringSelection("Copy This String");
  75.     clipboard.setContents(selec, this);
  76.   }
  77.   public void copyToClipboard()
  78.   {
  79.     Clipboard clipboard;
  80.     MyImageSelection selec;
  81.     clipboard = getToolkit().getSystemClipboard();
  82.     selec = new MyImageSelection();
  83.     clipboard.setContents(selec, this);
  84.   }
  85.   public void lostOwnership(Clipboard cb, Transferable trans)
  86.   {
  87.     System.out.println("Lost ownership of system clipboard");
  88.   }
  89.   public class MyImageSelection implements Transferable
  90.   {
  91.     public synchronized DataFlavor[] getTransferDataFlavors()
  92.     {
  93.       return new DataFlavor[]{myFlavor};
  94.     }
  95.     public boolean isDataFlavorSupported(DataFlavor flavor)
  96.     {
  97.       return myFlavor.equals(flavor);
  98.     }
  99.     public synchronized Object getTransferData(DataFlavor flavor)
  100.                                  throws UnsupportedFlavorException, IOException
  101.     {
  102.       if (!myFlavor.equals(flavor))
  103.       {
  104.         throw new UnsupportedFlavorException(flavor);
  105.       }
  106.       return (pix);
  107.     }
  108.   }
  109. }
 
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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值