capture/src/main/java/com/intel/realsense/capture/MainActivity.java
Go to the documentation of this file.
1 package com.intel.realsense.capture;
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 import android.widget.TextView;
14 
25 
26 public class MainActivity extends AppCompatActivity {
27  private static final String TAG = "librs capture example";
28  private static final int PERMISSIONS_REQUEST_CAMERA = 0;
29 
30  private boolean mPermissionsGranted = false;
31 
32  private Context mAppContext;
33  private TextView mBackGroundText;
35  private boolean mIsStreaming = false;
36  private final Handler mHandler = new Handler();
37 
41 
42  @Override
43  protected void onCreate(Bundle savedInstanceState) {
44  super.onCreate(savedInstanceState);
45  setContentView(R.layout.activity_main);
46 
47  mAppContext = getApplicationContext();
48  mBackGroundText = findViewById(R.id.connectCameraText);
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  if(mRsContext != null)
95  mRsContext.close();
96  stop();
97  mColorizer.close();
98  mPipeline.close();
99  }
100 
101  private void init(){
102  //RsContext.init must be called once in the application lifetime before any interaction with physical RealSense devices.
103  //For multi activities applications use the application context instead of the activity context
104  RsContext.init(mAppContext);
105 
106  //Register to notifications regarding RealSense devices attach/detach events via the DeviceListener.
107  mRsContext = new RsContext();
109 
110  mPipeline = new Pipeline();
111  mColorizer = new Colorizer();
112 
113  try(DeviceList dl = mRsContext.queryDevices()){
114  if(dl.getDeviceCount() > 0) {
115  showConnectLabel(false);
116  start();
117  }
118  }
119  }
120 
121  private void showConnectLabel(final boolean state){
122  runOnUiThread(new Runnable() {
123  @Override
124  public void run() {
125  mBackGroundText.setVisibility(state ? View.VISIBLE : View.GONE);
126  }
127  });
128  }
129 
131  @Override
132  public void onDeviceAttach() {
133  showConnectLabel(false);
134  }
135 
136  @Override
137  public void onDeviceDetach() {
138  showConnectLabel(true);
139  stop();
140  }
141  };
142 
143  Runnable mStreaming = new Runnable() {
144  @Override
145  public void run() {
146  try {
147  try(FrameSet frames = mPipeline.waitForFrames()) {
148  try(FrameSet processed = frames.applyFilter(mColorizer)) {
149  mGLSurfaceView.upload(processed);
150  }
151  }
152  mHandler.post(mStreaming);
153  }
154  catch (Exception e) {
155  Log.e(TAG, "streaming, error: " + e.getMessage());
156  }
157  }
158  };
159 
160  private void configAndStart() throws Exception {
161  try(Config config = new Config())
162  {
163  config.enableStream(StreamType.DEPTH, 640, 480);
164  config.enableStream(StreamType.COLOR, 640, 480);
165  // try statement needed here to release resources allocated by the Pipeline:start() method
166  try(PipelineProfile pp = mPipeline.start(config)){}
167  }
168  }
169 
170  private synchronized void start() {
171  if(mIsStreaming)
172  return;
173  try{
174  Log.d(TAG, "try start streaming");
175  mGLSurfaceView.clear();
176  configAndStart();
177  mIsStreaming = true;
178  mHandler.post(mStreaming);
179  Log.d(TAG, "streaming started successfully");
180  } catch (Exception e) {
181  Log.d(TAG, "failed to start streaming");
182  }
183  }
184 
185  private synchronized void stop() {
186  if(!mIsStreaming)
187  return;
188  try {
189  Log.d(TAG, "try stop streaming");
190  mIsStreaming = false;
191  mHandler.removeCallbacks(mStreaming);
192  mPipeline.stop();
193  mGLSurfaceView.clear();
194  Log.d(TAG, "streaming stopped successfully");
195  } catch (Exception e) {
196  Log.d(TAG, "failed to stop streaming");
197  }
198  }
199 }
::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
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
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)


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