recording/src/main/java/com/intel/realsense/recording/MainActivity.java
Go to the documentation of this file.
1 package com.intel.realsense.recording;
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.Environment;
8 import android.os.Handler;
9 import com.google.android.material.floatingactionbutton.FloatingActionButton;
10 import androidx.core.app.ActivityCompat;
11 import androidx.core.content.ContextCompat;
12 import androidx.appcompat.app.AppCompatActivity;
13 import android.util.Log;
14 import android.view.View;
15 import android.widget.TextView;
16 
26 
27 import java.io.File;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 
31 public class MainActivity extends AppCompatActivity {
32  private static final String TAG = "librs recording example";
33  private static final int PERMISSIONS_REQUEST_CAMERA = 0;
34  private static final int PERMISSIONS_REQUEST_WRITE = 1;
35 
36  private boolean mPermissionsGranted = false;
37 
38  private Context mAppContext;
39  private TextView mBackGroundText;
41  private boolean mIsStreaming = false;
42  private final Handler mHandler = new Handler();
43 
46 
47  private FloatingActionButton mStartRecordFab;
48  private FloatingActionButton mStopRecordFab;
49 
50  @Override
51  protected void onCreate(Bundle savedInstanceState) {
52  super.onCreate(savedInstanceState);
53  setContentView(R.layout.activity_main);
54 
55  mAppContext = getApplicationContext();
56  mBackGroundText = findViewById(R.id.connectCameraText);
57  mGLSurfaceView = findViewById(R.id.glSurfaceView);
58 
59  mStartRecordFab = findViewById(R.id.startRecordFab);
60  mStopRecordFab = findViewById(R.id.stopRecordFab);
61 
62  mStartRecordFab.setOnClickListener(new View.OnClickListener() {
63  @Override
64  public void onClick(View view) {
66  }
67  });
68  mStopRecordFab.setOnClickListener(new View.OnClickListener() {
69  @Override
70  public void onClick(View view) {
72  }
73  });
74 
75  // Android 9 also requires camera permissions
76  if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.O &&
77  ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
78  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
79  return;
80  }
81 
82  if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
83  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
84  return;
85  }
86 
87  mPermissionsGranted = true;
88  }
89 
90  @Override
91  protected void onDestroy() {
92  super.onDestroy();
93  mGLSurfaceView.close();
94  }
95 
96  @Override
97  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
98  if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
99  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_WRITE);
100  return;
101  }
102 
103  if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
104  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
105  return;
106  }
107 
108  mPermissionsGranted = true;
109  }
110 
111  @Override
112  protected void onResume() {
113  super.onResume();
114 
115  if(mPermissionsGranted)
116  init();
117  else
118  Log.e(TAG, "missing permissions");
119  }
120 
121  @Override
122  protected void onPause() {
123  super.onPause();
124  if(mRsContext != null)
125  mRsContext.close();
126  stop();
127  }
128 
129  private String getFilePath(){
130  File folder = new File(getExternalFilesDir(null).getAbsolutePath() + File.separator + "rs_bags");
131  folder.mkdir();
132  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
133  String currentDateAndTime = sdf.format(new Date());
134  File file = new File(folder, currentDateAndTime + ".bag");
135  return file.getAbsolutePath();
136  }
137 
138  void init(){
139  //RsContext.init must be called once in the application lifetime before any interaction with physical RealSense devices.
140  //For multi activities applications use the application context instead of the activity context
141  RsContext.init(mAppContext);
142 
143  //Register to notifications regarding RealSense devices attach/detach events via the DeviceListener.
144  mRsContext = new RsContext();
146 
147  mPipeline = new Pipeline();
148 
149  try(DeviceList dl = mRsContext.queryDevices()){
150  if(dl.getDeviceCount() > 0) {
151  showConnectLabel(false);
152  start(false);
153  }
154  }
155  }
156 
157  private void showConnectLabel(final boolean state){
158  runOnUiThread(new Runnable() {
159  @Override
160  public void run() {
161  mBackGroundText.setVisibility(state ? View.VISIBLE : View.GONE);
162  mStartRecordFab.setVisibility(!state ? View.VISIBLE : View.GONE);
163  mStopRecordFab.setVisibility(View.GONE);
164  }
165  });
166  }
167 
168  private void toggleRecording(){
169  stop();
170  start(mStartRecordFab.getVisibility() == View.VISIBLE);
171  runOnUiThread(new Runnable() {
172  @Override
173  public void run() {
174  mStartRecordFab.setVisibility(mStartRecordFab.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
175  mStopRecordFab.setVisibility(mStopRecordFab.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
176  }
177  });
178  }
179 
181  @Override
182  public void onDeviceAttach() {
183  showConnectLabel(false);
184  }
185 
186  @Override
187  public void onDeviceDetach() {
188  showConnectLabel(true);
189  stop();
190  }
191  };
192 
193  Runnable mStreaming = new Runnable() {
194  @Override
195  public void run() {
196  try {
197  try(FrameSet frames = mPipeline.waitForFrames()) {
198  mGLSurfaceView.upload(frames);
199  }
200  mHandler.post(mStreaming);
201  }
202  catch (Exception e) {
203  Log.e(TAG, "streaming, error: " + e.getMessage());
204  }
205  }
206  };
207 
208  private synchronized void start(boolean record) {
209  if(mIsStreaming)
210  return;
211  try{
212  mGLSurfaceView.clear();
213  Log.d(TAG, "try start streaming");
214  try(Config cfg = new Config()) {
215  cfg.enableStream(StreamType.DEPTH, 640, 480);
216  cfg.enableStream(StreamType.COLOR, 640, 480);
217  if (record)
218  cfg.enableRecordToFile(getFilePath());
219  // try statement needed here to release resources allocated by the Pipeline:start() method
220  try(PipelineProfile pp = mPipeline.start(cfg)){}
221  }
222  mIsStreaming = true;
223  mHandler.post(mStreaming);
224  Log.d(TAG, "streaming started successfully");
225  } catch (Exception e) {
226  Log.d(TAG, "failed to start streaming");
227  }
228  }
229 
230  private synchronized void stop() {
231  if(!mIsStreaming)
232  return;
233  try {
234  Log.d(TAG, "try stop streaming");
235  mIsStreaming = false;
236  mHandler.removeCallbacks(mStreaming);
237  mPipeline.stop();
238  mGLSurfaceView.clear();
239  Log.d(TAG, "streaming stopped successfully");
240  } catch (Exception e) {
241  Log.d(TAG, "failed to stop streaming");
242  mPipeline = null;
243  }
244  }
245 }
::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