Skip to main content
Camera Kit
Android

Building your first Camera Kit App

Camera Kit brings Snap's cutting-edge augmented reality technology to your mobile, desktop and web applications. In this tutorial, you'll learn how to build an Android-based Camera Kit application, allowing you to apply a Lens to the user's camera.

Prerequisites

Section 1

Getting Started

Let's prepare our local development environment to start building our app.

Step 1

Download & unzip the project files above and open the root folder Android Starter Project/ directory in Android Studio

Next Section
Update Configuration and initialize Camera Kit
Section 2

Update Configuration and initialize Camera Kit

We'll start by adding our credentials: API Token, Lens ID, Lens Group ID. Then we'll create a CameraXImageProcessorSource to display the camera feed.

Step 1

Open AndroidManifest.xml and update your API token

This can be found in My Lenses and will be used to initialize Camera Kit.

Step 2

Open CameraViewActivity.kt and update your Lens group ID and Lens ID

These can also be found in My Lenses and will be used to fetch lenses.

Step 3

Ensure that Camera Kit is supported on this device

This is a precaution, we show a simple popup(Toast) if the device is not supported.

Step 4

Initialize the Image processor source

The CameraXImageProcessorSource is supplying the Camera Feed to the CameraKit SDK.

Step 5

Create a function to start the camera preview

This method allows us to start the camera preview from the front camera (true paramater).

Step 6

Import Classes and Initialize Permission Launcher

Import the required classes for handling a camera permission request and then register a permission request launcher that prompts the user for camera access. If granted, proceed to start the camera preview.

Step 7

Check Camera Permission and Start Preview

We must request the permission at runtime in onCreate. We use ContextCompat to check if the camera permission is granted. If granted, proceed to start the camera preview. If not, launch the permission request to prompt the user for access.

Step 8

Import Camera Kit Session and Invoke then declare the Session object

The Session object is the main entry point for Camera Kit. It's responsible for managing Lens and AR functionality in your app.

Step 9

Initialize Camera Kit Session

Set up a Session for the Camera Kit, configuring it with an image processor and attaching it to the layout view.

Step 10

Run the app

You should now see a feed with no Lens applied.

AndroidManifest.xml
CameraViewActivity.kt
CameraViewActivity.kt
CameraViewActivity.kt
CameraViewActivity.kt
CameraViewActivity.kt
CameraViewActivity.kt
CameraViewActivity.kt
CameraViewActivity.kt
Next Section
Applying a Lens
Section 3

Applying a Lens

Now that you have a feed running, this section will guide you through applying a Lens to your live video feed.

Step 1

Setup Observer and query lens

Create an observer on the lenses repository, we search for a specific lens using its ID and group ID, and recieve real-time updates based on the query results.

Step 2

Apply requested lens!

We get the first lens from the result and apply it to the lens processor for that session.

Step 3

Close the Camera Kit Session in Activity onDestroy

This function ensures that the Camera Kit session is properly closed when the activity is destroyed, freeing up resources and preventing potential memory leaks.

Step 4

Run the app

You should now see a camera feed with the Lens applied.

CameraViewActivity.kt
CameraViewActivity.kt
CameraViewActivity.kt
Loading
Was this page helpful?
Yes
No