How to Create Custom Camera using CameraX in Android?
Last Updated : 03 Apr, 2025
CameraX is used to create a custom camera in the app. CameraX is a Jetpack support library, built to help you make camera app development easier. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using Java/Kotlin.
Important: The android.hardware.Camera package was deprecated due to its non-standard API, limited advanced features, and inconsistent support across devices. To address these issues, Android introduced the android.hardware.camera2 API, which offers a more flexible, standardized, and feature-rich approach to camera control. This ensures better compatibility across devices and access to advanced camera capabilities. Developers should use camera2 for improved performance and consistency.
Step by Step Implementation
Step 1: Create a new project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Adding dependencies for Camera
dependencies {
...
implementation ("androidx.camera:camera-core:1.3.0")
implementation ("androidx.camera:camera-camera2:1.3.0")
implementation ("androidx.camera:camera-lifecycle:1.3.0")
implementation ("androidx.camera:camera-view:1.3.0")
}
Step 3: Add Camera permission and File provider
Go to app > manifests > AndroidManifest.xml and add the following permissions for camera and storage.
AndroidManifest.xml:
XML <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" tools:ignore="ScopedStorage" />
Now, under the <application> tag add the following code for provide
AndroidManifest.xml:
XML <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider>
Step 4: Define file path in file_paths.xml
Navigate to app > res > xml right click on the folder and choose New > XML Resource File. Then, set the file name as “file_paths.xml” and the root element and “paths”. Now, add the following code to the file
file_paths.xml:
XML <?xml version="1.0" encoding="utf-8"?> <paths> <external-files-path name="images" path="Pictures/" /> </paths>
Step 5: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file. Notice that there is a view called androidx.camera.view.PreviewView with id previewView which we will use as Viewfinder for the camera.
activity_main.xml:
XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity" android:padding="16dp"> <androidx.camera.view.PreviewView android:id="@+id/previewView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <Button android:id="@+id/captureButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Capture Image" android:layout_marginTop="16dp"/> </LinearLayout>
Step 6: Working with the MainActivity file
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
MainActivity.java package org.geeksforgeeks.demo; import android.Manifest; import android.content.ContentValues; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.widget.Button; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.camera.core.*; import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.camera.view.PreviewView; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MainActivity extends AppCompatActivity { private PreviewView previewView; private Button captureButton; private ImageCapture imageCapture = null; private ExecutorService cameraExecutor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); previewView = findViewById(R.id.previewView); captureButton = findViewById(R.id.captureButton); if (allPermissionsGranted()) { startCamera(); } else { ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS); } captureButton.setOnClickListener(v -> captureImage()); // Initialize camera executor cameraExecutor = Executors.newSingleThreadExecutor(); } private void startCamera() { ProcessCameraProvider cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(() -> { try { ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); Preview preview = new Preview.Builder().build(); preview.setSurfaceProvider(previewView.getSurfaceProvider()); imageCapture = new ImageCapture.Builder().build(); CameraSelector cameraSelector = new CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build(); cameraProvider.unbindAll(); cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture); } catch (Exception e) { Toast.makeText(MainActivity.this, "Failed to start camera", Toast.LENGTH_SHORT).show(); } }, ContextCompat.getMainExecutor(this)); } private void captureImage() { if (imageCapture == null) return; String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date()); ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "IMG_" + timestamp + ".jpg"); contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "Pictures/CameraX-Images"); } ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder( getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues ).build(); imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() { @Override public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { Toast.makeText(MainActivity.this, "Image saved to gallery!", Toast.LENGTH_SHORT).show(); } @Override public void onError(@NonNull ImageCaptureException exception) { Toast.makeText(MainActivity.this, "Failed to save image", Toast.LENGTH_SHORT).show(); } }); } private boolean allPermissionsGranted() { for (String permission : REQUIRED_PERMISSIONS) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CODE_PERMISSIONS) { if (allPermissionsGranted()) { startCamera(); } else { Toast.makeText(this, "Permissions not granted", Toast.LENGTH_SHORT).show(); finish(); } } } @Override protected void onDestroy() { super.onDestroy(); cameraExecutor.shutdown(); } private static final int REQUEST_CODE_PERMISSIONS = 10; private static final String[] REQUIRED_PERMISSIONS = new String[]{ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }; }
MainActivity.kt package org.geeksforgeeks.demo import android.Manifest import android.content.ContentValues import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.provider.MediaStore import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.camera.core.* import androidx.camera.lifecycle.ProcessCameraProvider import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.camera.view.PreviewView import java.text.SimpleDateFormat import java.util.* class MainActivity : AppCompatActivity() { // UI Components private lateinit var previewView: PreviewView // Displays the camera preview private lateinit var captureButton: Button // Button to capture image // CameraX Image Capture Use Case private var imageCapture: ImageCapture? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize UI elements previewView = findViewById(R.id.previewView) captureButton = findViewById(R.id.captureButton) // Check and request permissions before starting the camera if (allPermissionsGranted()) { startCamera() } else { ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS) } // Capture image on button click captureButton.setOnClickListener { captureImage() } } // Initializes and starts the CameraX preview private fun startCamera() { val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get() // CameraX Preview Use Case val preview = Preview.Builder() .build() .also { it.setSurfaceProvider(previewView.surfaceProvider) } // CameraX Image Capture Use Case imageCapture = ImageCapture.Builder().build() val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA // Select back camera try { // Unbind previous use cases and bind new ones cameraProvider.unbindAll() cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture) } catch (e: Exception) { Toast.makeText(this, "Failed to start camera", Toast.LENGTH_SHORT).show() } }, ContextCompat.getMainExecutor(this)) } // Captures an image and saves it to the gallery private fun captureImage() { val imageCapture = imageCapture ?: return // Create a unique filename using timestamp val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date()) val contentValues = ContentValues().apply { put(MediaStore.MediaColumns.DISPLAY_NAME, "IMG_$timestamp.jpg") put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { put(MediaStore.MediaColumns.RELATIVE_PATH, "Pictures/CameraX-Images") // Save to gallery } } // Configure output file options val outputOptions = ImageCapture.OutputFileOptions.Builder( contentResolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues ).build() // Capture and save the image imageCapture.takePicture( outputOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback { override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { Toast.makeText(this@MainActivity, "Image saved to gallery!", Toast.LENGTH_SHORT).show() } override fun onError(exception: ImageCaptureException) { Toast.makeText(this@MainActivity, "Failed to save image", Toast.LENGTH_SHORT).show() } } ) } // Checks if all required permissions are granted private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED } // Handles the result of permission requests override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == REQUEST_CODE_PERMISSIONS) { if (allPermissionsGranted()) { startCamera() } else { Toast.makeText(this, "Permissions not granted", Toast.LENGTH_SHORT).show() finish() } } } companion object { private const val REQUEST_CODE_PERMISSIONS = 10 private val REQUIRED_PERMISSIONS = arrayOf( Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE // Required for saving images ) } }
Output:
Similar Reads
How to Create Bitmap From View in Android?
In Android, a Bitmap is a representation of an image that consists of pixels with a specified width, height, and color format. A Bitmap from a View is a Bitmap that is created from a View in your Android application. The process of creating a Bitmap from a View involves drawing the View on a Canvas
4 min read
Android - Detect Open or Closed Eyes Using ML Kit and CameraX
Google's ML kit is one of the best-trained models for face detection and its characteristics. Integrating with your own CameraX library can be quite a challenging task. so we are going to build an Android app that will detect whether a person's eyes are open or closed in real time. This process goin
10 min read
How to Implement Custom Calendar in Android?
Nowadays in most of the apps, we see Calendar in most of the apps for displaying birth dates or for any appointment application. Displaying the date in the app with the help of the Calendar view gives a better user experience. In this article, we are going to make a custom calendar for our android a
4 min read
Custom Chips using Jetpack Compose in Android
Chips in android are one of the components which are used to make the choice filters, actions, and display the selectable options in the compact area of the Android Window. In this article, we will use Android's Jetpack Compose to create those chips. A sample image is given below to give an idea of
5 min read
How to Use Canvas API in Android Apps?
Canvas API is also one of the most used in Android. The name of the API itself tells us that the API is being used for drawing on the drawing board. With the help of this API, we can draw different types of shapes and create custom UI components that are not present in Android. In this article, we w
5 min read
How to Generate Barcode in Android?
Barcodes are the graphical representation of data that enables effortless scanning. When we aim to incorporate this functionality for various purposes in Android applications it becomes crucial to understand the underlying principles and techniques involved in generating barcodes effectively. In thi
3 min read
How to Create Contacts App in Android Studio?
Contacts app in android device is a system app that comes installed on your android device. Different devices have different UI for the contacts app. In this article, we will take a look at how we can build our own contacts app in Android Studio. What we are going to build in this article? We will b
15+ min read
How to Create Star Animation in Android?
In this article, we are going to create star animation using an animated star library. Here we will be creating a background drawable file and will specify the color for the animation. The star animation we have created is easy to create since we are using a library. A sample GIF is given below to g
3 min read
Generate QR Code in Android using Kotlin
Many applications nowadays use QR codes within their application to hide some information. QR codes are seen within many payment applications which are used by the users to scan and make payments. The QR codes which are seen within these applications are generated inside the application itself. In t
4 min read
How to Build a QR Code Android App using Firebase?
QR (Quick Response) code is a type of two-dimensional barcode that contains information encoded in a pattern of black and white squares. It was first developed in 1994 by a company named Denso Wave. Qr codes can be scanned by a smartphone or a dedicated QR code scanner, which uses the device's camer
6 min read