mirror of
https://gitlab.shinice.net/pixeldroid/PixelDroid
synced 2025-02-03 17:07:31 +01:00
Don't re-bind camera if already bound
This prevents flashing black when entering the CameraFragment
This commit is contained in:
parent
1e2c73c2cf
commit
bacd5e138b
@ -188,41 +188,45 @@ class CameraFragment : Fragment() {
|
|||||||
// CameraProvider
|
// CameraProvider
|
||||||
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
|
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
|
||||||
|
|
||||||
// Preview
|
if (camera == null || preview == null || imageCapture == null || !cameraProvider.isBound(preview!!) || !cameraProvider.isBound(imageCapture!!)) {
|
||||||
preview = Preview.Builder()
|
|
||||||
// We request aspect ratio but no resolution
|
|
||||||
.setTargetAspectRatio(screenAspectRatio)
|
|
||||||
// Set initial target rotation
|
|
||||||
.setTargetRotation(rotation)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
// ImageCapture
|
|
||||||
imageCapture = ImageCapture.Builder()
|
|
||||||
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
|
|
||||||
// We request aspect ratio but no resolution to match preview config, but letting
|
|
||||||
// CameraX optimize for whatever specific resolution best fits our use cases
|
|
||||||
.setTargetAspectRatio(screenAspectRatio)
|
|
||||||
// Set initial target rotation, we will have to call this again if rotation changes
|
|
||||||
// during the lifecycle of this use case
|
|
||||||
.setTargetRotation(rotation)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
// Must unbind the use-cases before rebinding them
|
// Preview
|
||||||
cameraProvider.unbindAll()
|
preview = Preview.Builder()
|
||||||
|
// We request aspect ratio but no resolution
|
||||||
|
.setTargetAspectRatio(screenAspectRatio)
|
||||||
|
// Set initial target rotation
|
||||||
|
.setTargetRotation(rotation)
|
||||||
|
.build()
|
||||||
|
|
||||||
try {
|
// ImageCapture
|
||||||
// A variable number of use-cases can be passed here -
|
imageCapture = ImageCapture.Builder()
|
||||||
// camera provides access to CameraControl & CameraInfo
|
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
|
||||||
camera = cameraProvider.bindToLifecycle(
|
// We request aspect ratio but no resolution to match preview config, but letting
|
||||||
this, cameraSelector, preview, imageCapture
|
// CameraX optimize for whatever specific resolution best fits our use cases
|
||||||
)
|
.setTargetAspectRatio(screenAspectRatio)
|
||||||
|
// Set initial target rotation, we will have to call this again if rotation changes
|
||||||
|
// during the lifecycle of this use case
|
||||||
|
.setTargetRotation(rotation)
|
||||||
|
.build()
|
||||||
|
|
||||||
// Attach the viewfinder's surface provider to preview use case
|
// Must unbind the use-cases before rebinding them
|
||||||
preview?.setSurfaceProvider(viewFinder.surfaceProvider)
|
cameraProvider.unbindAll()
|
||||||
} catch (exc: Exception) {
|
|
||||||
Log.e(TAG, "Use case binding failed", exc)
|
try {
|
||||||
|
// A variable number of use-cases can be passed here -
|
||||||
|
// camera provides access to CameraControl & CameraInfo
|
||||||
|
camera = cameraProvider.bindToLifecycle(
|
||||||
|
this, cameraSelector, preview, imageCapture
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// Attach the viewfinder's surface provider to preview use case
|
||||||
|
preview?.setSurfaceProvider(viewFinder.surfaceProvider)
|
||||||
|
} catch (exc: Exception) {
|
||||||
|
Log.e(TAG, "Use case binding failed", exc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}, ContextCompat.getMainExecutor(requireContext()))
|
}, ContextCompat.getMainExecutor(requireContext()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,7 +294,7 @@ class CameraFragment : Fragment() {
|
|||||||
|
|
||||||
private fun setupUploadImage(controls: View) {
|
private fun setupUploadImage(controls: View) {
|
||||||
// Listener for button used to view the most recent photo
|
// Listener for button used to view the most recent photo
|
||||||
controls.findViewById<ImageButton>(R.id.photo_view_button).setOnClickListener {
|
controls.findViewById<ImageButton>(R.id.photo_view_button)?.setOnClickListener {
|
||||||
Intent().apply {
|
Intent().apply {
|
||||||
type = "image/*"
|
type = "image/*"
|
||||||
action = Intent.ACTION_GET_CONTENT
|
action = Intent.ACTION_GET_CONTENT
|
||||||
@ -306,7 +310,7 @@ class CameraFragment : Fragment() {
|
|||||||
|
|
||||||
private fun setupFlipCameras(controls: View) {
|
private fun setupFlipCameras(controls: View) {
|
||||||
// Listener for button used to switch cameras
|
// Listener for button used to switch cameras
|
||||||
controls.findViewById<ImageButton>(R.id.camera_switch_button).setOnClickListener {
|
controls.findViewById<ImageButton>(R.id.camera_switch_button)?.setOnClickListener {
|
||||||
lensFacing = if (CameraSelector.LENS_FACING_FRONT == lensFacing) {
|
lensFacing = if (CameraSelector.LENS_FACING_FRONT == lensFacing) {
|
||||||
CameraSelector.LENS_FACING_BACK
|
CameraSelector.LENS_FACING_BACK
|
||||||
} else {
|
} else {
|
||||||
@ -327,7 +331,7 @@ class CameraFragment : Fragment() {
|
|||||||
|
|
||||||
private fun setupImageCapture(controls: View) {
|
private fun setupImageCapture(controls: View) {
|
||||||
// Listener for button used to capture photo
|
// Listener for button used to capture photo
|
||||||
controls.findViewById<ImageButton>(R.id.camera_capture_button).setOnClickListener {
|
controls.findViewById<ImageButton>(R.id.camera_capture_button)?.setOnClickListener {
|
||||||
|
|
||||||
// Get a stable reference of the modifiable image capture use case
|
// Get a stable reference of the modifiable image capture use case
|
||||||
imageCapture?.let { imageCapture ->
|
imageCapture?.let { imageCapture ->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user