在 Java 中使用 ImageSearchDll.dll(来自 AutoIt)编辑:JNA

Posted

技术标签:

【中文标题】在 Java 中使用 ImageSearchDll.dll(来自 AutoIt)编辑:JNA【英文标题】:Use ImageSearchDll.dll in Java ( from AutoIt ) edit: JNA 【发布时间】:2012-11-01 11:41:33 【问题描述】:

我想对我的 Java 程序中的屏幕事件做出反应,所以我想在我的实际屏幕中找到一个图像。我尝试编写一种方法来从机器人类中获取屏幕截图,然后搜索像素 - 但它需要很长时间。

我知道在 AutoIt 中有一个外部 DLL 可以很好地完成这项工作,现在我试图让它在 java 中运行......但我被卡住了:/

.dll 在 AutoIt Includes 中调用如下:

Func _ImageSearch($findImage,$resultPosition,ByRef $x, ByRef $y,$tolerance)
   return _ImageSearchArea($findImage,$resultPosition,0,0,@DesktopWidth,@DesktopHeight,$x,$y,$tolerance)
EndFunc

和:

Func _ImageSearchArea($findImage,$resultPosition,$x1,$y1,$right,$bottom,ByRef $x, ByRef $y, $tolerance)

if $tolerance>0 then $findImage = "*" & $tolerance & " " & $findImage
$result = DllCall("ImageSearchDLL.dll","str","ImageSearch","int",$x1,"int",$y1,"int",$right,"int",$bottom,"str",$findImage)

if $result[0]="0" then return 0

$array = StringSplit($result[0],"|")

$x=Int(Number($array[2]))
$y=Int(Number($array[3]))
if $resultPosition=1 then
  $x=$x + Int(Number($array[4])/2)
  $y=$y + Int(Number($array[5])/2)
endif
return 1
EndFunc

我得到了 dll 并尝试了 jna 之类的东西,但我无法让它工作。我还尝试使用 AutoItX 让 AutoIt 函数在 Java 中运行,但它不适用于包含。你能帮帮我吗?

编辑:好的,我在 JNA 上做了另一次尝试,现在我得到了一个字符串 - 但字符串意味着错误。有什么问题? 我有一个接口:

public interface ImageSearchDLL extends Library
ImageSearchDLL INSTANCE = (ImageSearchDLL) Native.loadLibrary("ImageSearchDLL", ImageSearchDLL.class);
String ImageSearch(int x1, int y1, int x2, int y2, String findImage);   

我这样称呼它:

static 
    File file = new File("libs", "ImageSearchDLL.dll");
    System.load(file.getAbsolutePath());
    
(...)
String a = ImageSearchDLL.INSTANCE.ImageSearch(0, 0, 500, 500, "C:\myProg\OK.bmp");

我总是返回“0”,这意味着错误或未找到,就像我在 AutoIT 文件中看到的那样:

; If error exit
if $result[0]="0" then return 0

你能帮我解决这个问题吗?

【问题讨论】:

你是否有你试图调用的函数的实际原型(最好是 C)?原始 _ImageSearchArea 函数中的“ByRef”装饰器似乎暗示 ImageSearch 期望这些参数是 int *,而不是 int。 JNA中对应的类型是IntByReference 【参考方案1】:

您采用的另一种方法是使用Runtime.getRuntime().exec() 生成执行图像搜索的应用程序(c、autoit 脚本等)并利用基于文件/轮询方法(或通过标准输出)在它们之间进行通信将结果发送到您的 java 应用程序。

【讨论】:

以上是关于在 Java 中使用 ImageSearchDll.dll(来自 AutoIt)编辑:JNA的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Java 中使用 cURL?

如何在 Java 中使用引用?

在 Java 6 和 Java 7 中使用透明窗口

怎样在java程序中使用java启动flume

在java中使用数组有啥好处啊?

在Java中使用预定义的类名作为类或变量名称