Skip to content

raipankaj/JetCam

Repository files navigation

JetCam

Add camera capability to your app with just a single method - JetCam

To get started with JetCam just add the maven url and the Chip dependency

build.gradle (Project level)

allprojects {
    repositories {
    ...
    //Add this url
    maven { url 'https://jitpack.io' }
    }
}

If you are using Android Studio Arctic Fox and above where you don't have allProjects in build.gradle then add following maven url in settings.gradle like below

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        //Add this url
        maven { url 'https://jitpack.io' }
        jcenter() // Warning: this repository is going to shut down soon
    }
}

Once you have added the maven url now add the Chip dependency in the build.gradle (module level)

implementation 'com.github.raipankaj:JetCam:0.1.3'

Congratulations, you have successfully added the dependency. Now to get started with JetCam add the following code snippet

JetCam(
      imageAnalysis = { 
                            val buffer = it.planes[0].buffer
                            val data = buffer.toByteArray()
                            val pixels = data.map { it.toInt() and 0xFF }
                            val luma = pixels.average()

                            Log.i("LUM", ":${luma}")
                    }
)

JetCam composable provides various option to perform like 1. Toggle on/off flash light 2. Switch between front and back camera 3. Set back or front camera as default 4. Get image analysis lambda without creating class 5. Set image analysis object explicitly
@Composable
fun JetCam(
    imageAnalysis: ((ImageProxy?) -> Unit)? = null,
    imageAnalysisConfig: ImageAnalysis? = null,
    imageCapture: ImageCapture? = null,
    defaultCameraLens: Lens = Lens.Back,
    cameraLensToggle: ((CameraLens) -> Unit)? = null,
    cameraProcess: ((Camera) -> Unit)? = null
)

Following is a full fledged working sample code

Surface(color = MaterialTheme.colors.background) {

                    val imageCapture = rememberImageCapture()

                    var toggleCameraFlash by remember {
                        mutableStateOf(true)
                    }

                    var isBackCamShown by remember {
                        mutableStateOf(true)
                    }

                    var camera by remember {
                        mutableStateOf<Camera?>(null)
                    }

                    var cameraLensToggle by remember {
                        mutableStateOf<CameraLens?>(null)
                    }

                    Box(modifier = Modifier.fillMaxSize(),
                        contentAlignment = Alignment.BottomCenter) {

                        JetCam(
                            imageAnalysis = {
                                it?.let {
                                    val buffer = it.planes[0].buffer
                                    val data = buffer.toByteArray()
                                    val pixels = data.map { it.toInt() and 0xFF }
                                    val luma = pixels.average()

                                    Log.i("LUM", ":${luma}")
                                }
                            },
                            imageCapture = imageCapture,
                            cameraLensToggle = { cameraLens ->
                                cameraLensToggle = cameraLens
                            },
                            cameraProcess = { cam ->
                                camera = cam
                            }
                        )

                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(16.dp),
                            horizontalArrangement = Arrangement.SpaceEvenly) {
                            IconButton(onClick = {
                                toggleCameraFlash = toggleCameraFlash.not()
                                if (toggleCameraFlash) {
                                    camera?.flashOff()
                                } else {
                                    camera?.flashOn()
                                }
                            }, modifier = Modifier
                                .size(56.dp)
                                .clip(CircleShape)
                                .background(Color.LightGray)) {
                                Icon(
                                    painter = painterResource(
                                        id = if (toggleCameraFlash) R.drawable.ic_flash_off else R.drawable.ic_flash_on
                                    ),
                                    contentDescription = "Flash On/Off"
                                )
                            }

                            IconButton(onClick = {
                                isBackCamShown = isBackCamShown.not()
                                if (isBackCamShown) {
                                    cameraLensToggle?.toggleToBack()
                                } else {
                                    toggleCameraFlash = true
                                    camera?.flashOff()
                                    cameraLensToggle?.toggleToFront()
                                }
                            }, modifier = Modifier
                                .size(56.dp)
                                .clip(CircleShape)
                                .background(Color.LightGray)) {
                                Icon(
                                    painter = painterResource(id = R.drawable.ic_toggle),
                                    contentDescription = "Toggle Front/Back Cam"
                                )
                            }
                        }
                    }
                }

Also do not forget to request for camera permission. Note: If you like this library, then please hit the star button! 😃

About

Add camera capability to app built with Jetpack Compose

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages