Skip to content

Commit

Permalink
Merge pull request #1 from AlfredoHdez1709/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AlfredoHdez1709 authored Jul 25, 2022
2 parents 62f28c2 + a40fbae commit 5a6a75d
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -52,6 +56,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
Expand All @@ -61,46 +66,174 @@ class CameraActivity : AppCompatActivity() {
private val imageList = arrayListOf<ImageModel>()
private val imageSelected = arrayListOf<ImageModel>()
private lateinit var adapter: GalleryAdapter
private lateinit var resultGallery: ActivityResultLauncher<Intent>


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
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) {
galleryCaptureButton.setOnClickListener {
getPickImageIntent()
}
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
}
getListPath(list as ArrayList<String>)
}
}
if (allPermissionsGranted()) {
startCamera()
getAllImageFromGallery()
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
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(){
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)
}
}

private fun getPathFromURI(uri: Uri) {
var realPath = String()
uri.path?.let { path ->
val databaseUri: Uri
val selection: String?
val selectionArgs: Array<String>?
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()

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))
}
Expand All @@ -109,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
}
}

Expand Down Expand Up @@ -313,6 +448,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) {
Expand Down Expand Up @@ -374,6 +524,7 @@ class CameraActivity : AppCompatActivity() {
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.setTargetAspectRatio(aspectRadio(optionsCamera.ratio))
.setTargetRotation(ROTATION_0)
.setFlashMode(flashMode)
.build()
cameraProvider.unbindAll()

Expand All @@ -393,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<String>) {
val result = Intent()
result.putExtra(IMAGE_RESULTS, list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="90"
android:startColor="@color/black"
android:endColor="@color/transparent" />
</shape>
10 changes: 10 additions & 0 deletions EasyCameraAndGallery/src/main/res/drawable/gradient_top.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:startColor="@color/transparent"
android:endColor="@color/black"
android:type="linear" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="@android:color/white"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10,20L5,20v2h5v2l3,-3 -3,-3v2zM14,20v2h5v-2h-5zM12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -1.99,0.9 -1.99,2S10.9,8 12,8zM17,0L7,0C5.9,0 5,0.9 5,2v14c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2L19,2c0,-1.1 -0.9,-2 -2,-2zM7,2h10v10.5c0,-1.67 -3.33,-2.5 -5,-2.5s-5,0.83 -5,2.5L7,2z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="@android:color/white"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10,20L5,20v2h5v2l3,-3 -3,-3v2zM14,20v2h5v-2h-5zM17,0L7,0C5.9,0 5,0.9 5,2v14c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2L19,2c0,-1.1 -0.9,-2 -2,-2zM12,6c-1.11,0 -2,-0.9 -2,-2s0.89,-2 1.99,-2 2,0.9 2,2C14,5.1 13.1,6 12,6z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="@android:color/white"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M3,2v12h3v9l7,-12L9,11l4,-9L3,2zM19,2h-2l-3.2,9h1.9l0.7,-2h3.2l0.7,2h1.9L19,2zM16.85,7.65L18,4l1.15,3.65h-2.3z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="@android:color/white"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M3.27,3L2,4.27l5,5V13h3v9l3.58,-6.14L17.73,20 19,18.73 3.27,3zM17,10h-4l4,-8H7v2.18l8.46,8.46L17,10z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="@android:color/white"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M7,2v11h3v9l7,-12h-4l4,-8z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/>
</vector>
Loading

0 comments on commit 5a6a75d

Please sign in to comment.