Permalink
Browse files

started: find on screen

  • Loading branch information...
1 parent f086e95 commit 3c3b55ee8960c84a995f3677580e3f51a883fb9f @RaiMan committed Jan 11, 2017
@@ -4,15 +4,11 @@
package com.sikulix.api;
-import com.sikulix.core.Content;
-import com.sikulix.core.SX;
-import com.sikulix.core.SXLog;
-import com.sikulix.core.SXElement;
+import com.sikulix.core.*;
//import com.sikulix.scripting.JythonHelper;
//import com.sikulix.scripting.SXRunner;
import com.sikulix.util.FileChooser;
import org.sikuli.script.Key;
-import com.sikulix.core.HotkeyListener;
import javax.swing.*;
import java.awt.*;
@@ -27,7 +23,6 @@
private static SXLog log = SX.getLogger("SX.Do");
private static String klazz = Do.class.getName();
- ;
//<editor-fold desc="SX Do popat">
private static Point locPopAt = null;
@@ -870,9 +865,21 @@ public static String getLastCommandResult() {
}
//</editor-fold>
+ //<editor-fold desc="Screen related">
private static Element defaultScreenRegion = new Element(SX.getMonitor().getBounds());
private static Element defaultRegion = defaultScreenRegion;
+ private static LocalRobot getLocalRobot() {
+ if (SX.isNull(localRobot)) try {
+ localRobot = new LocalRobot();
+ } catch (AWTException e) {
+ log.error("getLocalRobot: %s", e.getMessage());
+ }
+ return localRobot;
+ }
+
+ private static LocalRobot localRobot = null;
+
public static Element use() {
defaultRegion = defaultScreenRegion;
return defaultRegion;
@@ -888,6 +895,21 @@ public static Element use(int monitor) {
return defaultRegion;
}
+ public static Image capture() {
+ return capture(defaultRegion);
+ }
+
+ public static Image capture(Element region) {
+ if (SX.isNull(region)) region = defaultRegion;
+ Image imgCapture = new Image();
+ if (!region.isSpecial()) {
+ imgCapture = getLocalRobot().captureScreen(region.getRectangle());
+ }
+ return imgCapture;
+ }
+ //</editor-fold>
+
+ //<editor-fold desc="actions like click">
public static Element click(Object... args) {
Target target = new Target(args);
if (target.isValid()) {
@@ -977,4 +999,5 @@ Element getTarget() {
return null;
}
}
+ //</editor-fold>
}
@@ -61,6 +61,7 @@ public Image(BufferedImage bimg) {
setContent(makeMat(bimg));
timeToLoad = new Date().getTime() - start;
init(0, 0, getContent().width(), getContent().height());
+ setAttributes();
}
public Image(Mat mat) {
@@ -73,6 +74,7 @@ public Image(Mat mat) {
timeToLoad = new Date().getTime() - start;
}
init(0, 0, getContent().width(), getContent().height());
+ setAttributes();
}
public Image(String fpImg) {
@@ -200,6 +202,9 @@ public boolean isMeanColorEqual(Color otherMeanColor) {
}
private void setAttributes() {
+ if (!hasContent()) {
+ return;
+ }
plainColor = false;
blackColor = false;
resizeFactor = Math.min(((double) getContent().width()) / resizeMinDownSample,
@@ -118,7 +118,7 @@ public void delay(int ms) {
@Override
public Image captureScreen(Rectangle rect) {
BufferedImage bImg = createScreenCapture(rect);
- log.trace("RobotDesktop: captureScreen: [%d,%d, %dx%d]",
+ log.trace("captureScreen: [%d,%d, %dx%d]",
rect.x, rect.y, rect.width, rect.height);
return new Image(bImg);
}
@@ -300,7 +300,6 @@ public void test_41_findImageInSameImage() {
result = end() + result;
assert success;
}
- //</editor-fold>
@Test
public void test_42_findImageInOtherImage() {
@@ -363,11 +362,71 @@ public void test_43_findAllInImage() {
}
assert success;
}
+ //</editor-fold>
+
+ @Test
+ public void test_50_captureDefaultScreen() {
+ currentTest = "test_50_captureDefaultScreen";
+ if (!SX.isHeadless()) {
+ start();
+ Image img = Do.capture();
+ result = end() + img.toString();
+ assert img.hasContent();
+ } else {
+ result = "headless: not tested";
+ }
+ assert true;
+ }
+
+ @Test
+ public void test_51_capturePartOfDefaultScreen() {
+ currentTest = "test_51_capturePartOfDefaultScreen";
+ if (!SX.isHeadless()) {
+ start();
+ Image img = Do.capture(new Element(50, 50, 200));
+ result = end() + img.toString();
+ if (img.hasContent()) {
+ if (log.isLevel(SXLog.DEBUG)) {
+ img.show();
+ }
+ assert true;
+ return;
+ }
+ assert false;
+ } else {
+ result = "headless: not tested";
+ assert true;
+ }
+ }
+
+ @Test
+ public void test_52_findInDefaultScreen() {
+ currentTest = "test_52_findInDefaultScreen";
+ if (!SX.isHeadless()) {
+ start();
+ Image base = Do.capture();
+ Image img = Do.capture(new Element(50, 50, 200));
+ Element match = new Element();
+ if (base.hasContent() && img.hasContent()) {
+ Finder finder = new Finder(base);
+ match = finder.find(img);
+ assert match.isValid();
+ result = end() + match.toString();
+ base.showMatch();
+
+ return;
+ }
+ assert false;
+ } else {
+ result = "headless: not tested";
+ }
+ assert true;
+ }
//<editor-fold desc="ignored">
@Ignore
- public void test_50_nativeHook() {
- currentTest = "test_50_nativeHook";
+ public void test_60_nativeHook() {
+ currentTest = "test_60_nativeHook";
if (!SX.isHeadless()) {
NativeHook hook = NativeHook.start();
SX.pause(3);

0 comments on commit 3c3b55e

Please sign in to comment.