From 1707daa21d0c6a210ffb902e4b19115cdce3195a Mon Sep 17 00:00:00 2001 From: huangkun Date: Wed, 24 Jan 2024 17:05:13 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=8F=AF=E4=BB=A5=E6=8B=8D=E7=85=A7?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/deploymentTargetDropDown.xml | 2 +- app/build.gradle.kts | 8 +- app/src/main/AndroidManifest.xml | 5 + .../java/com/bing89/travebing/MainActivity.kt | 150 ++++++++++++++++-- 4 files changed, 148 insertions(+), 17 deletions(-) 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) + } + } + ) + } + }