JetCam - Add camera capability to app built with Jetpack Compose

Overview

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.1'

def camerax_version = "1.0.2"
// CameraX core library using camera2 implementation
implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle Library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha32"

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

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}")
                        }
                    }
)

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! 😃
You might also like...
JetInstagram is an Instagram UI clone app built with Jetpack Compose.
JetInstagram is an Instagram UI clone app built with Jetpack Compose.

JetInstagram JetInstagram is an instagram clone app built with Jetpack Compose. This sample showcases: Home Screen Post Like Button Animation Photo Li

InstagramJetpackCompose is an Instagram UI clone app built with Jetpack Compose.
InstagramJetpackCompose is an Instagram UI clone app built with Jetpack Compose.

InstagramJetpackCompose is an Instagram UI clone app built with Jetpack Compose.

From Swedish
From Swedish "Öppettider", an app to quickly access your favorite places' opening times. Built to practice Android development and try out Jetpack Compose.

Appettider From Swedish "Öppettider", an app to quickly access your favorite places' opening times. Built to practice Android development and try out

A little app that was built with Jetpack Compose
A little app that was built with Jetpack Compose

RateIt Compose 📜 Description A little app that was built with Jetpack Compose. It contains only one screen with custom animated content. The goal of

A pet adoption app UI built with super powerful Jetpack Compose for #AndroidDevChallenge
A pet adoption app UI built with super powerful Jetpack Compose for #AndroidDevChallenge

PetyKT 🐶 Awesome pet adoption app built to demonstrate the use of super powerful and modern UI development toolkit i.e. Jetpack Compose UI for #Andro

An Android / Wear OS app built with Jetpack Compose to search for definitions of English words
An Android / Wear OS app built with Jetpack Compose to search for definitions of English words

EnglishDictionary A simple Android / Wear OS app built with Jetpack Compose to s

Clouddy - Weather App Built with Jetpack Compose
Clouddy - Weather App Built with Jetpack Compose

Clouddy - Weather App Daily Weather Forecast App built with Jetpack Compose View

An android library to highlight different features of the app built using Jetpack Compose.
An android library to highlight different features of the app built using Jetpack Compose.

An android library to highlight different features of the app built using Jetpack Compose.

This is a Game score tracker app built with Jetpack Compose.
This is a Game score tracker app built with Jetpack Compose.

This is a Game score tracker app built with Jetpack Compose. The purpose of this repository is to demonstrate below: Implementing entire UI elements w

Releases(0.1.3)
Owner
Pankaj Rai
I am an android developer who likes to learn new technologies and bring ideas into reality for the betterment of a large community.
Pankaj Rai
Add chips to your apps built with Jetpack Compose!

Chip Add chips to your apps built with Jetpack Compose! To get started with Chip just add the maven url and the Chip dependency build.gradle (Project

Pankaj Rai 6 Sep 27, 2022
Camera is a library for Android built using the Camera2 API.

Camera Camera is a library for Android built using the Camera2 API. The Camera2 API allows users to capture RAW images, i.e. unprocessed pixel data di

binishmatheww 4 Dec 15, 2022
This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Compose.

JetBMICalculator This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Co

BHAVNA THACKER 3 Dec 31, 2022
Jetpack-compose-uis - A collection of some UIs using Jetpack Compose. built using Katalog

Jetpack Compose UIs This is a collection of some UIs using Jetpack Compose. It i

Mori Atsushi 3 Dec 15, 2022
📱 WhatsApp clone project demonstrates modern Android development built with Jetpack Compose and Stream Chat SDK for Compose.

This is a WhatsApp clone app built with Jetpack Compose and Stream Chat SDK for Compose. The purpose of this repository is to demonstrate below: Imple

Stream 689 Dec 25, 2022
This is a sample app(For beginners - App #1) built using Jetpack Compose

This is a sample app(For beginners - App #1) built using Jetpack Compose. It is a simple - single screen app to demonstrate use of basic Jetpack Compose UI elements like Text, Image and Button & LazyColumn (Vertical Recyclerview). It also demonstrates how compose manages state with a Boolean State.

BHAVNA THACKER 3 Apr 3, 2022
ToDo-Task-App - An Android App built to demonstrate the use of Jetpack Compose, Material You, Room Database, and MVVM

ToDo Task App ToDo Task App is a sample Android app built with Jetpack Compose.

null 0 Jan 17, 2022
JetFlix - A clone of Android NetFlix app in Android built using Jetpack compose.

JetFlix A clone of Android NetFlix app in Android built using Jetpack compose. This sample app showcases the following: MVVM Architecture (ViewModel +

Pushpal Roy 40 Dec 6, 2022
A Rick and Morty app built with Jetpack Compose.

RickAndMortyCompose A Rick and Morty app built with Jetpack Compose. ?? Demo ✨ Features Compose UI Kotlin Coroutines Compose Navigation MVVM Architect

Prattham Arora 4 Mar 12, 2022
This app can be used to track open slots for vaccination. This is built using Jetpack Compose.

Covid Vaccine This projects used the Cowin APIs provided by the government. Screenshots Tech Stack Used Kotlin Dagger Hilt Clean Architecture with Mod

Himanshu Singh 101 Dec 15, 2022