从摄像头的Andr​​oid框架为OpenCV的获取垫目标 [英] Get Mat object from Camera frame Android for OpenCV

查看:261
本文介绍了从摄像头的Andr​​oid框架为OpenCV的获取垫目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于OpenCV4Android给予教程2开发Android应用程序。我想实现的就是让相机框架的垫目标,当我触摸手机的屏幕( onTouch())。我想修改code,这样我存储图像作为垫而不是在手机内存中的JPEG文件。然后,垫会在进一步的处理。

I am developing an android application based on Tutorial 2 given by OpenCV4Android. What I want to achieve is to get the Mat object of the camera frame when I touch the phone's screen (onTouch()). I want to modify the code such that I store the image as Mat instead of a jpeg file on the phone's memory. The Mat will then go under further processing.

我是否需要涉及到 onCameraFrame()功能?
任何指导将大大AP preciated。我很新开发Android和OpenCV为好。

Do I need to involve the onCameraFrame() function? Any guidance will be much appreciated. I am very new to Android developing and OpenCV as well.

请注意:我使用的Andr​​oid版本4.2.2和OpenCV2.4.8

Note: I am using Android version 4.2.2 and OpenCV2.4.8.

编辑:

修改后 onTouch() onCameraFrame()功能这些都是code片段:

After editing onTouch() and onCameraFrame() functions these are the code snippets:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

        Mat img = inputFrame.rgba();

        if(touched) {
            int imgsize = (int) (img.total()*img.channels());
            byte[] data = new byte[imgsize];
            img.get(0,0,data);
            int col = img.cols();
            int row = img.rows();
            Toast.makeText(this, "size:"+imgsize+" row:"+row+" col:"+col, Toast.LENGTH_SHORT).show();

            StartSocket(row, col, imgsize, data);

            touched = false;
        }

        return img;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i(TAG,"onTouch event"); 
        touched = true;
        return true;
    }

我所做的基本上是垫目标转换成的ByteArray 名为数据[] ,并将其发送到通过与其它信息一起套接字服务器。插座工作正常,作为一个单独的项目,当我尝试发送文本文件,所以我认为没有什么不妥的地方。

What I did is basically convert the Mat object into a ByteArray called data[] and send it to a server through a socket along with other information. The socket works fine as a separate project when i try to send text files, so I believe there is nothing wrong with it.

和我没有 LogCat中,因为我直接我的设备上运行的应用程序。

And I don't have the LogCat since I am running the app directly on my device.

推荐答案

如果你想处理来自摄像机的图像,是的,你需要的onCameraFrame()方法,因为它提供了你的形象。

if you want to process images from the camera, yes, you'll need the onCameraFrame() method, as it provides your image

public class MyActivity extends Activity implements CvCameraViewListener2,OnTouchListener {
    boolean touched=false;
    public boolean onTouch(View v, MotionEvent event) {
         touched = true;
         return true;
    }
    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Mat rgba = inputFrame.rgba();
        if ( touched ) {
            // do some processing on rgba Mat
            touched = false;
        }
        return rgba;
    }
}

这篇关于从摄像头的Andr​​oid框架为OpenCV的获取垫目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆