multicam/src/main/java/com/intel/realsense/multicam/MainActivity.java
Go to the documentation of this file.
1 package com.intel.realsense.multicam;
2 
3 import android.Manifest;
4 import android.content.Context;
5 import android.content.pm.PackageManager;
6 import android.os.Bundle;
7 import android.os.Handler;
8 import androidx.core.app.ActivityCompat;
9 import androidx.core.content.ContextCompat;
10 import androidx.appcompat.app.AppCompatActivity;
11 import android.util.Log;
12 import android.view.View;
13 
25 
26 import java.util.ArrayList;
27 
28 public class MainActivity extends AppCompatActivity {
29  private static final String TAG = "librs multicam example";
30  private static final int PERMISSIONS_REQUEST_CAMERA = 0;
31 
32  private boolean mPermissionsGranted = false;
33 
34  private Context mAppContext;
36  private final Handler mHandler = new Handler();
37 
39  private ArrayList<Pipeline> mPipelines;
40  private ArrayList<Colorizer> mColorizers;
42 
43  @Override
44  protected void onCreate(Bundle savedInstanceState) {
45  super.onCreate(savedInstanceState);
46  setContentView(R.layout.activity_main);
47 
48  mAppContext = getApplicationContext();
49  mGLSurfaceView = findViewById(R.id.glSurfaceView);
50  mGLSurfaceView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
51  | View.SYSTEM_UI_FLAG_FULLSCREEN
52  | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
53  | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
54  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
55  | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
56 
57  // Android 9 also requires camera permissions
58  if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.O &&
59  ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
60  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
61  return;
62  }
63 
64  mPermissionsGranted = true;
65  }
66 
67  @Override
68  protected void onDestroy() {
69  super.onDestroy();
70  mGLSurfaceView.close();
71  }
72 
73  @Override
74  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
75  if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
76  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
77  return;
78  }
79  mPermissionsGranted = true;
80  }
81 
82  @Override
83  protected void onResume() {
84  super.onResume();
85  if(mPermissionsGranted)
86  init();
87  else
88  Log.e(TAG, "missing permissions");
89  }
90 
91  @Override
92  protected void onPause() {
93  super.onPause();
94  //Release Context
95  if(mRsContext != null)
96  mRsContext.close();
97  stop();
98  }
99 
100  private void init(){
101  //RsContext.init must be called once in the application lifetime before any interaction with physical RealSense devices.
102  //For multi activities applications use the application context instead of the activity context
103  RsContext.init(mAppContext);
104 
105  //Register to notifications regarding RealSense devices attach/detach events via the DeviceListener.
106  mRsContext = new RsContext();
108  mPipelines = new ArrayList<>();
109  mColorizers = new ArrayList<>();
110 
111  start();
112  }
113 
115  @Override
116  public void onDeviceAttach() {
117  restart();
118  }
119 
120  @Override
121  public void onDeviceDetach() {
122  restart();
123  }
124  };
125 
126  Runnable mStreaming = new Runnable() {
127  @Override
128  public void run() {
129  try {
130  for(int i = 0; i < mPipelines.size(); i++) {
131  try (FrameSet frames = mPipelines.get(i).waitForFrames()) {
132  try (FrameSet processed = frames.applyFilter(mColorizers.get(i))) {
133  mGLSurfaceView.upload(processed);
134  }
135  }
136  }
137  if (mPipelines.size() > 0)
138  mHandler.post(mStreaming);
139  }
140  catch (Exception e) {
141  Log.e(TAG, "streaming, error: " + e.getMessage());
142  }
143  }
144  };
145 
146  private void configAndStart() throws Exception {
147  for(int i = 0; i < mPipelines.size(); i++) {
148  try (Config config = new Config()) {
149  config.enableDevice(deviceList.createDevice(i).getInfo(CameraInfo.SERIAL_NUMBER));
150  config.enableStream(StreamType.DEPTH, 640, 480);
151  Pipeline pipe = mPipelines.get(i);
152  // try statement needed here to release resources allocated by the Pipeline:start() method
153  try (PipelineProfile pp = pipe.start(config)) {}
154  }
155  }
156  }
157 
158  private synchronized void restart() {
159  stop();
160  start();
161  }
162 
163  private synchronized void start() {
164  deviceList = mRsContext.queryDevices();
165  int devCount = deviceList.getDeviceCount();
166  if( devCount > 0) {
167  for (int i = 0; i < devCount; i++)
168  {
169  mPipelines.add(new Pipeline());
170  mColorizers.add(new Colorizer());
171  }
172  }
173 
174  try{
175  Log.d(TAG, "try start streaming");
176  configAndStart();
177  mHandler.post(mStreaming);
178  mGLSurfaceView.clear();
179  Log.d(TAG, "streaming started successfully");
180  } catch (Exception e) {
181  Log.d(TAG, "failed to start streaming");
182  mGLSurfaceView.clear();
183  }
184  }
185 
186  private synchronized void stop() {
187  try {
188  Log.d(TAG, "try stop streaming");
189 
190  mHandler.removeCallbacks(mStreaming);
191 
192  for(Pipeline pipe : mPipelines)
193  pipe.stop();
194 
195  //Release pipelines
196  for (Pipeline pipeline : mPipelines) {
197  pipeline.close();
198  }
199  //Release colorizers
200  for (Colorizer colorizer : mColorizers) {
201  colorizer.close();
202  }
203 
204  mPipelines.clear();
205  mColorizers.clear();
206  mGLSurfaceView.clear();
207  Log.d(TAG, "streaming stopped successfully");
208 
209  deviceList.close();
210  Log.d(TAG, "closed devices list successfully");
211  } catch (Exception e) {
212  Log.d(TAG, "failed to stop streaming");
213  }
214  }
215 }
String getInfo(CameraInfo info)
Definition: Device.java:25
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
uvc_xu_option< int > super
Definition: l500-options.h:32
synchronized void setDevicesChangedCallback(DeviceListener listener)
Definition: RsContext.java:34
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
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
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
int i


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