java_example/app/src/main/java/com/example/realsense_java_example/MainActivity.java
Go to the documentation of this file.
1 package com.example.realsense_java_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 
20 
21 import java.text.DecimalFormat;
22 
23 public class MainActivity extends AppCompatActivity {
24  private static final int MY_PERMISSIONS_REQUEST_CAMERA = 0;
25 
27 
28  @Override
29  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
30  if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
31  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
32  }
33  }
34 
35  @Override
36  protected void onResume() {
37  super.onResume();
38 
39  if(!mStreamingThread.isAlive())
40  mStreamingThread.start();
41  }
42 
43  @Override
44  protected void onPause() {
45  super.onPause();
46  mStreamingThread.interrupt();
47  }
48 
49  @Override
50  protected void onCreate(Bundle savedInstanceState) {
51  super.onCreate(savedInstanceState);
52  setContentView(R.layout.activity_main);
53 
54  //RsContext.init must be called once in the application's lifetime before any interaction with physical RealSense devices.
55  //For multi activities applications use the application context instead of the activity context
56  RsContext.init(getApplicationContext());
57 
58  //Register to notifications regarding RealSense devices attach/detach events via the DeviceListener.
59  mRsContext = new RsContext();
60  mRsContext.setDevicesChangedCallback(new DeviceListener() {
61  @Override
62  public void onDeviceAttach() {
63  mStreamingThread.start();
64  }
65 
66  @Override
67  public void onDeviceDetach() {
68  mStreamingThread.interrupt();
69  }
70  });
71 
72  // Android 9 also requires camera permissions
73  if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.O &&
74  ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
75  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
76  }
77  }
78 
79  private Thread mStreamingThread = new Thread(new Runnable() {
80  @Override
81  public void run() {
82  try {
83  stream();
84  } catch (Exception e) {
85  e.printStackTrace();
86  }
87  }
88  });
89 
90  //Start streaming and print the distance of the center pixel in the depth frame.
91  private void stream() throws Exception {
92  Pipeline pipe = new Pipeline();
93  // try statement needed here to release resources allocated by the Pipeline:start() method
94  try(PipelineProfile pp = pipe.start()){}
95  final DecimalFormat df = new DecimalFormat("#.##");
96 
97  while (!mStreamingThread.isInterrupted())
98  {
99  try (FrameSet frames = pipe.waitForFrames()) {
100  try (Frame f = frames.first(StreamType.DEPTH))
101  {
103  final float deptValue = depth.getDistance(depth.getWidth()/2, depth.getHeight()/2);
104  runOnUiThread(new Runnable() {
105  @Override
106  public void run() {
107  TextView textView = findViewById(R.id.distanceTextView);
108  textView.setText("Distance: " + df.format(deptValue));
109  }
110  });
111  }
112  }
113  }
114  pipe.stop();
115  }
116 }
uvc_xu_option< int > super
Definition: l500-options.h:32
synchronized void setDevicesChangedCallback(DeviceListener listener)
Definition: RsContext.java:34
GLint GLint GLsizei GLsizei GLsizei depth
e
Definition: rmse.py:177
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
static void init(Context context)
Definition: RsContext.java:9
GLdouble f
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
Definition: threads.c:40


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