From fbd7a819f49b058577904d4cebb191f8918872ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Hern=C3=A1ndez?= Date: Mon, 25 Jul 2022 22:10:59 -0500 Subject: [PATCH 1/2] fix gallery android 10 and count gallery librery --- .../src/main/AndroidManifest.xml | 1 - .../easycameraandgallery/CameraActivity.kt | 90 +++++++++++++++---- .../easycameraandgallery/OptionsCamera.kt | 3 +- app/build.gradle | 2 + .../dev/ahrsoft/cameralibrary/MainActivity.kt | 28 +++++- 5 files changed, 106 insertions(+), 18 deletions(-) diff --git a/EasyCameraAndGallery/src/main/AndroidManifest.xml b/EasyCameraAndGallery/src/main/AndroidManifest.xml index 01375c7..6640efe 100644 --- a/EasyCameraAndGallery/src/main/AndroidManifest.xml +++ b/EasyCameraAndGallery/src/main/AndroidManifest.xml @@ -12,7 +12,6 @@ diff --git a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt index b04fe6a..954ab9d 100644 --- a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt +++ b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt @@ -2,6 +2,7 @@ package dev.ahrsoft.easycameraandgallery import android.annotation.SuppressLint import android.app.Activity +import android.content.ContentResolver import android.content.ContentUris import android.content.ContentValues import android.content.Intent @@ -70,17 +71,17 @@ class CameraActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + supportActionBar?.hide() binding = ActivityCameraBinding.inflate(layoutInflater) val view = binding.root setContentView(view) + initUI() if (allPermissionsGranted()) { startCamera() getAllImageFromGallery() } else { ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS) } - - initUI() } private fun initUI() { @@ -179,6 +180,7 @@ class CameraActivity : AppCompatActivity() { } } + private fun getPathFromURI(uri: Uri) { var realPath = String() uri.path?.let { path -> @@ -249,14 +251,41 @@ class CameraActivity : AppCompatActivity() { } } + private val imageCollection: Uri by lazy { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + MediaStore.Images.Media.getContentUri( + MediaStore.VOLUME_EXTERNAL + ) + } else { + MediaStore.Images.Media.EXTERNAL_CONTENT_URI + } + } + private fun getAllImageFromGallery(){ imageList.clear() val columns = arrayOf(MediaStore.Images.Media._ID) val orderBy = MediaStore.Images.Media.DATE_ADDED - contentResolver.query( - MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, - null, null, "$orderBy DESC" - )?.use { cursor -> + + val cursor : Cursor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val bundle = Bundle() + bundle.putInt( + ContentResolver.QUERY_ARG_SORT_DIRECTION, + ContentResolver.QUERY_SORT_DIRECTION_DESCENDING + ) + if (optionsCamera.galleryCount > 0) + bundle.putInt(ContentResolver.QUERY_ARG_LIMIT,optionsCamera.galleryCount) + + contentResolver.query(imageCollection,columns,bundle,null)!! + }else{ + val filter = if (optionsCamera.galleryCount > 0) { + "$orderBy DESC LIMIT ${optionsCamera.galleryCount}" + }else{ + orderBy + } + contentResolver.query(imageCollection,columns,null,null, filter)!! + } + + cursor.let { cursor -> val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID) while (cursor.moveToNext()) { val id = cursor.getLong(idColumn) @@ -265,6 +294,7 @@ class CameraActivity : AppCompatActivity() { getRealPathFromUri(uri)?.let { imageList.add(imageModel) } } } + cursor.close() print(imageList) setImageList() } @@ -385,19 +415,49 @@ class CameraActivity : AppCompatActivity() { } } + @Deprecated("delete function") fun getRealPathFromUri(contentUri: Uri): String? { - var cursor: Cursor? = null - return try { - val proj = arrayOf(MediaStore.Images.Media.DATA) - cursor = contentResolver.query(contentUri, proj, null, null, null) - val index: Int = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) - cursor.moveToFirst() - cursor.getString(index) - } finally { - cursor?.close() + + var realPath = String() + contentUri.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(contentUri).split(":")[1]) + } else { + databaseUri = contentUri + 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) + } } + return realPath } + + private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED } diff --git a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt index 3813381..eba00f9 100644 --- a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt +++ b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/OptionsCamera.kt @@ -10,7 +10,8 @@ data class OptionsCamera( var path : String = "EasyCamera/", var flash: Flash = Flash.Auto, var isFrontFacing : Boolean = false, - var ratio : Ratio = Ratio.RATIO_AUTO + var ratio : Ratio = Ratio.RATIO_AUTO, + var galleryCount : Int = 0 ) : Serializable @Parcelize diff --git a/app/build.gradle b/app/build.gradle index 8b34ac6..64837fd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,6 +40,8 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation project(path: ':EasyCameraAndGallery') + testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' diff --git a/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt b/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt index bfe77fb..3369f53 100644 --- a/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt +++ b/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt @@ -1,17 +1,43 @@ package dev.ahrsoft.cameralibrary - +import android.app.Activity +import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.activity.result.ActivityResultLauncher import dev.ahrsoft.cameralibrary.databinding.ActivityMainBinding +import dev.ahrsoft.easycameraandgallery.* class MainActivity : AppCompatActivity() { lateinit var binding: ActivityMainBinding + private lateinit var resultScan: ActivityResultLauncher override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) + initCall() + + val optionsCamera = OptionsCamera( + ratio = Ratio.RATIO_16_9, + path = "Evidences", + flash = Flash.Off, + galleryCount = 4 + ) + + binding.textInput.setOnClickListener { + EasyCamera.start(this, optionsCamera = optionsCamera, resultScan = resultScan) + } + } + + private fun initCall() { + resultScan = registerForActivityResult(androidx.activity.result.contract. + ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == Activity.RESULT_OK) { + val list = result.data?.getStringArrayListExtra(EasyCamera.IMAGE_RESULTS) + print(list) + } + } } } \ No newline at end of file From bfb4b7a205a528ade69d584b9220dc73ec61742f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Hern=C3=A1ndez?= Date: Mon, 25 Jul 2022 22:30:15 -0500 Subject: [PATCH 2/2] add Utils.kt class --- .../easycameraandgallery/CameraActivity.kt | 334 +++++------------- .../dev/ahrsoft/easycameraandgallery/Utils.kt | 124 +++++++ .../dev/ahrsoft/cameralibrary/MainActivity.kt | 2 +- 3 files changed, 222 insertions(+), 238 deletions(-) create mode 100644 EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/Utils.kt diff --git a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt index 954ab9d..8b6985f 100644 --- a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt +++ b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/CameraActivity.kt @@ -33,6 +33,7 @@ import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.net.toFile import androidx.recyclerview.widget.LinearLayoutManager +import com.bumptech.glide.util.Util import dev.ahrsoft.easycameraandgallery.Constant.FILENAME import dev.ahrsoft.easycameraandgallery.Constant.PHOTO_EXTENSION import dev.ahrsoft.easycameraandgallery.Constant.RATIO_16_9_VALUE @@ -67,11 +68,13 @@ class CameraActivity : AppCompatActivity() { private val imageSelected = arrayListOf() private lateinit var adapter: GalleryAdapter private lateinit var resultGallery: ActivityResultLauncher + private lateinit var utils: Utils override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) supportActionBar?.hide() + utils = Utils(this) binding = ActivityCameraBinding.inflate(layoutInflater) val view = binding.root setContentView(view) @@ -85,7 +88,7 @@ class CameraActivity : AppCompatActivity() { } private fun initUI() { - optionsCamera = (intent.getSerializableExtra("options") as? OptionsCamera)!! + optionsCamera = (intent.getSerializableExtra("options") as? OptionsCamera)!! flashModeOptions(optionsCamera.flash) with(binding) { @@ -96,18 +99,18 @@ class CameraActivity : AppCompatActivity() { takePhoto() } ibFrontCamera.setOnClickListener { - if (CameraSelector.LENS_FACING_FRONT == lensFacing){ - lensFacing = CameraSelector.LENS_FACING_BACK + if (CameraSelector.LENS_FACING_FRONT == lensFacing) { + lensFacing = CameraSelector.LENS_FACING_BACK enableFrontCamera(false) - }else{ + } else { lensFacing = CameraSelector.LENS_FACING_FRONT enableFrontCamera(true) } bindCameraUseCases() } ibFlashCamera.setOnClickListener { - when(flashMode){ - ImageCapture.FLASH_MODE_OFF ->{ + when (flashMode) { + ImageCapture.FLASH_MODE_OFF -> { flashMode = ImageCapture.FLASH_MODE_ON caseFlashMode() } @@ -144,12 +147,14 @@ class CameraActivity : AppCompatActivity() { for (i in 0 until mClipData!!.itemCount) { val item = mClipData.getItemAt(i) val uri = item.uri - getPathFromURI(uri) + val path = utils.getRealPathFromUri(uri) + addImage(path) } } else if (data.data != null) { val uri = data.data if (uri != null) { - getPathFromURI(uri) + val path = utils.getRealPathFromUri(uri) + addImage(path) } } } @@ -157,9 +162,9 @@ class CameraActivity : AppCompatActivity() { } } - private fun caseFlashMode(){ - when(flashMode){ - ImageCapture.FLASH_MODE_OFF ->{ + private fun caseFlashMode() { + when (flashMode) { + ImageCapture.FLASH_MODE_OFF -> { binding.ibFlashCamera.setImageResource(R.drawable.ic_flash_off_camera) } ImageCapture.FLASH_MODE_ON -> { @@ -172,79 +177,38 @@ class CameraActivity : AppCompatActivity() { } } - private fun enableFrontCamera(isFront : Boolean) { - if (isFront){ + private fun enableFrontCamera(isFront: Boolean) { + if (isFront) { binding.ibFrontCamera.setImageResource(R.drawable.ic_camera_back) - }else{ + } else { binding.ibFrontCamera.setImageResource(R.drawable.ic_front_camera) } } - - 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({ cameraProvider = cameraProviderFuture.get() - if (optionsCamera.isFrontFacing){ - enableFrontCamera(true) - lensFacing = CameraSelector.LENS_FACING_FRONT - hasBackCamera() - - }else{ - enableFrontCamera(false) - lensFacing = CameraSelector.LENS_FACING_BACK - hasFrontCamera() - } + if (optionsCamera.isFrontFacing) { + enableFrontCamera(true) + lensFacing = CameraSelector.LENS_FACING_FRONT + hasBackCamera() + + } else { + enableFrontCamera(false) + lensFacing = CameraSelector.LENS_FACING_BACK + hasFrontCamera() + } bindCameraUseCases() }, ContextCompat.getMainExecutor(this)) } - private fun hideUI(isCompleteSelect : Boolean){ - if (isCompleteSelect){ + private fun hideUI(isCompleteSelect: Boolean) { + if (isCompleteSelect) { binding.fabSendData.visibility = View.VISIBLE binding.cameraCaptureButtonCamera.visibility = View.GONE binding.galleryCaptureButtonCamera.visibility = View.GONE - }else{ + } else { binding.fabSendData.visibility = View.GONE binding.cameraCaptureButtonCamera.visibility = View.VISIBLE binding.galleryCaptureButtonCamera.visibility = View.VISIBLE @@ -261,37 +225,38 @@ class CameraActivity : AppCompatActivity() { } } - private fun getAllImageFromGallery(){ + private fun getAllImageFromGallery() { imageList.clear() val columns = arrayOf(MediaStore.Images.Media._ID) val orderBy = MediaStore.Images.Media.DATE_ADDED - val cursor : Cursor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val cursor: Cursor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val bundle = Bundle() bundle.putInt( ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING ) if (optionsCamera.galleryCount > 0) - bundle.putInt(ContentResolver.QUERY_ARG_LIMIT,optionsCamera.galleryCount) + bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, optionsCamera.galleryCount) - contentResolver.query(imageCollection,columns,bundle,null)!! - }else{ + contentResolver.query(imageCollection, columns, bundle, null)!! + } else { val filter = if (optionsCamera.galleryCount > 0) { "$orderBy DESC LIMIT ${optionsCamera.galleryCount}" - }else{ + } else { orderBy } - contentResolver.query(imageCollection,columns,null,null, filter)!! + contentResolver.query(imageCollection, columns, null, null, filter)!! } cursor.let { cursor -> val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID) while (cursor.moveToNext()) { val id = cursor.getLong(idColumn) - val uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id) - val imageModel = ImageModel(image = getRealPathFromUri(uri)) - getRealPathFromUri(uri)?.let { imageList.add(imageModel) } + val uri = + ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id) + val imageModel = ImageModel(image = utils.getRealPathFromUri(uri)) + utils.getRealPathFromUri(uri).let { imageList.add(imageModel) } } } cursor.close() @@ -299,16 +264,17 @@ class CameraActivity : AppCompatActivity() { setImageList() } - private fun setImageList(){ - adapter = GalleryAdapter(this,imageList) - binding.rvGalleryCamera.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false) + private fun setImageList() { + adapter = GalleryAdapter(this, imageList) + binding.rvGalleryCamera.layoutManager = + LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) binding.rvGalleryCamera.adapter = adapter - adapter.setOnItemClickListener(object : GalleryAdapter.OnItemClickListener{ + adapter.setOnItemClickListener(object : GalleryAdapter.OnItemClickListener { override fun onItemClick(position: Int) { val imageModel = imageList[position] - if (imageModel.isSelected){ + if (imageModel.isSelected) { unSelectImage(imageModel, position) - }else{ + } else { selectImage(imageModel, position) } } @@ -316,9 +282,9 @@ class CameraActivity : AppCompatActivity() { } private fun selectImage(imageModel: ImageModel, position: Int) { - if (isCompletedSelect()){ + if (isCompletedSelect()) { hideUI(true) - }else{ + } else { imageSelected.add(imageModel) imageList[position].isSelected = true adapter.notifyItemChanged(position) @@ -326,8 +292,8 @@ class CameraActivity : AppCompatActivity() { } } - private fun isCompletedSelect() : Boolean { - return imageSelected.size >= optionsCamera.count + private fun isCompletedSelect(): Boolean { + return imageSelected.size >= optionsCamera.count } private fun unSelectImage(imageModel: ImageModel, position: Int) { @@ -339,15 +305,20 @@ class CameraActivity : AppCompatActivity() { private fun takePhoto() { outputDirectory = getOutputDirectory() - var photoFile : File? = null + var photoFile: File? = null val outputOptions: ImageCapture.OutputFileOptions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val contentValues = ContentValues().apply { - put(MediaStore.MediaColumns.DISPLAY_NAME, SimpleDateFormat( - FILENAME, Locale.US - ).format(System.currentTimeMillis())) + put( + MediaStore.MediaColumns.DISPLAY_NAME, SimpleDateFormat( + FILENAME, Locale.US + ).format(System.currentTimeMillis()) + ) put(MediaStore.MediaColumns.MIME_TYPE, "image/$PHOTO_EXTENSION") - put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + optionsCamera.path) + put( + MediaStore.MediaColumns.RELATIVE_PATH, + Environment.DIRECTORY_PICTURES + File.separator + optionsCamera.path + ) } ImageCapture.OutputFileOptions.Builder( contentResolver, @@ -372,31 +343,28 @@ class CameraActivity : AppCompatActivity() { @SuppressLint("NotifyDataSetChanged") override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { - val path: String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - getRealPathFromUri(outputFileResults.savedUri!!) + val path: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + utils.getRealPathFromUri(outputFileResults.savedUri!!) } else { outputFileResults.savedUri?.let { mediaScanner(it) } photoFile?.path.toString() } - if (path != null) { - listSelectImages.add(path) - } - if (path != null) { - addImage(path) - } + listSelectImages.add(path) + addImage(path) } + override fun onError(exception: ImageCaptureException) { Log.d(TAG, "onError: ${exception.message}") } }) } - private fun addImage(path : String){ - if (isCompletedSelect()){ + private fun addImage(path: String) { + if (isCompletedSelect()) { hideUI(true) - }else{ + } else { val imageModel = ImageModel(image = path, isSelected = true) - imageList.add(0,imageModel) + imageList.add(0, imageModel) imageSelected.add(imageModel) adapter.notifyDataSetChanged() Log.d(TAG, "onImageSaved: $path") @@ -410,54 +378,11 @@ class CameraActivity : AppCompatActivity() { this, arrayOf(uri.toFile().absolutePath), arrayOf(mimeType) - ) {_,uri -> + ) { _, uri -> Log.d(TAG, "Imagen capturada fue guardada: $uri") } } - @Deprecated("delete function") - fun getRealPathFromUri(contentUri: Uri): String? { - - var realPath = String() - contentUri.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(contentUri).split(":")[1]) - } else { - databaseUri = contentUri - 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) - } - } - return realPath - } - - - private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED } @@ -490,25 +415,8 @@ class CameraActivity : AppCompatActivity() { return cameraProvider?.hasCamera(CameraSelector.DEFAULT_FRONT_CAMERA) ?: false } - private fun rationAuto(width: Int, height: Int): Int { - Log.d(TAG, "rationAuto: $width - $height") - val previewRatio = max(width, height).toDouble() / min(width, height) - if (abs(previewRatio - RATIO_4_3_VALUE) <= abs(previewRatio - RATIO_16_9_VALUE)) { - return AspectRatio.RATIO_4_3 - } - return AspectRatio.RATIO_16_9 - } - - private fun aspectRadio(ratio: Ratio): Int { - return when(ratio){ - Ratio.RATIO_4_3 -> AspectRatio.RATIO_4_3 - Ratio.RATIO_16_9 -> AspectRatio.RATIO_16_9 - Ratio.RATIO_AUTO -> rationAuto(getScreenWidth(),getScreenHeight()) - } - } - private fun flashModeOptions(flash: Flash) { - flashMode = when(flash){ + flashMode = when (flash) { Flash.On -> { ImageCapture.FLASH_MODE_ON } @@ -522,77 +430,29 @@ class CameraActivity : AppCompatActivity() { caseFlashMode() } - - private fun getScreenWidth(): Int { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - val windowMetrics = windowManager.currentWindowMetrics - val bounds: Rect = windowMetrics.bounds - val insets: android.graphics.Insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility( - WindowInsets.Type.systemBars() - ) - if (resources.configuration.orientation - == Configuration.ORIENTATION_LANDSCAPE - && resources.configuration.smallestScreenWidthDp < 600 - ) { // landscape and phone - val navigationBarSize = insets.right + insets.left - bounds.width() - navigationBarSize - } else { // portrait or tablet - bounds.width() - } - } else { - val outMetrics = DisplayMetrics() - windowManager.defaultDisplay.getMetrics(outMetrics) - outMetrics.widthPixels - } - } - - fun getScreenHeight(): Int { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - val windowMetrics = windowManager.currentWindowMetrics - val bounds: Rect = windowMetrics.bounds - val insets: android.graphics.Insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility( - WindowInsets.Type.systemBars() - ) - if (resources.configuration.orientation - == Configuration.ORIENTATION_LANDSCAPE - && resources.configuration.smallestScreenWidthDp < 600 - ) { // landscape and phone - bounds.height() - } else { // portrait or tablet - val navigationBarSize = insets.bottom - bounds.height() - navigationBarSize - } - } else { - val outMetrics = DisplayMetrics() - windowManager.defaultDisplay.getMetrics(outMetrics) - outMetrics.heightPixels - } - } - - private fun bindCameraUseCases() { - val cameraProvider = cameraProvider - ?: throw IllegalStateException("Error al iniciar la camara.") - val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build() - preview = Preview.Builder() - .setTargetAspectRatio(aspectRadio(optionsCamera.ratio)) - .setTargetRotation(ROTATION_0) - .build() - - imageCapture = ImageCapture.Builder() - .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) - .setTargetAspectRatio(aspectRadio(optionsCamera.ratio)) - .setTargetRotation(ROTATION_0) - .setFlashMode(flashMode) - .build() - cameraProvider.unbindAll() - - try { - cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture) - preview?.setSurfaceProvider(binding.viewFinderCamera.surfaceProvider) - } catch (exc: Exception) { - throw Exception("Use case fallo", exc) - } + val cameraProvider = cameraProvider + ?: throw IllegalStateException("Error al iniciar la camara.") + val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build() + preview = Preview.Builder() + .setTargetAspectRatio(utils.aspectRadio(optionsCamera.ratio)) + .setTargetRotation(ROTATION_0) + .build() + + imageCapture = ImageCapture.Builder() + .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) + .setTargetAspectRatio(utils.aspectRadio(optionsCamera.ratio)) + .setTargetRotation(ROTATION_0) + .setFlashMode(flashMode) + .build() + cameraProvider.unbindAll() + + try { + cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture) + preview?.setSurfaceProvider(binding.viewFinderCamera.surfaceProvider) + } catch (exc: Exception) { + throw Exception("Use case fallo", exc) + } } private fun getOutputDirectory(): File { diff --git a/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/Utils.kt b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/Utils.kt new file mode 100644 index 0000000..6243371 --- /dev/null +++ b/EasyCameraAndGallery/src/main/java/dev/ahrsoft/easycameraandgallery/Utils.kt @@ -0,0 +1,124 @@ +package dev.ahrsoft.easycameraandgallery + +import android.app.Activity +import android.content.Context +import android.content.res.Configuration +import android.graphics.Rect +import android.net.Uri +import android.os.Build +import android.provider.DocumentsContract +import android.provider.MediaStore +import android.util.DisplayMetrics +import android.util.Log +import android.view.WindowInsets +import androidx.camera.core.AspectRatio +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +class Utils(private val activity: Activity) { + fun getRealPathFromUri(contentUri: Uri): String { + var realPath = String() + contentUri.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(contentUri).split(":")[1]) + } else { + databaseUri = contentUri + selection = null + selectionArgs = null + } + try { + val column = "_data" + val projection = arrayOf(column) + val cursor = activity.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) + } + } + return realPath + } + + private fun rationAuto(width: Int, height: Int): Int { + Log.d(Constant.TAG, "rationAuto: $width - $height") + val previewRatio = max(width, height).toDouble() / min(width, height) + if (abs(previewRatio - Constant.RATIO_4_3_VALUE) <= abs(previewRatio - Constant.RATIO_16_9_VALUE)) { + return AspectRatio.RATIO_4_3 + } + return AspectRatio.RATIO_16_9 + } + + fun aspectRadio(ratio: Ratio): Int { + return when (ratio) { + Ratio.RATIO_4_3 -> AspectRatio.RATIO_4_3 + Ratio.RATIO_16_9 -> AspectRatio.RATIO_16_9 + Ratio.RATIO_AUTO -> rationAuto(getScreenWidth(), getScreenHeight()) + } + } + + fun getScreenHeight(): Int { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + val windowMetrics = activity.windowManager.currentWindowMetrics + val bounds: Rect = windowMetrics.bounds + val insets: android.graphics.Insets = + windowMetrics.windowInsets.getInsetsIgnoringVisibility( + WindowInsets.Type.systemBars() + ) + if (activity.resources.configuration.orientation + == Configuration.ORIENTATION_LANDSCAPE + && activity.resources.configuration.smallestScreenWidthDp < 600 + ) { // landscape and phone + bounds.height() + } else { // portrait or tablet + val navigationBarSize = insets.bottom + bounds.height() - navigationBarSize + } + } else { + val outMetrics = DisplayMetrics() + activity.windowManager.defaultDisplay.getMetrics(outMetrics) + outMetrics.heightPixels + } + } + + private fun getScreenWidth(): Int { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + val windowMetrics = activity.windowManager.currentWindowMetrics + val bounds: Rect = windowMetrics.bounds + val insets: android.graphics.Insets = + windowMetrics.windowInsets.getInsetsIgnoringVisibility( + WindowInsets.Type.systemBars() + ) + if (activity.resources.configuration.orientation + == Configuration.ORIENTATION_LANDSCAPE + && activity.resources.configuration.smallestScreenWidthDp < 600 + ) { // landscape and phone + val navigationBarSize = insets.right + insets.left + bounds.width() - navigationBarSize + } else { // portrait or tablet + bounds.width() + } + } else { + val outMetrics = DisplayMetrics() + activity.windowManager.defaultDisplay.getMetrics(outMetrics) + outMetrics.widthPixels + } + } + +} \ 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 3369f53..7e50e0d 100644 --- a/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt +++ b/app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt @@ -20,7 +20,7 @@ class MainActivity : AppCompatActivity() { initCall() val optionsCamera = OptionsCamera( - ratio = Ratio.RATIO_16_9, + ratio = Ratio.RATIO_AUTO, path = "Evidences", flash = Flash.Off, galleryCount = 4