Install and configure
Before starting, go through the Overview and Vision Basics pages.
To set up the Vision SDK you will need to download the SDK, install the frameworks relevant to your project, and complete a few configuration steps.
SDK integration
You can integrate the Mapbox Vision SDK for Android using Gradle or download the *aar
-s of the SDK directly.
To download the SDKs, you will need a Mapbox account. Sign up for a free account.
After logging in to mapbox.com with valid credentials, you will be able to access download and installation instructions at vision.mapbox.com/install.
SDK configuration
After downloading or importing the SDK into your project, you have to configure it:
Set your Mapbox access token
Mapbox APIs require a Mapbox account and access token:
- As soon as you signed up at Mapbox, you can get an access token.
- Get an access token from the Mapbox account page.
- Open the
strings.xml
file, create a new string withname="mapbox_access_token"
, and paste the access token.
<string name="mapbox_access_token" translatable="false">YOUR_ACCESS_TOKEN_HERE</string>
- Create a new class, initialize
VisionManager
, and pass it your Mapbox access token:
public class VisionApp extends Application {@Overridepublic void onCreate() {super.onCreate();VisionManager.init(this, getResources().getString(R.string.mapbox_access_token));}}
Configure permissions
The Mapbox Vision SDK will require the following permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
These should all be granted before calling the SDK.
Add VisionView
to the activity layout (optional)
VisionView
will render the image that was produced by Vision SDK. You can add it with the following snippet:
<com.mapbox.vision.view.VisionView
android:id="@+id/vision_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:visualization_mode="detection" />
app:visualization_mode
specifies what will be rendered on VisionView
: clear video source, segmentation, or detections.
Lifecycle methods
Call lifecycle methods of VisionManager
. To listen for events that Vision SDK produces, pass an instance of VisionEventsListener
to the VisionManager
.
@Overrideprotected void onResume() {super.onResume();VisionManager.create();visionView.setVisionManager(VisionManager.INSTANCE);visionView.onResume();VisionManager.setVisionEventsListener(visionEventsListener);VisionManager.start();} @Overrideprotected void onPause() {super.onPause();visionView.onPause();VisionManager.stop();VisionManager.destroy();}
Local testing setup
Read more about setting up your development environment for testing the capabilities of the Vision SDK in the Testing and development guide.
Vehicle setup
After installing the framework, you will need to set up the device in the vehicle. Some things to consider when choosing and setting up a mount:
- Generally, shorter length mounts will vibrate less. Mounting to your windshield or to the dashboard are both options.
- Place the phone near or behind your rearview mirror. Note that your local jurisdiction may have limits on where mounts may be placed.
- Make sure the phone’s camera view is unobstructed (you will be able to tell with any of the video screens open).