This Module has been designed to simplify the usage of CameraX, making it extremely easy for users to work with. By providing minimal initialization on the user's part, this module facilitates straightforward integration of CameraX functionality into Android applications.
With this interface, users can effortlessly manage various camera-related tasks with minimal setup, ensuring a seamless and hassle-free experience.
If you're using Groovy DSL,
//settings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
//app.gradle
dependencies {
implementation 'com.github.lyh990517:Simple_CameraX_Library:latest-release'
}
If you're using Kotlin DSL
//settings.gradle.kts
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven(url = "https://jitpack.io")
}
}
//app.gradle.kts
dependencies {
implementation ("com.github.lyh990517:Simple_CameraX_Library:latest-release")
}
-
initialize(context: Context)
: Initializes the CameraX module with the given application context. -
startCamera(lifecycleOwner: LifecycleOwner)
: Starts the camera with the providedLifecycleOwner
to manage the camera's lifecycle. -
takePicture(showMessage: (String) -> Unit)
: Captures a still image and triggers the providedshowMessage
callback with the image result. -
startRecordVideo()
: Starts recording a video. -
stopRecordVideo()
: Stops the video recording. -
resumeRecordVideo()
: Resumes video recording if it was paused. -
pauseRecordVideo()
: Pauses video recording. -
closeRecordVideo()
: Closes the video recording session. -
flipCameraFacing()
: Toggles between the front and back camera facing. -
turnOnOffFlash()
: Toggles the camera flash on or off. -
unBindCamera()
: Unbinds and releases the camera resources. -
getPreviewView(): PreviewView
: Returns thePreviewView
associated with the camera for displaying the camera preview. -
getFlashState(): StateFlow<Boolean>
: Provides aStateFlow
that emits the current state of the camera flash (on or off). -
getFacingState(): StateFlow<Int>
: Provides aStateFlow
that emits the current camera facing (front or back). -
getRecordingState(): StateFlow<RecordingState>
: Provides aStateFlow
that emits the current video recording state (recording, paused, or stopped). -
getRecordingInfo(): SharedFlow<RecordingInfo>
: Provides aSharedFlow
that emits information about the video recording, such as duration, file path, and other relevant details.
This CameraX Interface simplifies camera integration in your Android app, making it easier to manage and control camera functionality.
Here's an example of how to use CameraX
@Composable
fun Example() {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
val cameraScope = rememberCoroutineScope()
val cameraX = remember { CameraXFactory.create() }
val previewView = remember { mutableStateOf<PreviewView?>(null) }
val facing = cameraX.getFacingState().collectAsState()
LaunchedEffect(Unit) {
cameraX.initialize(context = context)
previewView.value = cameraX.getPreviewView()
}
DisposableEffect(facing.value) {
cameraScope.launch(Dispatchers.Main) {
cameraX.startCamera(lifecycleOwner = lifecycleOwner)
}
onDispose {
cameraX.unBindCamera()
}
}
Button(
onClick = { cameraX.takePicture {} }
) {
Text("takePicture")
}
}
Feel free to customize CameraX Interface to suit your specific needs. Whether it's adjusting camera settings, tweaking the user interface, or tailoring the behavior, you have the flexibility to make it your own.
If you find CameraX Interface helpful and it makes your development journey smoother, we kindly ask for your support. Please consider ⭐starring⭐ this repository—it motivates us to keep improving and providing you with valuable tools.