native_example/app/src/main/java/com/example/realsense_native_example/MainActivity.java
Go to the documentation of this file.
1 package com.example.realsense_native_example;
2 
3 import android.Manifest;
4 import android.content.pm.PackageManager;
5 import android.os.Bundle;
6 import androidx.core.app.ActivityCompat;
7 import androidx.core.content.ContextCompat;
8 import androidx.appcompat.app.AppCompatActivity;
9 import android.widget.TextView;
10 
13 
14 public class MainActivity extends AppCompatActivity {
15  private static final int MY_PERMISSIONS_REQUEST_CAMERA = 0;
16 
18 
19  // Used to load the 'native-lib' library on application startup.
20  static {
21  System.loadLibrary("native-lib");
22  }
23 
24  @Override
25  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
26  if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
27  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
28  }
29  }
30 
31  @Override
32  protected void onCreate(Bundle savedInstanceState) {
33  super.onCreate(savedInstanceState);
34  setContentView(R.layout.activity_main);
35 
36  //RsContext.init must be called once in the application's lifetime before any interaction with physical RealSense devices.
37  //For multi activities applications use the application context instead of the activity context
38  RsContext.init(getApplicationContext());
39 
40  printMessage();
41 
42  //Register to notifications regarding RealSense devices attach/detach events via the DeviceListener.
43  mRsContext = new RsContext();
44  mRsContext.setDevicesChangedCallback(new DeviceListener() {
45  @Override
46  public void onDeviceAttach() {
47  printMessage();
48  }
49 
50  @Override
51  public void onDeviceDetach() {
52  printMessage();
53  }
54  });
55 
56  // Android 9 also requires camera permissions
57  if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.O &&
58  ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
59  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
60  }
61  }
62 
63  private void printMessage(){
64  // Example of a call to native methods
65  int cameraCount = nGetCamerasCountFromJNI();
66  final String version = nGetLibrealsenseVersionFromJNI();
67  final String cameraCountString;
68  if(cameraCount == 0)
69  cameraCountString = "No cameras are currently connected.";
70  else
71  cameraCountString = "Camera is connected";
72  runOnUiThread(new Runnable() {
73  @Override
74  public void run() {
75  TextView tv = (TextView) findViewById(R.id.sample_text);
76  tv.setText("This app use librealsense: " + version + "\n" + cameraCountString);
77  }
78  });
79  }
80 
85  private static native String nGetLibrealsenseVersionFromJNI();
86  private static native int nGetCamerasCountFromJNI();
87 }
uvc_xu_option< int > super
Definition: l500-options.h:32
synchronized void setDevicesChangedCallback(DeviceListener listener)
Definition: RsContext.java:34
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
static void init(Context context)
Definition: RsContext.java:9
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:21