ACT-CV - Machine Vision for Cognitive Modeling
Robot.cpp
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
28 #include <general/OpenCVHelpers.h>
29 
30 #include "Robot.h"
31 #include "config.h"
32 
34  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
35 
36  cls = CreateGlobalRef(GetEnv()->FindClass("java/awt/Robot"));
37  if (cls == nullptr) throw FileLineException(__FILE__,__LINE__);
38  MHDBGMSG_P(__FILE__,__LINE__,"cls:",cls);
39 
40  jmethodID mid = GetEnv()->GetMethodID(cls, "<init>", "()V");
41  MHDBGMSG_P(__FILE__,__LINE__,"mid:",mid);
42 
43  jobject localRobot=GetEnv()->NewObject(cls, mid);
44  MHDBGMSG_P(__FILE__,__LINE__,"localRobot:",localRobot);
45  if (localRobot == nullptr) throw FileLineException(__FILE__,__LINE__);
46 
47  robot = CreateGlobalRef(localRobot);
48  GetEnv()->DeleteLocalRef(localRobot);
49  MHDBGMSG_P(__FILE__,__LINE__,"robot:",robot);
50  if (robot == nullptr) throw FileLineException(__FILE__,__LINE__);
51 
52  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
53 
54  // cache method ids
55  setAutoDelayMID = GetEnv()->GetMethodID(cls, "setAutoDelay", "(I)V");
56  if (setAutoDelayMID==nullptr) throw FileLineException(__FILE__,__LINE__);
57  delayMID = GetEnv()->GetMethodID(cls, "delay", "(I)V");
58  if (setAutoDelayMID==nullptr) throw FileLineException(__FILE__,__LINE__);
59 
60  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
61 
62  mouseMoveMID = GetEnv()->GetMethodID(cls, "mouseMove", "(II)V");
63  if (setAutoDelayMID==nullptr) throw FileLineException(__FILE__,__LINE__);
64  mousePressMID = GetEnv()->GetMethodID(cls, "mousePress", "(I)V");
65  if (mousePressMID==nullptr) throw FileLineException(__FILE__,__LINE__);
66  mouseReleaseMID = GetEnv()->GetMethodID(cls, "mouseRelease", "(I)V");
67  if (mouseReleaseMID==nullptr) throw FileLineException(__FILE__,__LINE__);
68 
69  keyPressMID = GetEnv()->GetMethodID(cls, "keyPress", "(I)V");
70  if (keyPressMID==nullptr) throw FileLineException(__FILE__,__LINE__);
71  keyReleaseMID = GetEnv()->GetMethodID(cls, "keyRelease", "(I)V");
72  if (keyReleaseMID==nullptr) throw FileLineException(__FILE__,__LINE__);
73 
74  getPixelColorMID = GetEnv()->GetMethodID(cls, "getPixelColor", "(II)Ljava/awt/Color;");
75  if (getPixelColorMID==nullptr) throw FileLineException(__FILE__,__LINE__);
76 
77  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
78  // for the Color class
79  colorCls = CreateGlobalRef(GetEnv()->FindClass("java/awt/Color"));
80  if (colorCls == nullptr) throw FileLineException(__FILE__,__LINE__);
81  getRGBMID = GetEnv()->GetMethodID(colorCls, "getRGB", "()I");
82  if (getRGBMID == nullptr) throw FileLineException(__FILE__,__LINE__);
83 
84  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
85  // screen capture
86  createScreenCaptureMID = GetEnv()->GetMethodID(cls, "createScreenCapture", "(Ljava/awt/Rectangle;)Ljava/awt/image/BufferedImage;");
87  if (createScreenCaptureMID == nullptr) throw FileLineException(__FILE__,__LINE__);
88  rectCls = CreateGlobalRef(GetEnv()->FindClass("java/awt/Rectangle"));
89  if (rectCls == nullptr) throw FileLineException(__FILE__,__LINE__);
90  initRectMID = GetEnv()->GetMethodID(rectCls, "<init>", "(IIII)V");
91  if (initRectMID == nullptr) throw FileLineException(__FILE__,__LINE__);
92 
93  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
94 
95  bufrImgCls = CreateGlobalRef(GetEnv()->FindClass("java/awt/image/BufferedImage"));
96  if (bufrImgCls == nullptr) throw FileLineException(__FILE__,__LINE__);
97  getRGBArrMID = GetEnv()->GetMethodID(bufrImgCls, "getRGB", "(IIII[III)[I");
98  if (getRGBArrMID == nullptr) throw FileLineException(__FILE__,__LINE__);
99 
100  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
101  // get Screen Dimensions
102  jclass toolkitClass = CreateGlobalRef(GetEnv()->FindClass("java/awt/Toolkit"));
103  if (toolkitClass == nullptr) throw FileLineException(__FILE__,__LINE__);
104 
105  jmethodID factoryMID = GetEnv()->GetStaticMethodID(
106  toolkitClass, "getDefaultToolkit", "()Ljava/awt/Toolkit;");
107  if (factoryMID == nullptr) throw FileLineException(__FILE__,__LINE__);
108 
109  jobject toolkit = CreateGlobalRef(GetEnv()->CallStaticObjectMethod(toolkitClass, factoryMID));
110  if (toolkit == nullptr) throw FileLineException(__FILE__,__LINE__);
111 
112  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
113 
114  jmethodID getScreenSizeMID = GetEnv()->GetMethodID(
115  toolkitClass, "getScreenSize", "()Ljava/awt/Dimension;");
116  if (getScreenSizeMID == nullptr) throw FileLineException(__FILE__,__LINE__);
117 
118  jobject dim = GetEnv()->CallObjectMethod(toolkit, getScreenSizeMID);
119  if (dim == nullptr) throw FileLineException(__FILE__,__LINE__);
120 
121  jclass dimClass = GetEnv()->GetObjectClass(dim);
122  if (dimClass == nullptr) throw FileLineException(__FILE__,__LINE__);
123 
124  jfieldID hFID = GetEnv()->GetFieldID(dimClass, "height", "I");
125  if (hFID == nullptr) throw FileLineException(__FILE__,__LINE__);
126  screenHeight = GetEnv()->GetIntField(dim, hFID);
127 
128  jfieldID wFID = GetEnv()->GetFieldID(dimClass, "width", "I");
129  if (wFID == nullptr) throw FileLineException(__FILE__,__LINE__);
130  screenWidth = GetEnv()->GetIntField(dim, wFID);
131 
132  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
133 }
134 
136  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
137  GetEnv()->DeleteGlobalRef(robot);
138  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
139 }
140 
142  return CreateRect(0,0,screenWidth,screenHeight);
143 
144  //cout << height << " " << width << endl;
145 
146 #if 0
147  jclass rectCls = GetEnv()->FindClass("java/awt/Rectangle");
148  if (rectCls == nullptr) throw FileLineException(__FILE__,__LINE__);
149 
150  jmethodID mid = GetEnv()->GetMethodID(rectCls, "<init>", "(Ljava/lang/Dimension;)V");
151  jobject result = GetEnv()->NewObject(rectCls, mid, dim);
152  if (result == nullptr) throw FileLineException(__FILE__,__LINE__);
153 
154  return result;
155 #endif
156 }
157 
158 
159 int Robot::GetPixelRGB(int x, int y) {
160  jobject color = GetEnv()->CallObjectMethod(robot, getPixelColorMID, x, y);
161  return GetEnv()->CallIntMethod(color, getRGBMID);
162 }
163 
164 
166 #define MULT 1
167 
168 IplImage* Robot::GetScreenCapture(int left, int top, int right, int bottom) {
169  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
170 
171  int width=right-left+1;
172  int height=bottom-top+1;
173 
174  IplImage *result = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
175  cvZero(result);
176  MHDBGMSG_P(__FILE__,__LINE__,"result:",result);
177 
178  jobject rect = CreateRect(left,top,width,height);
179  if (rect == nullptr) throw FileLineException(__FILE__,__LINE__);
180  MHDBGMSG_P(__FILE__,__LINE__,"rect:",rect);
181  jobject img = GetEnv()->CallObjectMethod(robot, createScreenCaptureMID, rect);
182  if (img == nullptr) throw FileLineException(__FILE__,__LINE__);
183  MHDBGMSG_P(__FILE__,__LINE__,"img:",img);
184 
185  // jetzt: int-array machen und Daten abholen!
186  jintArray arr=(jintArray)GetEnv()->CallObjectMethod(img, getRGBArrMID, 0, 0, width, height,
187  nullptr, 0, width*MULT);
188  if (arr == nullptr) throw FileLineException(__FILE__,__LINE__);
189  MHDBGMSG_P(__FILE__,__LINE__,"arr:",arr);
190 
191  jint *pixels = GetEnv()->GetIntArrayElements(arr, nullptr);
192  if (pixels == nullptr) throw FileLineException(__FILE__,__LINE__);
193 
194  MHDBGMSG_P(__FILE__,__LINE__,"pixels:",pixels);
195 
196 
197  for (int x=left;x<=right;x++) {
198  for (int y=top;y<=bottom;y++) {
199  //int rgb=GetPixelRGB(x,y);
200  int rgb=pixels[(y-top)*width*MULT+x-left];
201  cvSet2D(result,y-top,x-left,Rgb2Scalar(rgb));
202  }
203  }
204 
205  // JNI_ABORT -> don't need to copy it back!
206  GetEnv()->ReleaseIntArrayElements(arr, pixels, JNI_ABORT);
207  GetEnv()->DeleteLocalRef(arr);
208  GetEnv()->DeleteLocalRef(img);
209  GetEnv()->DeleteLocalRef(rect);
210 
211  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
212  return result;
213 }
214 
215 // createScreenCapture
216 


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)