java 从剪贴板中获取图片_java访问windows剪贴板中的图片

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:

importjava.awt.*;

importjava.awt.image.*;

importjava.applet.*;

importjava.io.*;

importjava.lang.*;

importjava.util.*;

importjava.net.*;

importjava.awt.datatransfer.*;//for clipboard ops

publicclasscbTestextendsAppletimplementsClipboardOwner

{

publicstaticDataFlavor myFlavor =newDataFlavor("image/gif","GIF Image");

BufferedImage bufImg;

intimgWid;

intimgHt;

intpix[];

publicvoidinit()

{

Image img;

URL imgURL;

PixelGrabber pixGrab;

imgURL =null;

try

{

imgURL =newURL("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 =newint[imgWid * imgHt];

pixGrab =newPixelGrabber(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 =newBufferedImage(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.

}

publicvoidpaint(Graphics g)

{

g.drawImage(bufImg,0,0,this);

}

publicvoidcopyStringToClipboard()

{

Clipboard clipboard;

StringSelection selec;

clipboard = getToolkit().getSystemClipboard();

selec =newStringSelection("Copy This String");

clipboard.setContents(selec,this);

}

publicvoidcopyToClipboard()

{

Clipboard clipboard;

MyImageSelection selec;

clipboard = getToolkit().getSystemClipboard();

selec =newMyImageSelection();

clipboard.setContents(selec,this);

}

publicvoidlostOwnership(Clipboard cb, Transferable trans)

{

System.out.println("Lost ownership of system clipboard");

}

publicclassMyImageSelectionimplementsTransferable

{

publicsynchronizedDataFlavor[] getTransferDataFlavors()

{

returnnewDataFlavor[]{myFlavor};

}

publicbooleanisDataFlavorSupported(DataFlavor flavor)

{

returnmyFlavor.equals(flavor);

}

publicsynchronizedObject getTransferData(DataFlavor flavor)

throwsUnsupportedFlavorException, IOException

{

if(!myFlavor.equals(flavor))

{

thrownewUnsupportedFlavorException(flavor);

}

return(pix);

}

}

}

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、付费专栏及课程。

余额充值