From dd229b57947fceb87e974f7d252c4428b46904e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Hern=C3=A1ndez?= Date: Sun, 24 Jul 2022 21:51:49 -0500 Subject: [PATCH 1/3] add flash mode and camera mode --- .../easycameraandgallery/CameraActivity.kt | 97 ++++++++++++++++--- .../easycameraandgallery/OptionsCamera.kt | 4 +- .../src/main/res/drawable/gradient_bottom.xml | 8 ++ .../src/main/res/drawable/gradient_top.xml | 10 ++ .../drawable/ic_baseline_camera_front_24.xml | 5 + .../drawable/ic_baseline_camera_rear_24.xml | 5 + .../drawable/ic_baseline_flash_auto_24.xml | 5 + .../res/drawable/ic_baseline_flash_off_24.xml | 5 + .../res/drawable/ic_baseline_flash_on_24.xml | 5 + .../src/main/res/layout/activity_camera.xml | 54 ++++++++++- .../src/main/res/values/colors.xml | 5 + .../src/main/res/values/dimens.xml | 2 +- .../dev/ahrsoft/cameralibrary/MainActivity.kt | 2 + 13 files changed, 189 insertions(+), 18 deletions(-) create mode 100644 EasyCameraAndGallery/src/main/res/drawable/gradient_bottom.xml create mode 100644 EasyCameraAndGallery/src/main/res/drawable/gradient_top.xml create mode 100644 EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_front_24.xml create mode 100644 EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_rear_24.xml create mode 100644 EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_auto_24.xml create mode 100644 EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_off_24.xml create mode 100644 EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_on_24.xml create mode 100644 EasyCameraAndGallery/src/main/res/values/colors.xml diff --git a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt index 50dc790..7a416e9 100644 --- a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt +++ b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt @@ -52,6 +52,7 @@ class CameraActivity : AppCompatActivity() { private var cameraProvider: ProcessCameraProvider? = null private var lensFacing: Int = CameraSelector.LENS_FACING_BACK + private var flashMode: Int = ImageCapture.FLASH_MODE_OFF private var preview: Preview? = null private var imageCapture: ImageCapture? = null private lateinit var outputDirectory: File @@ -67,15 +68,52 @@ class CameraActivity : AppCompatActivity() { binding = ActivityCameraBinding.inflate(layoutInflater) val view = binding.root setContentView(view) + if (allPermissionsGranted()) { + startCamera() + getAllImageFromGallery() + } else { + ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS) + } + initUI() } private fun initUI() { optionsCamera = (intent.getSerializableExtra("options") as? OptionsCamera)!! + flashModeOptions(optionsCamera.flash) + with(binding) { cameraCaptureButton.setOnClickListener { takePhoto() } + ibFrontCamera.setOnClickListener { + if (CameraSelector.LENS_FACING_FRONT == lensFacing){ + lensFacing = CameraSelector.LENS_FACING_BACK + enableFrontCamera(false) + }else{ + lensFacing = CameraSelector.LENS_FACING_FRONT + enableFrontCamera(true) + } + bindCameraUseCases() + } + ibFlashCamera.setOnClickListener { + when(flashMode){ + ImageCapture.FLASH_MODE_OFF ->{ + flashMode = ImageCapture.FLASH_MODE_ON + caseFlashMode() + } + ImageCapture.FLASH_MODE_ON -> { + flashMode = ImageCapture.FLASH_MODE_AUTO + caseFlashMode() + } + ImageCapture.FLASH_MODE_AUTO -> { + flashMode = ImageCapture.FLASH_MODE_OFF + caseFlashMode() + } + } + bindCameraUseCases() + } + fabSendData.setOnClickListener { val list = imageSelected.map { it.image @@ -83,11 +121,28 @@ class CameraActivity : AppCompatActivity() { getListPath(list as ArrayList) } } - if (allPermissionsGranted()) { - startCamera() - getAllImageFromGallery() - } else { - ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS) + } + + private fun caseFlashMode(){ + when(flashMode){ + ImageCapture.FLASH_MODE_OFF ->{ + binding.ibFlashCamera.setImageResource(R.drawable.ic_baseline_flash_off_24) + } + ImageCapture.FLASH_MODE_ON -> { + binding.ibFlashCamera.setImageResource(R.drawable.ic_baseline_flash_on_24) + + } + ImageCapture.FLASH_MODE_AUTO -> { + binding.ibFlashCamera.setImageResource(R.drawable.ic_baseline_flash_auto_24) + } + } + } + + private fun enableFrontCamera(isFront : Boolean) { + if (isFront){ + binding.ibFrontCamera.setImageResource(R.drawable.ic_baseline_camera_rear_24) + }else{ + binding.ibFrontCamera.setImageResource(R.drawable.ic_baseline_camera_front_24) } } @@ -95,12 +150,16 @@ class CameraActivity : AppCompatActivity() { val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ cameraProvider = cameraProviderFuture.get() - - lensFacing = when { - hasBackCamera() -> CameraSelector.LENS_FACING_BACK - hasFrontCamera() -> CameraSelector.LENS_FACING_FRONT - else -> throw IllegalStateException("No existen dispositivo de captura disponibles...") - } + if (optionsCamera.isFrontFacing){ + enableFrontCamera(true) + lensFacing = CameraSelector.LENS_FACING_FRONT + hasBackCamera() + + }else{ + enableFrontCamera(false) + lensFacing = CameraSelector.LENS_FACING_BACK + hasFrontCamera() + } bindCameraUseCases() }, ContextCompat.getMainExecutor(this)) } @@ -313,6 +372,21 @@ class CameraActivity : AppCompatActivity() { } } + private fun flashModeOptions(flash: Flash) { + flashMode = when(flash){ + Flash.On -> { + ImageCapture.FLASH_MODE_ON + } + Flash.Off -> { + ImageCapture.FLASH_MODE_OFF + } + Flash.Auto -> { + ImageCapture.FLASH_MODE_AUTO + } + } + caseFlashMode() + } + private fun getScreenWidth(): Int { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { @@ -374,6 +448,7 @@ class CameraActivity : AppCompatActivity() { .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) .setTargetAspectRatio(aspectRadio(optionsCamera.ratio)) .setTargetRotation(ROTATION_0) + .setFlashMode(flashMode) .build() cameraProvider.unbindAll() diff --git a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt index 41e0557..3813381 100644 --- a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt +++ b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt @@ -8,14 +8,14 @@ import java.io.Serializable data class OptionsCamera( var count : Int = 1, var path : String = "EasyCamera/", - var flash: Flash = Flash.Disabled, + var flash: Flash = Flash.Auto, var isFrontFacing : Boolean = false, var ratio : Ratio = Ratio.RATIO_AUTO ) : Serializable @Parcelize enum class Flash : Parcelable { - Disabled, On, Off, Auto + On, Off, Auto } @Parcelize diff --git a/EasyCameraAndGallery/src/main/res/drawable/gradient_bottom.xml b/EasyCameraAndGallery/src/main/res/drawable/gradient_bottom.xml new file mode 100644 index 0000000..e37ad1a --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/gradient_bottom.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/EasyCameraAndGallery/src/main/res/drawable/gradient_top.xml b/EasyCameraAndGallery/src/main/res/drawable/gradient_top.xml new file mode 100644 index 0000000..855065d --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/gradient_top.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_front_24.xml b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_front_24.xml new file mode 100644 index 0000000..f631d07 --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_front_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_rear_24.xml b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_rear_24.xml new file mode 100644 index 0000000..1978cfa --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_camera_rear_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_auto_24.xml b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_auto_24.xml new file mode 100644 index 0000000..10d2282 --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_auto_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_off_24.xml b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_off_24.xml new file mode 100644 index 0000000..dcdd1af --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_off_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_on_24.xml b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_on_24.xml new file mode 100644 index 0000000..72a3cc2 --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_flash_on_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml b/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml index b4908b0..21b0f84 100644 --- a/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml +++ b/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml @@ -6,6 +6,31 @@ android:layout_height="match_parent" tools:context=".CameraActivity"> + + + + - - + tools:layout_editor_absoluteY="0dp" /> + android:contentDescription="@string/tomar_foto" + android:elevation="9dp"/> + + + + \ No newline at end of file diff --git a/EasyCameraAndGallery/src/main/res/values/colors.xml b/EasyCameraAndGallery/src/main/res/values/colors.xml new file mode 100644 index 0000000..2eb23b3 --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #00000000 + #FF000000 + \ No newline at end of file diff --git a/EasyCameraAndGallery/src/main/res/values/dimens.xml b/EasyCameraAndGallery/src/main/res/values/dimens.xml index f72d6fa..3b8ff3c 100644 --- a/EasyCameraAndGallery/src/main/res/values/dimens.xml +++ b/EasyCameraAndGallery/src/main/res/values/dimens.xml @@ -1,5 +1,5 @@ - 50dp + 60dp 25dp \ No newline at end of file diff --git a/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt b/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt index ff31ef2..5128110 100644 --- a/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt +++ b/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt @@ -20,7 +20,9 @@ class MainActivity : AppCompatActivity() { initCall() val optionsCamera = OptionsCamera( + ratio = Ratio.RATIO_16_9, path = "Evidences", + flash = Flash.Off ) binding.textInput.setOnClickListener { From de317cdbaaf8a0d3673336158a75627986fd6277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Hern=C3=A1ndez?= Date: Sun, 24 Jul 2022 22:58:22 -0500 Subject: [PATCH 2/3] add gallery get data --- .../easycameraandgallery/CameraActivity.kt | 83 +++++++++++++++++++ .../res/drawable/ic_baseline_image_24.xml | 5 ++ .../src/main/res/layout/activity_camera.xml | 21 ++++- .../src/main/res/values/dimens.xml | 1 + .../dev/ahrsoft/cameralibrary/MainActivity.kt | 3 +- 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 EasyCameraAndGallery/src/main/res/drawable/ic_baseline_image_24.xml diff --git a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt index 7a416e9..699b8cf 100644 --- a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt +++ b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt @@ -1,6 +1,7 @@ package dev.ahrsoft.easycameraandgallery import android.annotation.SuppressLint +import android.app.Activity import android.content.ContentUris import android.content.ContentValues import android.content.Intent @@ -13,6 +14,7 @@ import android.net.Uri import android.os.Build import android.os.Bundle import android.os.Environment +import android.provider.DocumentsContract import android.provider.MediaStore import android.util.DisplayMetrics import android.util.Log @@ -21,6 +23,8 @@ import android.view.View import android.view.WindowInsets import android.webkit.MimeTypeMap import android.widget.Toast +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.camera.core.* import androidx.camera.lifecycle.ProcessCameraProvider @@ -62,6 +66,8 @@ class CameraActivity : AppCompatActivity() { private val imageList = arrayListOf() private val imageSelected = arrayListOf() private lateinit var adapter: GalleryAdapter + private lateinit var resultGallery: ActivityResultLauncher + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -83,6 +89,9 @@ class CameraActivity : AppCompatActivity() { flashModeOptions(optionsCamera.flash) with(binding) { + galleryCaptureButton.setOnClickListener { + getPickImageIntent() + } cameraCaptureButton.setOnClickListener { takePhoto() } @@ -121,6 +130,31 @@ class CameraActivity : AppCompatActivity() { getListPath(list as ArrayList) } } + openCallback() + } + + private fun openCallback() { + resultGallery = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == Activity.RESULT_OK) { + val data = result.data + if (data != null) { + if (data.clipData != null) { + val mClipData = data.clipData + for (i in 0 until mClipData!!.itemCount) { + val item = mClipData.getItemAt(i) + val uri = item.uri + getPathFromURI(uri) + } + } else if (data.data != null) { + val uri = data.data + if (uri != null) { + getPathFromURI(uri) + } + } + } + } + } } private fun caseFlashMode(){ @@ -146,6 +180,46 @@ class CameraActivity : AppCompatActivity() { } } + private fun getPathFromURI(uri: Uri) { + var realPath = String() + uri.path?.let { path -> + val databaseUri: Uri + val selection: String? + val selectionArgs: Array? + if (path.contains("/document/image:")) { + databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI + selection = "_id=?" + selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1]) + } else { + databaseUri = uri + selection = null + selectionArgs = null + } + try { + val column = "_data" + val projection = arrayOf(column) + val cursor = contentResolver.query( + databaseUri, + projection, + selection, + selectionArgs, + null + ) + cursor?.let { + if (it.moveToFirst()) { + val columnIndex = cursor.getColumnIndexOrThrow(column) + realPath = cursor.getString(columnIndex) + } + cursor.close() + } + } catch (e: Exception) { + println(e) + } + } + addImage(realPath) + } + + private fun startCamera() { val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ @@ -168,9 +242,11 @@ class CameraActivity : AppCompatActivity() { if (isCompleteSelect){ binding.fabSendData.visibility = View.VISIBLE binding.cameraCaptureButton.visibility = View.GONE + binding.galleryCaptureButton.visibility = View.GONE }else{ binding.fabSendData.visibility = View.GONE binding.cameraCaptureButton.visibility = View.VISIBLE + binding.galleryCaptureButton.visibility = View.VISIBLE } } @@ -468,6 +544,13 @@ class CameraActivity : AppCompatActivity() { return if (path.exists()) path else filesDir } + fun getPickImageIntent() { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "image/*" + intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false) + resultGallery.launch(intent) + } + private fun getListPath(list: ArrayList) { val result = Intent() result.putExtra(IMAGE_RESULTS, list) diff --git a/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_image_24.xml b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_image_24.xml new file mode 100644 index 0000000..d35859d --- /dev/null +++ b/EasyCameraAndGallery/src/main/res/drawable/ic_baseline_image_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml b/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml index 21b0f84..3eaae5b 100644 --- a/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml +++ b/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml @@ -59,14 +59,13 @@ android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginBottom="16dp" + android:elevation="9dp" app:layout_constraintBottom_toTopOf="@+id/camera.capture.button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - tools:listitem="@layout/image_picker_list" tools:itemCount="8" - tools:orientation="horizontal" - android:elevation="9dp" - /> + tools:listitem="@layout/image_picker_list" + tools:orientation="horizontal" /> + + + 60dp 25dp + 40dp \ No newline at end of file diff --git a/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt b/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt index 5128110..6a8400f 100644 --- a/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt +++ b/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt @@ -22,7 +22,8 @@ class MainActivity : AppCompatActivity() { val optionsCamera = OptionsCamera( ratio = Ratio.RATIO_16_9, path = "Evidences", - flash = Flash.Off + flash = Flash.Off, + count = 2 ) binding.textInput.setOnClickListener { From a40fbae9eec430142f7103fec8febd8883e84b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Hern=C3=A1ndez?= Date: Sun, 24 Jul 2022 23:01:46 -0500 Subject: [PATCH 3/3] fix ui --- .../src/main/res/layout/activity_camera.xml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml b/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml index 3eaae5b..60ee719 100644 --- a/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml +++ b/EasyCameraAndGallery/src/main/res/layout/activity_camera.xml @@ -71,14 +71,14 @@ android:id="@+id/camera.capture.button" android:layout_width="@dimen/round_button_large" android:layout_height="@dimen/round_button_large" - android:layout_marginBottom="@dimen/shutter_button_margin" - android:scaleType="fitCenter" + android:layout_marginBottom="24dp" android:background="@drawable/ic_camera" - app:layout_constraintLeft_toLeftOf="parent" - app:layout_constraintRight_toRightOf="parent" - app:layout_constraintBottom_toBottomOf="parent" android:contentDescription="@string/tomar_foto" - android:elevation="9dp"/> + android:elevation="9dp" + android:scaleType="fitCenter" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintRight_toRightOf="parent" /> + app:layout_constraintTop_toTopOf="@+id/camera.capture.button" />