While testing performance of Android Camera2 API I noticed bug connected with ImageReader
.
When application is launched onImageAvailable()
function is being invoked correctly but after locking and unlocking screen it is not invoked at all. The part of my application which deals with camera is based on Camera2Basic Google tutorial.
What is more, I have installed Camera2Basic Android demo app which presents Camera2 API and I find out that even this app made by Google has the same problem.
Is there some kind of solution to this problem?
After some time spend while using Debug tool in Android Studio I found that the problem with onImageAvailable()
function being called occurs only if
TextureView
that is used to preview is available in onResume()
function.
@Override
public void onResume() {
super.onResume();
startBackgroundThread();
// When the screen is turned off and turned back on, the SurfaceTexture is already
// available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
// a camera and start preview from here (otherwise, we wait until the surface is ready in
// the SurfaceTextureListener).
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
If mTextureView.isAvailable()
returns false and SurfaceTextureListener
is set on mTextureView
then onImageAvailable()
is called properly but if function openCamera(mTextureView.getWidth(), mTextureView.getHeight())
is invoked in onResume()
the problem occurs.