-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add camera permissions and taking photo
- Loading branch information
1 parent
af6da56
commit 150ec88
Showing
6 changed files
with
240 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+6 KB
app/src/main/java/edu/card/clarity/presentation/recordReceiptScreen/.DS_Store
Binary file not shown.
107 changes: 107 additions & 0 deletions
107
app/src/main/java/edu/card/clarity/presentation/recordReceiptScreen/CameraCapture.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package edu.card.clarity.presentation.recordReceiptScreen | ||
|
||
import android.content.Context | ||
import android.os.Environment | ||
import androidx.camera.core.* | ||
import androidx.camera.lifecycle.ProcessCameraProvider | ||
import androidx.camera.view.PreviewView | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.core.content.ContextCompat | ||
import java.io.File | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Text | ||
import androidx.compose.ui.unit.dp | ||
import java.util.concurrent.Executor | ||
|
||
@Composable | ||
fun CameraCapture(onImageCaptured: (String) -> Unit, onError: (ImageCaptureException) -> Unit) { | ||
val context = LocalContext.current | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
val cameraProviderFuture = remember { ProcessCameraProvider.getInstance(context) } | ||
val imageCapture = remember { ImageCapture.Builder().build() } | ||
val executor = ContextCompat.getMainExecutor(context) // Define executor here | ||
|
||
Box(modifier = Modifier | ||
.fillMaxSize() | ||
.padding(bottom = 50.dp)) { | ||
AndroidView( | ||
factory = { ctx -> | ||
val previewView = PreviewView(ctx) | ||
|
||
cameraProviderFuture.addListener({ | ||
val cameraProvider = cameraProviderFuture.get() | ||
val preview = Preview.Builder().build().also { | ||
it.setSurfaceProvider(previewView.surfaceProvider) | ||
} | ||
|
||
try { | ||
cameraProvider.unbindAll() | ||
cameraProvider.bindToLifecycle( | ||
lifecycleOwner, | ||
CameraSelector.DEFAULT_BACK_CAMERA, | ||
preview, | ||
imageCapture | ||
) | ||
} catch (exc: Exception) { | ||
onError(exc as ImageCaptureException) | ||
} | ||
}, executor) | ||
|
||
previewView | ||
}, | ||
modifier = Modifier.fillMaxSize() | ||
) | ||
|
||
Button( | ||
onClick = { | ||
takePhoto(context, imageCapture, executor, onImageCaptured, onError) | ||
}, | ||
modifier = Modifier.align(Alignment.BottomCenter) | ||
) { | ||
Text("Take Picture") | ||
} | ||
} | ||
} | ||
|
||
fun takePhoto( | ||
context: Context, | ||
imageCapture: ImageCapture, | ||
executor: Executor, | ||
onImageCaptured: (String) -> Unit, | ||
onError: (ImageCaptureException) -> Unit | ||
) { | ||
val photoFile = createFile(context) | ||
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build() | ||
|
||
imageCapture.takePicture( | ||
outputOptions, | ||
executor, | ||
object : ImageCapture.OnImageSavedCallback { | ||
override fun onImageSaved(output: ImageCapture.OutputFileResults) { | ||
onImageCaptured(photoFile.absolutePath) | ||
} | ||
|
||
override fun onError(exc: ImageCaptureException) { | ||
onError(exc) | ||
} | ||
} | ||
) | ||
} | ||
|
||
fun createFile(context: Context): File { | ||
val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) | ||
return File(storageDir, SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS", Locale.US) | ||
.format(System.currentTimeMillis()) + ".jpg") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters