-
-
Save deepak140596/3ef33be34da73e6429ddeaf75aab6bf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun startCamera() { | |
... | |
// Add this before CameraX.bindToLifecycle | |
// Create configuration object for the image capture use case | |
val imageCaptureConfig = ImageCaptureConfig.Builder() | |
.apply { | |
setTargetAspectRatio(Rational(1, 1)) | |
// We don't set a resolution for image capture; instead, we | |
// select a capture mode which will infer the appropriate | |
// resolution based on aspect ration and requested mode | |
setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY) | |
}.build() | |
// Build the image capture use case and attach button click listener | |
val imageCapture = ImageCapture(imageCaptureConfig) | |
findViewById<ImageButton>(R.id.capture_button).setOnClickListener { | |
val file = File(externalMediaDirs.first(), | |
"${System.currentTimeMillis()}.jpg") | |
imageCapture.takePicture(file, | |
object : ImageCapture.OnImageSavedListener { | |
override fun onError(error: ImageCapture.UseCaseError, | |
message: String, exc: Throwable?) { | |
val msg = "Photo capture failed: $message" | |
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() | |
Log.e("CameraXApp", msg) | |
exc?.printStackTrace() | |
} | |
override fun onImageSaved(file: File) { | |
val msg = "Photo capture succeeded: ${file.absolutePath}" | |
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() | |
Log.d("CameraXApp", msg) | |
} | |
}) | |
} | |
// Bind use cases to lifecycle | |
// If Android Studio complains about "this" being not a LifecycleOwner | |
// try rebuilding the project or updating the appcompat dependency to | |
// version 1.1.0 or higher. | |
CameraX.bindToLifecycle(this, preview) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment