Skip to content

kamyab9k/KamVision

Repository files navigation

KamVision

Medium Read my article Codacy Badge API

KamVision is a Library that provides easy access to complex Camera 2 API in jetpack compose, you can use internal elements like Preview and Frame Capturing as a one line method call in your projects. KamVisionLogo


Tech Stack

Category Details
Core Technologies Kotlin, Java, Coroutines, Flow, MVVM (Model-View-ViewModel), Jetpack Compose
Design Patterns Singleton, Builder, Facade
Preview and Image Handling Camera 2 Api, AndroidView for preview start/stop, Bitmap handling for single and multiple frames

Table of Contents


Core Logic

The ❤️ of this library is provided by Camera2 Api.


Main Characteristics

  • Start Camera Preview with only one line function call
  • Stop Camera Preview
  • Single frame capturing
  • Multiple frame capturing
  • Full compatibality with jetpack compose
  • Fluent UI
  • Optimized with coroutines

🎯 Download SampleApp.apk


Download

KamVision is available on MavenCentral to download using build tools systems. Add the following lines to your build.gradle file:

dependencies {
    implementation (Libs.)
    implementation (Libs.)
}

Usage

To enjoy KamVision in jetpack compose, create an instance using a builder pattern in simple 4 steps.

  1. First create an AndroidView and TextureView in a Box(Composable) to later pass it to the Library function parameters:
// To create textureView to pass later on
var textureView: TextureView? by remember { mutableStateOf(null) }

@Composable
fun CameraPreview() {
    val coroutineScope =
        rememberCoroutineScope()
    var textureView: TextureView? by remember { mutableStateOf(null) }

    // Use Box to ensure that the preview fills the entire screen
    Box(modifier = Modifier.fillMaxSize()) {
        AndroidView(
            factory = { ctx ->
                // Create the TextureView
                TextureView(ctx).also {
                    textureView = it
                }
            },
            modifier = Modifier.fillMaxSize(),
            update = { textureView ->
                   }
            }
        )
    }
}
  1. Create instance of the Library:
    var cameraService: CameraService? by remember { mutableStateOf(null) }

    var textureView: TextureView? by remember { mutableStateOf(null) }
...
  1. Build the Lib in the update block :
...

 update = { textureView ->
                if (cameraService == null) {
                    cameraService = CameraService.Builder(
                        context = textureView.context,
                        textureView = textureView,
                        scope = coroutineScope
                    ).build()
...
  1. finally call the Library functions:
...
                    cameraService?.startPreview()

                    cameraService?.captureFrame(20)

                    cameraService!!.getCapturedFrames { frames: List<Bitmap> ->
//                  Handle captured frames here

}
...

Library functions

There are several functions you can call with KamVision.


Function Example Usage
• startPreview() cameraService?.startPreview()
Starts the camera preview.
• stopPreview() cameraService?.stopPreview()
Stops the camera preview.
• captureFrame(Frames: Int) cameraService?.captureFrame(15)
Capture frames and the number is given in parameter .
• getCapturedFrames(frames: List) cameraService?.getCapturedFrames { frames: List ->}
Returns the list of captured frames in Bitmap format.

Input KamVision Configurations

In addition to the builder functions, KamVision receives some configurations. For example:

// shows a Preview in jetpack compose AndroidView
    Box(modifier = Modifier.fillMaxSize()) {
        AndroidView(
            factory = { ctx ->
                // Create the TextureView
                TextureView(ctx).also {
                    textureView = it
                }
            },
            modifier = Modifier.fillMaxSize(),
            update = { textureView ->
                if (cameraService == null) {
                    cameraService = CameraService.Builder(
                        context = textureView.context,
                        textureView = textureView,
                        scope = coroutineScope
                    ).build()

Read more about these two important topics to better understand the funcionality of this Library:


In Development

  • Single image capturing

License

Copyright 2024 Moahammad Khosravi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.