Taking 5 pictures in difference of 400 ms

asked 2019-01-15 07:34:07 -0500

student_CS gravatar image

Hi all,

I'm new in opencv for android and I'm trying to take 5 pictures every 400 ms and save them. Here is what I did:

btn_ok = findViewById(R.id.btn_ok); btn_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

            Log.i(TAG, "on button click");


            for(int i =0; i<5;i++) {
                try {
                    TimeUnit.MILLISECONDS.sleep(400);

                } catch (InterruptedException e) {
                    Log.i(TAG, "sleep exception");
                    e.printStackTrace();
                }
                Date sdf = new Date();
                Log.i(TAG, "loop number: "+i);

                String currentDateandTime = sdf.toString();
                String fileName = Environment.getExternalStorageDirectory().getPath() +
                        "/sample_" + currentDateandTime +"__"+ i + ".jpeg";
                cameraBridgeViewBase.takePicture(fileName);

            }
        }

also:

public class myJavaCameraView extends JavaCameraView implements android.hardware.Camera.PictureCallback {

static{
    System.loadLibrary("native-lib");
}

private  static final String TAG = "Opencv";
private String mPictureFilename;

public myJavaCameraView(Context context, AttributeSet attrs ){
    super(context,attrs);
}


public void takePicture(final String fileName)
{
    Log.i(TAG,"Taking picture");
    this.mPictureFilename = fileName;
    mCamera.setPreviewCallback(null);
    mCamera.takePicture(null,null,this);

}

@Override
public void onPictureTaken(byte[] data, Camera camera) {

    Log.i(TAG,"saving bit,ap to file");

    // the camera preview automatically stopped so we start it again
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    // write image to file
    try {
        FileOutputStream fos = new FileOutputStream(mPictureFilename);

        fos.write(data);
        fos.close();

    }
    catch (java.io.IOException e)
    {
        Log.e("PictureDemo", "exception in photocallback",e);

    }

}

Looking at the log files I can see that after the fifth call to takePicture the OnPictureTaken get called 3/4 times so I guess it been stored in a queue. Someone know how can I overcome this problem? I need OnPictureTaken to be called right after every call for takePicture.

edit retag flag offensive close merge delete