diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 93fa3ad..0c0c338 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -2,7 +2,7 @@ - + diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e8c5255..5f0fff5 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -33,10 +33,16 @@ android { kotlinOptions { jvmTarget = "1.8" } + } dependencies { - + implementation("androidx.camera:camera-core:1.2.1") + implementation("androidx.camera:camera-camera2:1.2.1") + implementation("androidx.camera:camera-lifecycle:1.2.1") + implementation("androidx.camera:camera-video:1.2.1") + implementation("androidx.camera:camera-view:1.2.1") + implementation("androidx.camera:camera-extensions:1.2.1") implementation("androidx.core:core-ktx:1.10.1") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.9.0") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bee331d..343a860 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,11 @@ + + + + startCamera() + Log.d(TAG, "Average luminosity: $luma") + } + ) + } + */ + + // Select back camera as a default + val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA + + try { + // Unbind use cases before rebinding + cameraProvider.unbindAll() + + // Bind use cases to camera + cameraProvider.bindToLifecycle( + this, cameraSelector, imageCapture) + + } catch(exc: Exception) { + Log.e(TAG, "Use case binding failed", exc) + } + + }, ContextCompat.getMainExecutor(this)) + } + private fun takePhoto() { + // Get a stable reference of the modifiable image capture use case + val imageCapture = imageCapture ?: return + Log.d(TAG, "================>>>>> now do capture photo") + // Create time stamped name and MediaStore entry. + val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US) + .format(System.currentTimeMillis()) + val contentValues = ContentValues().apply { + put(MediaStore.MediaColumns.DISPLAY_NAME, name) + put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg") + put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Image") + } + + // Create output options object which contains file + metadata + val outputOptions = ImageCapture.OutputFileOptions + .Builder(contentResolver, + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + contentValues) + .build() + + // Set up image capture listener, which is triggered after photo has + // been taken + imageCapture.takePicture( + outputOptions, + ContextCompat.getMainExecutor(this), + object : ImageCapture.OnImageSavedCallback { + override fun onError(exc: ImageCaptureException) { + Log.e(TAG, "+++++++++++Photo capture failed: ${exc.message}", exc) + } + + override fun + onImageSaved(output: ImageCapture.OutputFileResults){ + previewImageView.setImageURI(output.savedUri) + val msg = "--------- Photo capture succeeded: ${output.savedUri}" + Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() + Log.d(TAG, msg) + } + } + ) + } + }