I want to react on a screen event in my Java Programm so i want to find a Image in my actual screen. I tried to write a method to get a Screenshot from the robot class and then search the pixels - but it took way to long.
I know that in AutoIt there is a external DLL that does this job pretty good and now i tried to get it running in java... But i am stuck :/
The .dll is called in the AutoIt Includes like this:
Func _ImageSearch($findImage,$resultPosition,ByRef $x, ByRef $y,$tolerance)
return _ImageSearchArea($findImage,$resultPosition,0,0,@DesktopWidth,@DesktopHeight,$x,$y,$tolerance)
EndFunc
and:
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
I got the dll and tried things like jna but i can't get it working. I also tried AutoItX to get AutoIt Functions running in Java but it doesn't work with includes. Can you help me?
edit: Ok i did another try on JNA and now i get a String back - but the String means error. Whats the problem? I have a Interface:
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);
}
and i call it like this:
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");
I always get "0" back where means Error or not found like i can see in the AutoIT file:
; If error exit
if $result[0]="0" then return 0
Can you help me fix that?
An alternative to the path your taking is to use Runtime.getRuntime().exec()
to spawn an application (c, autoit script, etc) that does the imagesearch and utilize a file based/polling methodology (or through Standard output) for communication between them to get your results to your java application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With