Skip to content

Latest commit

 

History

History
145 lines (128 loc) · 4.99 KB

File metadata and controls

145 lines (128 loc) · 4.99 KB

Native RealSense Application for Android

This sample demonstrates how to configure an Android application to add a RealSense dependency.
The sample also demonstrates basic streaming using the Pipeline class and how to respond to connection events of a RealSense device.

Read about Android support here.

Project Configuration

Before jumping to the code, let's review the required modifications both for the module build.gradle file and to the app's manifest.

Module Build Gradle

The app should add RealSense dependency in the module build.gradle which is currently available in a private repo.

repositories {
   maven{
       url "https://dl.bintray.com/intel-realsense/librealsense"
   }
}

configurations {
   downloadHeader
   downloadSo
}

dependencies {
   def version = '2.+'
   downloadSo 'com.intel.realsense:librealsense:' + version + '@aar'
   implementation 'com.intel.realsense:librealsense:' + version + '@aar'
   downloadHeader 'com.intel.realsense:librealsense:' + version + '@zip'
}

task extractHeaders(type: Sync) {
   dependsOn configurations.downloadHeader
   from { configurations.downloadHeader.collect { zipTree(it) } }
   into "$projectDir/src/main/cpp/include"
}

task extractSo(type: Sync) { dependsOn configurations.downloadSo from { configurations.downloadSo.collect { zipTree(it) } } include("jni/**") into "$buildDir/" }

preBuild.dependsOn(extractHeaders) preBuild.dependsOn(extractSo)

The example shows how to use any librealsense version which is greater than 2.0.0, it is recommended to specify a specific version for example:

   def version = '2.18.0'
   downloadSo 'com.intel.realsense:librealsense:' + version + '@aar'
   implementation 'com.intel.realsense:librealsense:' + version + '@aar'
   downloadHeader 'com.intel.realsense:librealsense:' + version + '@zip'

Once the gradle changes are done and the gradle sync was executed, you should be able to import librealsense classes. It might be required to invalidate caches in case your app is failing to import librealsense (File->Invalidate Caches->Invalidate and restart)

CMakeLists

Your native app need to link with 'librealsense.so' in order to do that you will need to edit the app's CMakeLists.txt

Add RealSense Library

   include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/include")
   add_library(realsense2 SHARED IMPORTED)
   set_target_properties(
       realsense2
       PROPERTIES
       IMPORTED_LOCATION
       ${CMAKE_CURRENT_SOURCE_DIR}/build/jni/${ANDROID_ABI}/librealsense2.so
   )

Link RealSense Library

   target_link_libraries(
       native-lib
       ${log-lib}
       realsense2
   )

Application Manifest

A modifications is required in the application manifest file.

Camera Permissions

This permission is required by Androis >= 9, for apps that target lower Android version this is not required.

   <uses-permission android:name="android.permission.CAMERA"/>

Example Code

Java Code

Let's look at the only Java source code file in this example, the MainActivity.

On Create

RsContext.init must be called once in the application's lifetime before any interaction with physical RealSense devices. For multi activities applications use the application context instead of the activity context.

   RsContext.init(getApplicationContext());

Register to notifications regarding RealSense devices attach/detach events via the DeviceListener. In this example we read RealSense version through the native API and print it to the screen once a device is detected.

   mRsContext = new RsContext();
   mRsContext.setDevicesChangedCallback(new DeviceListener() {
       @Override
       public void onDeviceAttach() {
           printMessage();
       }

      @Override
      public void onDeviceDetach() {
          printMessage();
      }
   });

Accessing The Native API

We read RealSense version and query the number of connected cameras using the native (C++) API via JNI:

   int cameraCount = nGetCamerasCountFromJNI();
   String version = nGetLibrealsenseVersionFromJNI();

Native (C++) Code

The native code of our example is located in native-lib.cpp.

Reading RealSense Version

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_realsense_1native_1example_MainActivity_nGetLibrealsenseVersionFromJNI(JNIEnv *env, jclass type) {
   return (*env).NewStringUTF(RS2_API_VERSION_STR);
}

Query The Number Of Connected Devices

extern "C" JNIEXPORT jint JNICALL
Java_com_example_realsense_1native_1example_MainActivity_nGetCamerasCountFromJNI(JNIEnv *env, jclass type) {
   rs2::context ctx;
   return ctx.query_devices().size();;
}