直接从Windows剪贴板获取二进制数据 [英] Getting binary data directly off the windows clipboard

查看:372
本文介绍了直接从Windows剪贴板获取二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在脑子里对着桌子打一个小时,现在只是想找到某种说出来的方法……从剪贴板上拿出一个字节数组.相反,我似乎只能找到关于如何从剪贴板中获取纯文本的信息……这根本没有帮助.

I've been beating my head against the desk for about an hour now just trying to find some way of getting say... an array of bytes off the clipboard. Instead, all I can seem to find is information on how to get plain text from the clipboard... that's not helpful at all.

我尝试过以下操作: Java获取剪贴板的字节

我也尝试过以下操作: http://mrbool.com/manipulating-clipboard-content-with-java/24758

I've also tried following this: http://mrbool.com/manipulating-clipboard-content-with-java/24758

每次我遇到不受支持"的愚蠢DataFlavor时.当然,这里只是缺少一些简单的东西……我的意思是……它如何支持纯文本,图像和Java对象,但没有必须具备的基本功能在所有这一切的掩盖下?

Every time I run into the silly DataFlavor being "unsupported". Surely there's just something simple that I'm missing here... I mean... how can it support plain text, images, and java objects, but NOT have the basic functionality that just has to be under the hood of all this?

对不起,如果我听起来很讽刺并且在这里生气……Java似乎对我有影响. :(

Sorry if I sound sarcastic and pissed off here... Java seems to have that effect on me. :(

推荐答案

Awt剪贴板和MIME类型

InsideClipboard 显示内容的MIME类型为application/spark editor

InsideClipboard shows that the content's MIME type is application/spark editor

您应该能够通过使用构造函数

You should be able to create a MIME type DataFlavor by using the constructor DataFlavor(String mimeType, String humanReadableFormat) in which case the class representation will be an InputStream from which you can extract bytes in a classic manner...

但是,此剪贴板实现对mime类型定义非常严格,您不能使用id格式的空格,这太糟糕了,因为您的编辑器似乎在其中放置了一个空格:(

However, this clipboard implementation is very strict on the mime type definition and you cannot use spaces in the format id, which is too bad because your editor seems to put a space there :(

可能的解决方案,如果您有权访问JavaFX

JavaFX的剪贴板管理更加宽松,并且可以容纳剪贴板中的各种格式名称"(InsideClipboard称呼它们),而不仅仅是像awt中那样的无空格type/subtype mime格式.

JavaFX's clipboard management is more lenient and tolerates various "Format Names" (as InsideClipboard calls them) in the clipboard, not just no-space type/subtype mime formats like in awt.

例如,使用LibreOffice Draw 4.2并复制一个Rectangle形状,awt仅看到application/x-java-rawimage格式,而JavaFX看到与InsideClipboard相同的所有格式:

For example, using LibreOffice Draw 4.2 and copying a Rectangle shape, awt only sees a application/x-java-rawimage format whereas JavaFX sees all the same formats as InsideClipboard :

[应用程序/x-java-rawimage],[PNG],[星标对象描述符(XML)],[cf3],[Windows位图],[GDIMetaFile],[cf17],[星标嵌入源(XML) ],[绘图格式]

[application/x-java-rawimage], [PNG], [Star Object Descriptor (XML)], [cf3], [Windows Bitmap], [GDIMetaFile], [cf17], [Star Embed Source (XML)], [Drawing Format]

然后您可以在java.nio.ByteBuffer

//with awt
DataFlavor[] availableDataFlavors = Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors();
System.out.println("Awt detected flavors : "+availableDataFlavors.length);
for (DataFlavor f : availableDataFlavors) {
    System.out.println(f);
}

//with JavaFX (called from JavaFX thread, eg start method in a javaFX Application
Set<DataFormat> contentTypes = Clipboard.getSystemClipboard().getContentTypes();
System.out.println("JavaFX detected flavors : " + contentTypes.size());
for (DataFormat s : contentTypes) {
        System.out.println(s);
}

//let's attempt to extract bytes from the clipboard containing data from the game editor
// (note : some types will be automatically mapped to Java classes, and unknown types to a ByteBuffer)
// another reproducable example is type "Drawing Format" with a Rectangle shape copied from LibreOffice Draw 4.2
DataFormat df = DataFormat.lookupMimeType("application/spark editor");
if (df != null) {
    Object content = Clipboard.getSystemClipboard().getContent(df);
    if (content instanceof ByteBuffer) {
        ByteBuffer buffer = (ByteBuffer) content;
        System.err.println(new String(buffer.array(), "UTF-8"));
    } else
        System.err.println(content);
}

这篇关于直接从Windows剪贴板获取二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆