1 package com.introlab.rtabmap;
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;
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
57 Log.i(
TAG,
"close()");
75 new CameraDevice.StateCallback() {
77 public void onOpened(@NonNull CameraDevice
cameraDevice) {
84 public void onClosed(@NonNull CameraDevice
cameraDevice) {
90 public void onDisconnected(@NonNull CameraDevice
cameraDevice) {
91 Log.w(
TAG,
"Camera depth ID " +
cameraDevice.getId() +
" disconnected.");
108 public void onConfigured(@NonNull CameraCaptureSession session) {
109 Log.d(
TAG,
"Camera capture session configured.");
115 public void onSurfacePrepared(
116 @NonNull CameraCaptureSession session, @NonNull Surface surface) {
117 Log.d(
TAG,
"Camera capture surface prepared.");
121 public void onReady(@NonNull CameraCaptureSession session) {
122 Log.d(
TAG,
"Camera capture session ready.");
126 public void onActive(@NonNull CameraCaptureSession session) {
127 Log.d(
TAG,
"Camera capture session active.");
131 public void onClosed(@NonNull CameraCaptureSession session) {
132 Log.d(
TAG,
"Camera capture session closed.");
136 public void onConfigureFailed(@NonNull CameraCaptureSession session) {
137 Log.e(
TAG,
"Failed to configure camera capture session.");
143 new CameraCaptureSession.CaptureCallback() {
146 public void onCaptureCompleted(
147 @NonNull CameraCaptureSession session,
148 @NonNull CaptureRequest request,
149 @NonNull TotalCaptureResult
result) {
154 public void onCaptureBufferLost(
155 @NonNull CameraCaptureSession session,
156 @NonNull CaptureRequest request,
159 Log.e(
TAG,
"onCaptureBufferLost: " + frameNumber);
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());
171 public void onCaptureSequenceAborted(
172 @NonNull CameraCaptureSession session,
int sequenceId) {
173 Log.e(
TAG,
"onCaptureSequenceAborted: " + sequenceId +
" " + session);
183 }
catch (CameraAccessException
e) {
184 Log.e(
TAG,
"Failed to set repeating request",
e);
189 Log.e(
TAG,
"createCameraPreviewSession: " +
"starting camera preview session.");
201 }
catch (CameraAccessException
e) {
202 Log.e(
TAG,
"CameraAccessException",
e);
207 Log.w(
TAG, String.valueOf(width) +
"x" + String.valueOf(height));
211 ImageReader.newInstance(
216 this.imageReader.setOnImageAvailableListener(
this, this.backgroundHandler);
224 Log.w(
TAG,
"onImageAvailable: Skipping null image.");
228 if(image.getFormat() == ImageFormat.DEPTH16){
229 synchronized (
this) {
230 depth16_raw = image.getPlanes()[0].getBuffer().asReadOnlyBuffer();
237 Log.w(
TAG,
"onImageAvailable: depth image not in DEPTH16 format, skipping image");
245 this.backgroundThread =
new HandlerThread(
"DepthDecoderThread");
246 this.backgroundThread.start();
252 if (this.backgroundThread !=
null) {
253 this.backgroundThread.quitSafely();
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);