processing/src/main/java/com/intel/realsense/processing/MainActivity.java
Go to the documentation of this file.
1 package com.intel.realsense.processing;
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 
36 
37 public class MainActivity extends AppCompatActivity {
38  private static final String TAG = "librs process example";
39  private static final int PERMISSIONS_REQUEST_CAMERA = 0;
40 
41  private boolean mPermissionsGranted = false;
42 
43  private Context mAppContext;
44  private TextView mBackGroundText;
47  private boolean mIsStreaming = false;
48  private final Handler mHandler = new Handler();
49 
51 
52  //filters
53  private Align mAlign;
62 
64 
65  @Override
66  protected void onCreate(Bundle savedInstanceState) {
67  super.onCreate(savedInstanceState);
68  setContentView(R.layout.activity_main);
69 
70  mAppContext = getApplicationContext();
71  mBackGroundText = findViewById(R.id.connectCameraText);
72 
73  mGLSurfaceViewOrg = findViewById(R.id.glSurfaceViewOrg);
74  mGLSurfaceViewOrg.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
75  | View.SYSTEM_UI_FLAG_FULLSCREEN
76  | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
77  | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
78  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
79  | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
80 
81  mGLSurfaceViewProcessed = findViewById(R.id.glSurfaceViewProcessed);
82  mGLSurfaceViewProcessed.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
83  | View.SYSTEM_UI_FLAG_FULLSCREEN
84  | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
85  | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
86  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
87  | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
88 
89  // Android 9 also requires camera permissions
90  if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.O &&
91  ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
92  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
93  return;
94  }
95 
96  mPermissionsGranted = true;
97  }
98 
99  @Override
100  protected void onDestroy() {
101  super.onDestroy();
102  mGLSurfaceViewOrg.close();
103  mGLSurfaceViewProcessed.close();
104  }
105 
106  @Override
107  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
108  if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
109  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
110  return;
111  }
112  mPermissionsGranted = true;
113  }
114 
115  @Override
116  protected void onResume() {
117  super.onResume();
118  if(mPermissionsGranted)
119  init();
120  else
121  Log.e(TAG, "missing permissions");
122  }
123 
124  @Override
125  protected void onPause() {
126  super.onPause();
127  if(mRsContext != null)
128  mRsContext.close();
129  stop();
130  mPipeline.close();
131  }
132 
133  private void init(){
134  //RsContext.init must be called once in the application lifetime before any interaction with physical RealSense devices.
135  //For multi activities applications use the application context instead of the activity context
136  RsContext.init(mAppContext);
137 
138  //Register to notifications regarding RealSense devices attach/detach events via the DeviceListener.
139  mRsContext = new RsContext();
141 
142  mPipeline = new Pipeline();
143 
144  //init filters
145  mAlign = new Align(StreamType.COLOR);
146  mColorizerOrg = new Colorizer();
147  mColorizerProcessed = new Colorizer();
148  mDecimationFilter = new DecimationFilter();
149  mHoleFillingFilter = new HoleFillingFilter();
150  mPointcloud = new Pointcloud();
151  mTemporalFilter = new TemporalFilter();
152  mThresholdFilter = new ThresholdFilter();
153  mSpatialFilter = new SpatialFilter();
154 
155  //config filters
156  mThresholdFilter.setValue(Option.MIN_DISTANCE, 0.1f);
157  mThresholdFilter.setValue(Option.MAX_DISTANCE, 0.8f);
158 
159  mDecimationFilter.setValue(Option.FILTER_MAGNITUDE, 8);
160 
161  try(DeviceList dl = mRsContext.queryDevices()){
162  if(dl.getDeviceCount() > 0) {
163  showConnectLabel(false);
164  start();
165  }
166  }
167  }
168 
169  private void showConnectLabel(final boolean state){
170  runOnUiThread(new Runnable() {
171  @Override
172  public void run() {
173  mBackGroundText.setVisibility(state ? View.VISIBLE : View.GONE);
174  }
175  });
176  }
177 
179  @Override
180  public void onDeviceAttach() {
181  showConnectLabel(false);
182  }
183 
184  @Override
185  public void onDeviceDetach() {
186  showConnectLabel(true);
187  stop();
188  }
189  };
190 
191  Runnable mStreaming = new Runnable() {
192  @Override
193  public void run() {
194  try {
195  try(FrameReleaser fr = new FrameReleaser()){
196  FrameSet frames = mPipeline.waitForFrames().releaseWith(fr);
197  FrameSet orgSet = frames.applyFilter(mColorizerOrg).releaseWith(fr);
198  FrameSet processedSet = frames.applyFilter(mDecimationFilter).releaseWith(fr).
199  applyFilter(mHoleFillingFilter).releaseWith(fr).
200  applyFilter(mTemporalFilter).releaseWith(fr).
201  applyFilter(mSpatialFilter).releaseWith(fr).
202  applyFilter(mThresholdFilter).releaseWith(fr).
203  applyFilter(mColorizerProcessed).releaseWith(fr).
204  applyFilter(mAlign).releaseWith(fr);
205  try(Frame org = orgSet.first(StreamType.DEPTH, StreamFormat.RGB8).releaseWith(fr)){
206  try(Frame processed = processedSet.first(StreamType.DEPTH, StreamFormat.RGB8).releaseWith(fr)){
207  mGLSurfaceViewOrg.upload(org);
208  mGLSurfaceViewProcessed.upload(processed);
209  }
210  }
211  }
212  mHandler.post(mStreaming);
213  }
214  catch (Exception e) {
215  Log.e(TAG, "streaming, error: " + e.getMessage());
216  }
217  }
218  };
219 
220  private void configAndStart() throws Exception {
221  try(Config config = new Config())
222  {
223  config.enableStream(StreamType.DEPTH, 640, 480);
224  config.enableStream(StreamType.COLOR, 640, 480);
225  // try statement needed here to release resources allocated by the Pipeline:start() method
226  try(PipelineProfile pp = mPipeline.start(config)){}
227  }
228  }
229 
230  private synchronized void start() {
231  if(mIsStreaming)
232  return;
233  try{
234  Log.d(TAG, "try start streaming");
235  mGLSurfaceViewOrg.clear();
236  mGLSurfaceViewProcessed.clear();
237  configAndStart();
238  mIsStreaming = true;
239  mHandler.post(mStreaming);
240  Log.d(TAG, "streaming started successfully");
241  } catch (Exception e) {
242  Log.d(TAG, "failed to start streaming");
243  }
244  }
245 
246  private synchronized void stop() {
247  if(!mIsStreaming)
248  return;
249  try {
250  Log.d(TAG, "try stop streaming");
251  mIsStreaming = false;
252  mHandler.removeCallbacks(mStreaming);
253  mPipeline.stop();
254  Log.d(TAG, "streaming stopped successfully");
255  mGLSurfaceViewOrg.clear();
256  mGLSurfaceViewProcessed.clear();
257  } catch (Exception e) {
258  Log.d(TAG, "failed to stop streaming");
259  mPipeline = null;
260  mColorizerOrg.close();
261  mColorizerProcessed.close();
262  }
263  }
264 }
FrameSet releaseWith(FrameReleaser frameReleaser)
Definition: FrameSet.java:41
::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 setValue(Option option, float value)
Definition: Options.java:36
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
FrameSet applyFilter(FilterInterface filter)
Definition: FrameSet.java:37
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
Frame releaseWith(FrameReleaser frameReleaser)
Definition: Frame.java:56


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