Setting up CameraX

<uses-permission android:name="android.permission.CAMERA" />
def camerax_version = "1.0.0-alpha01"// Add the CameraX core dependency implementation 
// Add the CameraX Camera2 API interop support dependency
implementation “${camerax_version}”
  • The Camera Core library provides us with the required classes for using the CameraX library
  • The CameraX Camera2 dependency provides us with some interop features so that we can integrate CameraX with our existing Camera2 implementation


android:layout_height="match_parent" />
  • scaleType — set the scale type to be used for the captured stream. This can be one of either CENTER_CROP or CENTER_INSIDE
  • quality — set the quality to be used for the captured media. This can be either MAX, HIGH, MEDIUM or LOW
  • pinchToZoomEnabled — a boolean value representing whether or not the user can pinch-to-zoom within the camera view
  • captureMode — the capture mode to be used for the camera view. This can be either IMAGE, VIDEO or FIXED
  • lensFacing — set the lens to be used for the media capture. This can be one of either FRONT, BACK or NONE
  • flashMode — set the flash mode to be used for the camera view instance. This can be either AUTO, ON or OFF
class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) { 
  • toggleCamera() — Toggle the camera being used on the device (e.g between the front and back camera)
  • enableTorch() — Enable the torch on the device
  • setCameraByLensFacing() — Set the camera to be used via a Lens facing the given direction. This can be either LensFacing.BACK or LensFacing.FRONT.
  • hasCameraWithLensFacing() — Check whether or not the camera has a lens with the corresponding LensFacing value
  • focus() — Use the camera to focus on the given Rect instances
object : ImageCaptureUseCase.OnImageSavedListener {
override fun onImageSaved(file: File) {
// Handle image saved
override fun onError(
error: ImageCaptureUseCase.UseCaseError,
message: String,
throwable: Throwable?
) {
// Handle image error
camera_view.takePicture(object :     
ImageCaptureUseCase.OnImageCapturedListener() {
override fun onCaptureSuccess(
image: ImageProxy,
rotationDegrees: Int
) {
// Handle image captured
override fun onError(
useCaseError: ImageCaptureUseCase.UseCaseError?,
message: String?,
cause: Throwable?
) {
// Handle image capture error
object : VideoCaptureUseCase.OnVideoSavedListener {
override fun onVideoSaved(file: File?) {
// Handle video saved
override fun onError(
error: VideoCaptureUseCase.UseCaseError?,
message: String?,
throwable: Throwable?
) {
// Handle video error
override fun onDestroyView() {



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joe Birch

Joe Birch

Android @ Buffer, Google Developer Expert for Android. Passionate about mobile development and learning.