如何通过OpenCV模板匹配使用Android MediaProjection

如何解决如何通过OpenCV模板匹配使用Android MediaProjection

我正在尝试开发一个监视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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?