问题描述
我正在尝试开发一个监视android屏幕的应用程序,一旦它使用OpenCV模板匹配检测到按钮,就会单击sed按钮。 我设法利用Android的辅助功能手势在任何应用程序上进行触摸,并使用OpenCV的模板匹配功能在已保存的图像上查找按钮。
但是我将如何从任何应用程序连续获取屏幕的“快照”,以便OpenCV可以处理图像。
我研究了Android的MediaProjection,但是,我不知道如何获取每一帧并将其转换为“垫”对象。我会使用MediaProjection来获取可由OpenCV处理的屏幕图像数据,如果没有的话,还有其他方法吗?
以下是与OpenCV匹配的模板的代码:
class MatchingDemo {
public Point run(Context con,int match_method) throws IOException {
System.out.println("\nRunning Template Matching");
Mat templ = Utils.loadResource(con,R.drawable.template,CvType.CV_8UC4);
Mat img = Utils.loadResource(con,R.drawable.background,CvType.CV_8UC4);
// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows,result_cols,CvType.CV_32FC1);
// / Do the Matching and normalize
Imgproc.matchTemplate(img,templ,result,match_method);
// Core.normalize(result,1,Core.norM_MINMAX,-1,new Mat());
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);
Double matchCertainty = mmr.maxVal;
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF
|| match_method == Imgproc.TM_SQDIFF_norMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
if (matchCertainty > 0.98) {
Point clickLocation = new Point(matchLoc.x + (templ.cols()*0.5),matchLoc.y + (templ.rows()*0.5));
System.out.println(clickLocation.x);
System.out.println(clickLocation.y);
return clickLocation;
} else{
System.out.println("Image not Found!!!");
return null;
}
}
}
public class TemplateMatching {
public static Point main(Context con) throws IOException {
Point clickLocation = new MatchingDemo().run(con,Imgproc.TM_CCOEFF_norMED);
return clickLocation;
}
}
我也有用于截屏的代码,但这不是我想要的,因为它仅在我的应用程序中有效:
public class ScreenCapture {
public void takeScreenshot(Window win) {
Date Now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss",Now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + Now + ".jpg";
System.out.println(mPath);
// create bitmap screen capture
View v1 = win.getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG,quality,outputStream);
outputStream.flush();
outputStream.close();
// openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printstacktrace();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)