使用Clipboard,Toolkit和Transferable传输图像时出错

时间:2010-06-13 11:31:35

标签: java

我从以下代码中收到此错误消息:

Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
  at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)
  at sun.awt.datatransfer.DataTransferer.imageToStandardBytes(DataTransferer.java:1994)
  at sun.awt.windows.WDataTransferer.imageToPlatformBytes(WDataTransferer.java:267)
  at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1123)
  at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:163)
  at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:73)
  at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:93)
  at automateSignature.LoadToClipboard.main(LoadToClipboard.java:8)

需要对代码进行哪些更改或更改图像文件才能解决此问题 错误? 请注意,此code由Oscar Reyes提供,但任何错误都是我的。

import java.awt.*;
import java.awt.datatransfer.*;

public class LoadToClipboard {

    public static void main(String[] args) {
        Toolkit tolkit = Toolkit.getDefaultToolkit();
        Clipboard clip = tolkit.getSystemClipboard();
        clip.setContents(new ImageSelection(
            tolkit.getImage("MKSignature.jpg")), null);
    }
}

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 {
        if (!DataFlavor.imageFlavor.equals(flavor)) {
            throw new UnsupportedFlavorException(flavor);
        }
        return image;
    }
}

我试图在代码中找到宽度和高度可以的位置 指定但未成功。我还检查了它的属性 指定了jpg文件和w和h。enter code here

NEW ATTEMPT

我现在已经将代码更改为以下内容:注意使用 createImage()prepareImage()现在是宽度和高度错误 固定的。

public static void main(String[] args) {
    Toolkit tolkit = Toolkit.getDefaultToolkit();
    Clipboard clip = tolkit.getSystemClipboard();
    //File file = new File(
    //  "C:\\aaaa\\Admin\\SIGNATUREForInsertionIntoDocs\\MKSignature.jpg");
    signatureImage = tolkit.createImage(
        "C:\\aaaa\\Admin\\SIGNATUREForInsertionIntoDocs\\MKSignature.jpg");
    tolkit.prepareImage(signatureImage, 108, 60, null);
    // JOptionPane.showConfirmDialog(null, "Press to continue");
    ImageSelection i = new ImageSelection(signatureImage);
    clip.setContents(i, null);
}

然而现在放置的图像 剪贴板中的图像与jpg文件中的图像不同。 它的大小相同,但内容不同。

GIMP中显示的图像(来自.jpg文件)是钢琴。

运行上述内容后从剪贴板粘贴的图像 程序设置剪贴板内容,大小相同,但是空白!

不知何故,图片被剥夺了内容。 我做错了什么?

2 个答案:

答案 0 :(得分:0)

仔细查看工具包方法用于读取图像。您最有可能想要一个同步方法阅读图标。

答案 1 :(得分:0)

我使用ExampleDepot.com中的代码重写了程序:

package source;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageFromFileToClipboard {

    public static void main(String[] args) {
        Image image = null;
        Image imageFromClipboard = null;
        try { 
            File file = new File("C:\\Documents and Settings\\Mike King\\My Documents\\My Pictures\\PianoSmallThumbnail.jpg"); 
            image = ImageIO.read(file);
        }
        catch (IOException e) { 

        } 

        // Use a label to display the image 
        JFrame frame = new JFrame(); 
        JLabel label = new JLabel(new ImageIcon(image)); 
        frame.getContentPane().add(label, BorderLayout.CENTER); 
        frame.pack(); 
        frame.setVisible(true); 

        System.out.println(image.toString());

        setClipboard(image);

        imageFromClipboard = getClipboard();
        // Use a label to display the image 
        JFrame frame2 = new JFrame(); 
        JLabel label2 = new JLabel(new ImageIcon(imageFromClipboard)); 

        frame2.getContentPane().add(label2, BorderLayout.CENTER); 
        frame2.pack(); 
        frame2.setVisible(true); 
    }

    // If an image is on the system clipboard, this method returns it; 
    // otherwise it returns null. 
    public static Image getClipboard() { 
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 
        try {
            if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) { 
                Image text = (Image) t.getTransferData(DataFlavor.imageFlavor); 
                return text;
            }
        } 
        catch (UnsupportedFlavorException e) { } 
        catch (IOException e) { } 

        return null; 
    } 

    // This method writes a image to the system clipboard
    public static void setClipboard(Image image) { 
        ImageSelection imgSel = new ImageSelection(image); 
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
    } 

    // This class is used to hold an image while on the clipboard.      
    public static class ImageSelection implements Transferable { 
        private Image image; 

        public ImageSelection(Image image) { 
            this.image = image;
        }  

        public DataFlavor[] getTransferDataFlavors() { 
            return new DataFlavor[]{DataFlavor.imageFlavor};
        } 

        public boolean isDataFlavorSupported(DataFlavor flavor) { 
            return DataFlavor.imageFlavor.equals(flavor);
        } 

        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 
            if (!DataFlavor.imageFlavor.equals(flavor)) { 
                throw new UnsupportedFlavorException(flavor);
            }

            return image;
        } 
    }
}

现在已成功读取jpg文件中的图像并将其放入剪贴板。 手动将剪贴板粘贴到任何文档中,功能正常。 JFrame的使用只是检查从文件中读取的内容,以及 然后把什么放在剪贴板中。