androidsocketsopencvmat

Get Mat object from Camera frame Android for OpenCV


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.

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.

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

Edit:

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;
    }

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.

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


Solution

  • 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;
        }
    }