[FIXED] Using opencv4android,how to cut out video frame automatically when the camera is opened

Issue

There is a sample which can cut out the video frame in the official SDK.
The main code as follows:

@SuppressLint("SimpleDateFormat")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i(TAG,"onTouch event");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String currentDateandTime = sdf.format(new Date());
        String fileName = Environment.getExternalStorageDirectory().getPath() +
                               "/sample_picture_" + currentDateandTime + ".jpg";
        mOpenCvCameraView.takePicture(fileName);
        Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
        return false;
    }

Now I want to change it to be a sample which can cute out video frame automatically. I had Commented the above code and changed the method named
onManagerConnected.The change as follows:

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                    String filename = Environment.getExternalStorageDirectory().getPath()+"test"+".jpg";
                    mOpenCvCameraView.takePicture(filename);
                    Log.i(TAG, filename);
//                  mOpenCvCameraView.setOnTouchListener(Tutorial3Activity.this);
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

And then there throws a nullpointException showing the var “mPictureFileName” and “mCamera” are null.

 public void takePicture(final String fileName) {
            Log.i(TAG, "Taking picture");
            mPictureFileName = fileName;
            // Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
            // Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
            mCamera.setPreviewCallback(null);

            // PictureCallback is implemented by the current class
            mCamera.takePicture(null, null, this);
        }

Anyway!

Thanks for answers!

Solution

When the view opens, the camera isn’t initating.So you can’t cut out video in the method “initating”.
I move those code to the the method “onCameraFrame”.Done!

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    String filename = null;
    filename = Environment.getExternalStorageDirectory().getPath()+"/test"+i+".jpg";
    mOpenCvCameraView.takePicture(filename);
    Log.d("camera", filename);
    return inputFrame.rgba();
}

Answered By – fancyT

Answer Checked By – David Marino (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published