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


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

    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";
        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) {
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG, "OpenCV loaded successfully");
                    String filename = Environment.getExternalStorageDirectory().getPath()+"test"+".jpg";
                    Log.i(TAG, filename);
//                  mOpenCvCameraView.setOnTouchListener(Tutorial3Activity.this);
                } break;
                } 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

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


Thanks for answers!


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