TOF_ImageReader.java
Go to the documentation of this file.
1 package com.introlab.rtabmap;
2 
3 
4 import android.graphics.ImageFormat;
5 import android.hardware.camera2.CameraAccessException;
6 import android.hardware.camera2.CameraCaptureSession;
7 import android.hardware.camera2.CameraDevice;
8 import android.hardware.camera2.CameraManager;
9 import android.hardware.camera2.CaptureFailure;
10 import android.hardware.camera2.CaptureRequest;
11 import android.hardware.camera2.TotalCaptureResult;
12 import android.media.Image;
13 import android.media.ImageReader;
14 import android.os.Handler;
15 import android.os.HandlerThread;
16 import android.support.annotation.NonNull;
17 import android.util.Log;
18 import android.view.Surface;
19 
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 
25 public class TOF_ImageReader implements ImageReader.OnImageAvailableListener {
26 
27  public static final String TAG = TOF_ImageReader.class.getSimpleName();
28 
29  public int WIDTH;
30  public int HEIGHT;
31 
32  public ImageReader imageReader;
33  public int frameCount = 0;
34  public long timestamp;
35 
36  // Looper handler thread.
37  private HandlerThread backgroundThread;
38  // Looper handler.
39  public Handler backgroundHandler;
40 
41  public ByteBuffer depth16_raw;
42 
43  // Camera capture session.
44  private CameraCaptureSession captureSession = null;
45  // Camera device.
46  private CameraDevice cameraDevice = null;
47 
48  // Camera preview capture request builder
49  private CaptureRequest.Builder previewCaptureRequestBuilder;
50 
51 
53  }
54 
55  public void close()
56  {
57  Log.i(TAG, "close()");
58  if (captureSession != null) {
59  captureSession.close();
60  captureSession = null;
61  }
62  if (cameraDevice != null) {
63  cameraDevice.close();
64  cameraDevice = null;
65  }
66 
67  if (imageReader != null) {
68  imageReader.close();
69  imageReader = null;
70  }
71  }
72 
73  // Camera device state callback.
74  public final CameraDevice.StateCallback cameraDeviceCallback =
75  new CameraDevice.StateCallback() {
76  @Override
77  public void onOpened(@NonNull CameraDevice cameraDevice) {
78  Log.d(TAG, "Camera depth ID " + cameraDevice.getId() + " opened.");
81  }
82 
83  @Override
84  public void onClosed(@NonNull CameraDevice cameraDevice) {
85  Log.d(TAG, "Camera device ID " + cameraDevice.getId() + " closed.");
86  TOF_ImageReader.this.cameraDevice = null;
87  }
88 
89  @Override
90  public void onDisconnected(@NonNull CameraDevice cameraDevice) {
91  Log.w(TAG, "Camera depth ID " + cameraDevice.getId() + " disconnected.");
92  cameraDevice.close();
93  TOF_ImageReader.this.cameraDevice = null;
94  }
95 
96  @Override
97  public void onError(@NonNull CameraDevice cameraDevice, int error) {
98  Log.e(TAG, "Camera depth ID " + cameraDevice.getId() + " error " + error);
99  cameraDevice.close();
100  TOF_ImageReader.this.cameraDevice = null;
101  }
102  };
103 
104  private CameraCaptureSession.StateCallback captureStateCallback = new CameraCaptureSession.StateCallback() {
105  // Called when the camera capture session is first configured after the app
106  // is initialized, and again each time the activity is resumed.
107  @Override
108  public void onConfigured(@NonNull CameraCaptureSession session) {
109  Log.d(TAG, "Camera capture session configured.");
110  captureSession = session;
112  }
113 
114  @Override
115  public void onSurfacePrepared(
116  @NonNull CameraCaptureSession session, @NonNull Surface surface) {
117  Log.d(TAG, "Camera capture surface prepared.");
118  }
119 
120  @Override
121  public void onReady(@NonNull CameraCaptureSession session) {
122  Log.d(TAG, "Camera capture session ready.");
123  }
124 
125  @Override
126  public void onActive(@NonNull CameraCaptureSession session) {
127  Log.d(TAG, "Camera capture session active.");
128  }
129 
130  @Override
131  public void onClosed(@NonNull CameraCaptureSession session) {
132  Log.d(TAG, "Camera capture session closed.");
133  }
134 
135  @Override
136  public void onConfigureFailed(@NonNull CameraCaptureSession session) {
137  Log.e(TAG, "Failed to configure camera capture session.");
138  }
139  };
140 
141  // Repeating camera capture session capture callback.
142  private final CameraCaptureSession.CaptureCallback captureSessionCallback =
143  new CameraCaptureSession.CaptureCallback() {
144 
145  @Override
146  public void onCaptureCompleted(
147  @NonNull CameraCaptureSession session,
148  @NonNull CaptureRequest request,
149  @NonNull TotalCaptureResult result) {
150  //Log.i(TAG, "onCaptureCompleted");
151  }
152 
153  //@Override // android 23
154  public void onCaptureBufferLost(
155  @NonNull CameraCaptureSession session,
156  @NonNull CaptureRequest request,
157  @NonNull Surface target,
158  long frameNumber) {
159  Log.e(TAG, "onCaptureBufferLost: " + frameNumber);
160  }
161 
162  @Override
163  public void onCaptureFailed(
164  @NonNull CameraCaptureSession session,
165  @NonNull CaptureRequest request,
166  @NonNull CaptureFailure failure) {
167  Log.e(TAG, "onCaptureFailed: " + failure.getFrameNumber() + " " + failure.getReason());
168  }
169 
170  @Override
171  public void onCaptureSequenceAborted(
172  @NonNull CameraCaptureSession session, int sequenceId) {
173  Log.e(TAG, "onCaptureSequenceAborted: " + sequenceId + " " + session);
174  }
175  };
176 
177  // Called when starting non-AR mode or switching to non-AR mode.
178  // Also called when app starts in AR mode, or resumes in AR mode.
179  private void setRepeatingCaptureRequest() {
180  try {
181  captureSession.setRepeatingRequest(
183  } catch (CameraAccessException e) {
184  Log.e(TAG, "Failed to set repeating request", e);
185  }
186  }
187 
188  private void createCameraPreviewSession() {
189  Log.e(TAG, "createCameraPreviewSession: " + "starting camera preview session.");
190  frameCount = 0;
191  try {
192 
193  // Create an ARCore compatible capture request using `TEMPLATE_RECORD`.
194  previewCaptureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
195 
196  previewCaptureRequestBuilder.addTarget(imageReader.getSurface());
197 
198  // Create camera capture session for camera preview using callback.
199  cameraDevice.createCaptureSession(Arrays.asList(imageReader.getSurface()), captureStateCallback, backgroundHandler);
200 
201  } catch (CameraAccessException e) {
202  Log.e(TAG, "CameraAccessException", e);
203  }
204  }
205 
206  public void createImageReader(int width, int height){
207  Log.w(TAG, String.valueOf(width) + "x" + String.valueOf(height));
208  WIDTH = width;
209  HEIGHT = height;
210  this.imageReader =
211  ImageReader.newInstance(
212  width,
213  height,
214  ImageFormat.DEPTH16,
215  2);
216  this.imageReader.setOnImageAvailableListener(this, this.backgroundHandler);
217  }
218 
219  // CPU image reader callback.
220  @Override
221  public void onImageAvailable(ImageReader imageReader) {
222  Image image = imageReader.acquireLatestImage();
223  if (image == null) {
224  Log.w(TAG, "onImageAvailable: Skipping null image.");
225  return;
226  }
227  else{
228  if(image.getFormat() == ImageFormat.DEPTH16){
229  synchronized (this) {
230  depth16_raw = image.getPlanes()[0].getBuffer().asReadOnlyBuffer();
231  timestamp = image.getTimestamp();
232  }
233  // copy raw undecoded DEPTH16 format depth data to NativeBuffer
234  frameCount++;
235  }
236  else{
237  Log.w(TAG, "onImageAvailable: depth image not in DEPTH16 format, skipping image");
238  }
239  }
240  image.close();
241  }
242 
243  // Start background handler thread, used to run callbacks without blocking UI thread.
244  public void startBackgroundThread() {
245  this.backgroundThread = new HandlerThread("DepthDecoderThread");
246  this.backgroundThread.start();
247  this.backgroundHandler = new Handler(backgroundThread.getLooper());
248  }
249 
250  // Stop background handler thread.
251  public void stopBackgroundThread() {
252  if (this.backgroundThread != null) {
253  this.backgroundThread.quitSafely();
254  try {
255  this.backgroundThread.join();
256  this.backgroundThread = null;
257  this.backgroundHandler = null;
258  } catch (InterruptedException e) {
259  Log.e(TAG, "Interrupted while trying to join depth background handler thread", e);
260  }
261  }
262  }
263 
264 }
265 
com.introlab.rtabmap.TOF_ImageReader.captureSessionCallback
final CameraCaptureSession.CaptureCallback captureSessionCallback
Definition: TOF_ImageReader.java:142
com.introlab.rtabmap.TOF_ImageReader.backgroundHandler
Handler backgroundHandler
Definition: TOF_ImageReader.java:39
com.introlab.rtabmap.TOF_ImageReader.captureStateCallback
CameraCaptureSession.StateCallback captureStateCallback
Definition: TOF_ImageReader.java:104
com.introlab.rtabmap.TOF_ImageReader.createCameraPreviewSession
void createCameraPreviewSession()
Definition: TOF_ImageReader.java:188
com.introlab.rtabmap.TOF_ImageReader.imageReader
ImageReader imageReader
Definition: TOF_ImageReader.java:32
com.introlab.rtabmap.TOF_ImageReader.timestamp
long timestamp
Definition: TOF_ImageReader.java:34
com.introlab.rtabmap.TOF_ImageReader.cameraDevice
CameraDevice cameraDevice
Definition: TOF_ImageReader.java:46
com.introlab.rtabmap.TOF_ImageReader.frameCount
int frameCount
Definition: TOF_ImageReader.java:33
com.introlab.rtabmap.TOF_ImageReader.startBackgroundThread
void startBackgroundThread()
Definition: TOF_ImageReader.java:244
com.introlab.rtabmap.TOF_ImageReader.close
void close()
Definition: TOF_ImageReader.java:55
com.introlab.rtabmap.TOF_ImageReader.HEIGHT
int HEIGHT
Definition: TOF_ImageReader.java:30
com.introlab.rtabmap.TOF_ImageReader.WIDTH
int WIDTH
Definition: TOF_ImageReader.java:29
com.introlab.rtabmap.TOF_ImageReader.TAG
static final String TAG
Definition: TOF_ImageReader.java:27
com.introlab.rtabmap.TOF_ImageReader.depth16_raw
ByteBuffer depth16_raw
Definition: TOF_ImageReader.java:41
target
target
error
void error(const char *str)
com.introlab.rtabmap.TOF_ImageReader.createImageReader
void createImageReader(int width, int height)
Definition: TOF_ImageReader.java:206
com.introlab.rtabmap.TOF_ImageReader.backgroundThread
HandlerThread backgroundThread
Definition: TOF_ImageReader.java:37
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
com.introlab.rtabmap.TOF_ImageReader.setRepeatingCaptureRequest
void setRepeatingCaptureRequest()
Definition: TOF_ImageReader.java:179
com.introlab.rtabmap.TOF_ImageReader.stopBackgroundThread
void stopBackgroundThread()
Definition: TOF_ImageReader.java:251
com.introlab.rtabmap.TOF_ImageReader.previewCaptureRequestBuilder
CaptureRequest.Builder previewCaptureRequestBuilder
Definition: TOF_ImageReader.java:49
com.introlab.rtabmap.TOF_ImageReader.cameraDeviceCallback
final CameraDevice.StateCallback cameraDeviceCallback
Definition: TOF_ImageReader.java:74
com.introlab.rtabmap.TOF_ImageReader
Definition: TOF_ImageReader.java:25
result
RESULT & result
com.introlab.rtabmap.TOF_ImageReader.captureSession
CameraCaptureSession captureSession
Definition: TOF_ImageReader.java:44
com.introlab.rtabmap.TOF_ImageReader.onImageAvailable
void onImageAvailable(ImageReader imageReader)
Definition: TOF_ImageReader.java:221


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:22