Browse Source

ImageViewer Android swipe camera (#2995)

pull/2998/head
dima.avdeev 2 years ago committed by GitHub
parent
commit
a275db2596
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt

31
examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt

@ -8,6 +8,7 @@ import androidx.camera.core.ImageProxy
import androidx.camera.core.Preview import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView import androidx.camera.view.PreviewView
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@ -18,6 +19,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -40,6 +42,7 @@ import java.util.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
import kotlin.math.absoluteValue
private val executor = Executors.newSingleThreadExecutor() private val executor = Executors.newSingleThreadExecutor()
@ -74,16 +77,25 @@ private fun CameraWithGrantedPermission(
) { ) {
val context = LocalContext.current val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current val lifecycleOwner = LocalLifecycleOwner.current
val viewScope = rememberCoroutineScope()
val preview = Preview.Builder().build() val preview = Preview.Builder().build()
val previewView = remember { PreviewView(context) } val previewView = remember { PreviewView(context) }
val imageCapture: ImageCapture = remember { ImageCapture.Builder().build() } val imageCapture: ImageCapture = remember { ImageCapture.Builder().build() }
val cameraSelector = CameraSelector.Builder() var isFrontCamera by remember { mutableStateOf(false) }
.requireLensFacing(CameraSelector.LENS_FACING_BACK) val cameraSelector = remember(isFrontCamera) {
.build() val lensFacing =
val viewScope = rememberCoroutineScope() if (isFrontCamera) {
CameraSelector.LENS_FACING_FRONT
} else {
CameraSelector.LENS_FACING_BACK
}
CameraSelector.Builder()
.requireLensFacing(lensFacing)
.build()
}
LaunchedEffect(Unit) { LaunchedEffect(isFrontCamera) {
val cameraProvider = suspendCoroutine<ProcessCameraProvider> { continuation -> val cameraProvider = suspendCoroutine<ProcessCameraProvider> { continuation ->
ProcessCameraProvider.getInstance(context).also { cameraProvider -> ProcessCameraProvider.getInstance(context).also { cameraProvider ->
cameraProvider.addListener({ cameraProvider.addListener({
@ -102,7 +114,14 @@ private fun CameraWithGrantedPermission(
} }
val nameAndDescription = createNewPhotoNameAndDescription() val nameAndDescription = createNewPhotoNameAndDescription()
var capturePhotoStarted by remember { mutableStateOf(false) } var capturePhotoStarted by remember { mutableStateOf(false) }
Box(modifier = modifier) {
Box(modifier = modifier.pointerInput(isFrontCamera) {
detectHorizontalDragGestures { change, dragAmount ->
if (dragAmount.absoluteValue > 50.0) {
isFrontCamera = !isFrontCamera
}
}
}) {
AndroidView({ previewView }, modifier = Modifier.fillMaxSize()) AndroidView({ previewView }, modifier = Modifier.fillMaxSize())
CircularButton( CircularButton(
imageVector = IconPhotoCamera, imageVector = IconPhotoCamera,

Loading…
Cancel
Save