properly close the camera on pause
This commit is contained in:
parent
c69e34a9b4
commit
a894f6e01c
|
@ -69,6 +69,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
if (hasStorageAndCameraPermissions()) {
|
if (hasStorageAndCameraPermissions()) {
|
||||||
|
mPreview?.onResumed()
|
||||||
resumeCameraItems()
|
resumeCameraItems()
|
||||||
setupPreviewImage(mIsInPhotoMode)
|
setupPreviewImage(mIsInPhotoMode)
|
||||||
scheduleFadeOut()
|
scheduleFadeOut()
|
||||||
|
@ -84,7 +85,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
|
||||||
if (hasStorageAndCameraPermissions()) {
|
if (hasStorageAndCameraPermissions()) {
|
||||||
mOrientationEventListener.enable()
|
mOrientationEventListener.enable()
|
||||||
}
|
}
|
||||||
mPreview?.onResumed()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
|
|
|
@ -76,11 +76,8 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPaused() {
|
override fun onPaused() {
|
||||||
|
closeCamera()
|
||||||
stopBackgroundThread()
|
stopBackgroundThread()
|
||||||
mCaptureSession?.close()
|
|
||||||
mCameraDevice?.close()
|
|
||||||
mCameraDevice = null
|
|
||||||
mCaptureSession = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
|
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
|
||||||
|
@ -125,6 +122,21 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun closeCamera() {
|
||||||
|
try {
|
||||||
|
mCameraOpenCloseLock.acquire()
|
||||||
|
mCaptureSession?.close()
|
||||||
|
mCaptureSession = null
|
||||||
|
mCameraDevice?.close()
|
||||||
|
mCameraDevice = null
|
||||||
|
mImageReader?.close()
|
||||||
|
mImageReader = null
|
||||||
|
} catch (e: Exception) {
|
||||||
|
} finally {
|
||||||
|
mCameraOpenCloseLock.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val imageAvailableListener = ImageReader.OnImageAvailableListener { reader ->
|
private val imageAvailableListener = ImageReader.OnImageAvailableListener { reader ->
|
||||||
val image = reader.acquireNextImage()
|
val image = reader.acquireNextImage()
|
||||||
val buffer = image.planes[0].buffer
|
val buffer = image.planes[0].buffer
|
||||||
|
@ -249,7 +261,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||||
mTextureView.setTransform(matrix)
|
mTextureView.setTransform(matrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var cameraStateCallback = object : CameraDevice.StateCallback() {
|
private val cameraStateCallback = object : CameraDevice.StateCallback() {
|
||||||
override fun onOpened(cameraDevice: CameraDevice) {
|
override fun onOpened(cameraDevice: CameraDevice) {
|
||||||
mCameraOpenCloseLock.release()
|
mCameraOpenCloseLock.release()
|
||||||
mCameraDevice = cameraDevice
|
mCameraDevice = cameraDevice
|
||||||
|
|
Loading…
Reference in New Issue