I'm working on a face detection app with android studio (Based on Camera2 Api face recon). The problem is the following : the capture session is stopped when some condition are true. Then, the user can take the picture or retry (capture session is started). I would like to give time to user after he used retry button in order to replace his face.
Buttons retry and take picture appear when the face take more than 20% of the screen. During the same time, I use stopRepeating in order to freeze the camera capture session. I tried to put these functions in a handler, but when I put the stopRepeating inside the handler, the app crash.
if(face/sizeEc>0.20 && face/sizeEc<0.6){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
TextView textView = (TextView) getView().findViewById(R.id.picture);
textView.setVisibility(View.VISIBLE);
TextView textView2 = (TextView) getView().findViewById(R.id.picture2);
textView2.setVisibility(View.VISIBLE);
try {
mCaptureSession.stopRepeating();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}, 2000);
//I tried to use this handler in order to allow user to replace his head
//But there is a problem when the handler is used with stopRepeating
}
and the code for the retry button :
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mCaptureSession.setRepeatingRequest(mPreviewRequest,
mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
textView.setVisibility(View.GONE);
textView2.setVisibility(View.GONE);
asDelayed=true;
}
});
As I said, when I do this, the app crash when I use take picture after a retry. I would like to give time to user to replace his face after using retry button.
[EDIT] Here is the error
2019-05-15 08:44:12.149 28923-28923/weladee.frontware.com.weladee_android E/AndroidRuntime: FATAL EXCEPTION: main Process: weladee.frontware.com.weladee_android, PID: 28923 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.camera2.CameraCaptureSession.stopRepeating()' on a null object reference at weladee.frontware.com.weladee_android.utils.front_camera.CameraFacingFront$4$1$1.run(CameraFacingFront.java:429) at android.os.Handler.handleCallback(Handler.java:761) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
You're resetting mCaptureSession to null somewhere in your code before the handler method can run, or this particular mCaptureSession is never correctly initialized to begin with.
You could explicitly use a copy of mCaptureSession to pass into the Handler runnable to avoid the nullness issue if you can't find what sets the member variable to null, but then you still have to make sure nothing actually closes the session before your handler runs.