Fotoapparat

Camera API in Android is hard. Having 2 different API for new and old Camera does not make things any easier. But fret not, that is your lucky day! After several years of working with Camera, we came up with Fotoapparat.

What it provides:

  • Camera API which does not allow you to shoot yourself in the foot.
  • Simple yet powerful parameters customization.
  • Standalone custom CameraView which can be integrated into any Activity.
  • Fixes and workarounds for device-specific problems.
  • Both Kotlin and Java friendly configurations.
  • Last, but not least, non 0% test coverage.

Taking picture becomes as simple as:

val fotoapparat = Fotoapparat(
    context = this,
    view = cameraView
)
 
fotoapparat.start()
    
fotoapparat
    .takePicture()
    .saveToFile(someFile)
Kotlin

How it works

Step One

Add CameraView to your layout

<io.fotoapparat.view.CameraView
    android:id="@+id/camera_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
XML

Step Two

Configure Fotoapparat instance.

Fotoapparat(
            context = this,
            view = cameraView,                   // view which will draw the camera preview
            scaleType = ScaleType.CenterCrop,    // (optional) we want the preview to fill the view
            lensPosition = back(),               // (optional) we want back camera
            cameraConfiguration = configuration, // (optional) define an advanced configuration
            logger = loggers(                    // (optional) we want to log camera events in 2 places at once
                     logcat(),                   // ... in logcat
                     fileLogger(this)            // ... and to file
            ),
            cameraErrorCallback = { error -> }   // (optional) log fatal errors
    )
Kotlin

Check the wiki for the configuration options e.g. change iso

Are you using Java only? See our wiki for the java-friendly configuration.

Step Three

Call start() and stop(). No rocket science here.

override fun onStart() {
    super.onStart()
    fotoapparat.start()
}
 
override fun onStop() {
    super.onStop()
    fotoapparat.stop()
}
Kotlin

Take a picture

Finally, we are ready to take a picture. You have various options.

val photoResult = fotoapparat.takePicture()
 
// Asynchronously saves photo to file
photoResult.saveToFile(someFile)
 
// Asynchronously converts photo to bitmap and returns the result on the main thread
photoResult
    .toBitmap()
    .whenAvailable { bitmapPhoto ->
            val imageView = (ImageView) findViewById(R.id.result)
 
            imageView.setImageBitmap(bitmapPhoto.bitmap)
            imageView.setRotation(-bitmapPhoto.rotationDegrees)
    }
    
// Of course, you can also get a photo in a blocking way. Do not do it on the main thread though.
val result = photoResult.toBitmap().await()
 
// Convert asynchronous events to RxJava 1.x/2.x types. 
// See /fotoapparat-adapters/ module 
photoResult
        .toBitmap()
        .toSingle()
        .subscribe { bitmapPhoto -> 
            
        }
Kotlin

Update parameters

It is also possible to update some parameters after Fotoapparat was already started.

fotoapparat.updateConfiguration(
        UpdateConfiguration(
                flashMode = if (isChecked) torch() else off()
                // ...
                // all the parameters available in CameraConfiguration 
        )
)
Kotlin

Or alternatively, you may provide updates on an existing full configuration.

val configuration = CameraConfiguration(
    // A full configuration
    // ...
)
 
fotoapparat.updateConfiguration(
    configuration.copy(
            flashMode = if (isChecked) torch() else off()
            // all the parameters available in CameraConfiguration 
    )
)
Kotlin

Switch cameras

In order to switch between cameras, Fotoapparat.switchTo() can be used with the new desired lensPosition and its cameraConfiguration.

fotoapparat.switchTo(
    lensPosition = front(),
    cameraConfiguration = newConfigurationForFrontCamera
)
Kotlin

Set up

Add dependency to your build.gradle

repositories {
  maven { url 'https://jitpack.io' }
}
 
implementation 'io.fotoapparat.fotoapparat:library:2.2.0'
Groovy

Camera permission will be automatically added to your AndroidManifest.xml. Do not forget to request this permission on Marshmallow and higher.

Snapshot versions

If you like to test and try the latest added features & bugfixes you can use the -SNAPSHOT version.
Gradle will download & cache the latest snapshot version, so be sure to delete the cache if you want to try a newer snapshot version than the downloaded/cached one.

implementation 'io.fotoapparat:fotoapparat:-SNAPSHOT'
Groovy

GitHub