improve the camera behaviour at changing the resolution

This commit is contained in:
tibbi 2018-06-05 21:07:45 +02:00
parent f1359e1909
commit 6a1cc57c00
1 changed files with 35 additions and 35 deletions

View File

@ -117,7 +117,10 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
stopBackgroundThread()
}
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {}
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
closeCamera()
openCamera(width, height)
}
override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {}
@ -339,14 +342,6 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
}
private fun createCameraPreviewSession() {
try {
val texture = mTextureView.surfaceTexture!!
texture.setDefaultBufferSize(mPreviewSize!!.width, mPreviewSize!!.height)
val surface = Surface(texture)
mPreviewRequestBuilder = mCameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
mPreviewRequestBuilder!!.addTarget(surface)
val stateCallback = object : CameraCaptureSession.StateCallback() {
override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
if (mCameraDevice == null) {
@ -369,8 +364,16 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
override fun onConfigureFailed(cameraCaptureSession: CameraCaptureSession) {}
}
mCameraDevice!!.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null)
} catch (e: CameraAccessException) {
try {
val texture = mTextureView.surfaceTexture!!
texture.setDefaultBufferSize(mPreviewSize!!.width, mPreviewSize!!.height)
val surface = Surface(texture)
mPreviewRequestBuilder = mCameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
mPreviewRequestBuilder!!.addTarget(surface)
mCameraDevice?.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null)
} catch (e: Exception) {
}
}
@ -603,14 +606,6 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
private fun getCameraCharacteristics(cameraId: String = mCameraId) = getCameraManager().getCameraCharacteristics(cameraId)
private fun takePicture() {
if (mIsFocusSupported) {
lockFocus()
} else {
captureStillPicture()
}
}
private fun getFlashlightMode() = when (mFlashlightState) {
FLASH_ON -> CameraMetadata.FLASH_MODE_TORCH
else -> CameraMetadata.FLASH_MODE_OFF
@ -638,7 +633,8 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
val videoResolutions = ArrayList<MySize>()
ChangeResolutionDialog(mActivity, mUseFrontCamera, photoResolutions, videoResolutions) {
if (oldResolution != getCurrentResolution()) {
closeCamera()
openCamera(mTextureView.width, mTextureView.height)
}
}
}
@ -655,7 +651,11 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
}
override fun tryTakePicture() {
takePicture()
if (mIsFocusSupported) {
lockFocus()
} else {
captureStillPicture()
}
}
override fun toggleRecording(): Boolean {