h4p0
h4p0

Reputation: 43

Android How to capture two consecutive frames from camera

I'm trying to program Optical flow on android device. My problem is to get two consecutive frames from camera.

That's the code to get ONE frame.

mCamera.setPreviewCallback(new PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            synchronized (SampleViewBase.this) {
                mFrame2 = data;
                SampleViewBase.this.notify();
            }
        }
    });     

Upvotes: 3

Views: 729

Answers (1)

MobileCushion
MobileCushion

Reputation: 7095

Can't you then do something like:

private byte[] currFrame;
private byte[] prevFrame;    

private void copyFrame(byte[] a){
        if(a != null) prevFrame = a;
}

mCamera.setPreviewCallback(new PreviewCallback() {
            public void onPreviewFrame(byte[] data, Camera camera) {
                synchronized (SampleViewBase.this) {
                    copyFrame(currFrame);              
                    currFrame = data;
                    SampleViewBase.this.notify();
                }
            }
        });  

I'm not sure that's proper Java syntax but just copy the currentFrame, before assigning data to it. Anyway, I think you can also use the VideoCapture class to obtain the frames already in a Mat format. I'm not sure if this class is still available in the latest release, but from my experience with Opencv 2.3 it was much faster to use it to grab camera frames than to use the Android camera.

Upvotes: 1

Related Questions