DisplayRotationHelper.java
Go to the documentation of this file.
1 package com.introlab.rtabmap;
2 
3 /*
4  * Copyright 2017 Google LLC
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.hardware.camera2.CameraAccessException;
22 import android.hardware.camera2.CameraCharacteristics;
23 import android.hardware.camera2.CameraManager;
24 import android.hardware.display.DisplayManager;
25 import android.hardware.display.DisplayManager.DisplayListener;
26 import android.view.Display;
27 import android.view.Surface;
28 import android.view.WindowManager;
29 import com.google.ar.core.Session;
30 
36 public final class DisplayRotationHelper implements DisplayListener {
37  private boolean viewportChanged;
38  private int viewportWidth;
39  private int viewportHeight;
40  private final Display display;
41  private final DisplayManager displayManager;
42  private final CameraManager cameraManager;
43 
49  public DisplayRotationHelper(Context context) {
50  displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
51  cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
52  WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
53  display = windowManager.getDefaultDisplay();
54  }
55 
57  public void onResume() {
58  displayManager.registerDisplayListener(this, null);
59  }
60 
62  public void onPause() {
63  displayManager.unregisterDisplayListener(this);
64  }
65 
75  public void onSurfaceChanged(int width, int height) {
76  viewportWidth = width;
77  viewportHeight = height;
78  viewportChanged = true;
79  }
80 
90  if (viewportChanged) {
91  int displayRotation = display.getRotation();
92  session.setDisplayGeometry(displayRotation, viewportWidth, viewportHeight);
93  viewportChanged = false;
94  }
95  }
96 
101  public float getCameraSensorRelativeViewportAspectRatio(String cameraId) {
102  float aspectRatio;
103  int cameraSensorToDisplayRotation = getCameraSensorToDisplayRotation(cameraId);
104  switch (cameraSensorToDisplayRotation) {
105  case 90:
106  case 270:
107  aspectRatio = (float) viewportHeight / (float) viewportWidth;
108  break;
109  case 0:
110  case 180:
111  aspectRatio = (float) viewportWidth / (float) viewportHeight;
112  break;
113  default:
114  throw new RuntimeException("Unhandled rotation: " + cameraSensorToDisplayRotation);
115  }
116  return aspectRatio;
117  }
118 
123  public int getCameraSensorToDisplayRotation(String cameraId) {
124  CameraCharacteristics characteristics;
125  try {
126  characteristics = cameraManager.getCameraCharacteristics(cameraId);
127  } catch (CameraAccessException e) {
128  throw new RuntimeException("Unable to determine display orientation", e);
129  }
130 
131  // Camera sensor orientation.
132  int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
133 
134  // Current display orientation.
135  int displayOrientation = toDegrees(display.getRotation());
136 
137  // Make sure we return 0, 90, 180, or 270 degrees.
138  return (sensorOrientation - displayOrientation + 360) % 360;
139  }
140 
141  private int toDegrees(int rotation) {
142  switch (rotation) {
143  case Surface.ROTATION_0:
144  return 0;
145  case Surface.ROTATION_90:
146  return 90;
147  case Surface.ROTATION_180:
148  return 180;
149  case Surface.ROTATION_270:
150  return 270;
151  default:
152  throw new RuntimeException("Unknown rotation " + rotation);
153  }
154  }
155 
156  @Override
157  public void onDisplayAdded(int displayId) {}
158 
159  @Override
160  public void onDisplayRemoved(int displayId) {}
161 
162  @Override
163  public void onDisplayChanged(int displayId) {
164  viewportChanged = true;
165  }
166 }
com.introlab.rtabmap.DisplayRotationHelper.onResume
void onResume()
Definition: DisplayRotationHelper.java:57
com.introlab.rtabmap.DisplayRotationHelper.display
final Display display
Definition: DisplayRotationHelper.java:40
com.introlab.rtabmap.DisplayRotationHelper.onDisplayChanged
void onDisplayChanged(int displayId)
Definition: DisplayRotationHelper.java:163
com.introlab.rtabmap.DisplayRotationHelper.DisplayRotationHelper
DisplayRotationHelper(Context context)
Definition: DisplayRotationHelper.java:49
com.introlab.rtabmap.DisplayRotationHelper
Definition: DisplayRotationHelper.java:36
com.introlab.rtabmap.DisplayRotationHelper.getCameraSensorToDisplayRotation
int getCameraSensorToDisplayRotation(String cameraId)
Definition: DisplayRotationHelper.java:123
com.introlab.rtabmap.DisplayRotationHelper.onDisplayAdded
void onDisplayAdded(int displayId)
Definition: DisplayRotationHelper.java:157
com.introlab.rtabmap.DisplayRotationHelper.onDisplayRemoved
void onDisplayRemoved(int displayId)
Definition: DisplayRotationHelper.java:160
com.introlab.rtabmap.DisplayRotationHelper.onPause
void onPause()
Definition: DisplayRotationHelper.java:62
com.introlab.rtabmap.DisplayRotationHelper.onSurfaceChanged
void onSurfaceChanged(int width, int height)
Definition: DisplayRotationHelper.java:75
com.introlab.rtabmap.DisplayRotationHelper.viewportHeight
int viewportHeight
Definition: DisplayRotationHelper.java:39
com.introlab.rtabmap.DisplayRotationHelper.displayManager
final DisplayManager displayManager
Definition: DisplayRotationHelper.java:41
com.introlab.rtabmap.DisplayRotationHelper.viewportChanged
boolean viewportChanged
Definition: DisplayRotationHelper.java:37
com.introlab.rtabmap.ARCoreSharedCamera.setDisplayGeometry
void setDisplayGeometry(int rotation, int width, int height)
Definition: ARCoreSharedCamera.java:645
com.introlab.rtabmap.DisplayRotationHelper.toDegrees
int toDegrees(int rotation)
Definition: DisplayRotationHelper.java:141
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
glm::rotation
GLM_FUNC_DECL detail::tquat< T, P > rotation(detail::tvec3< T, P > const &orig, detail::tvec3< T, P > const &dest)
com.introlab.rtabmap.DisplayRotationHelper.getCameraSensorRelativeViewportAspectRatio
float getCameraSensorRelativeViewportAspectRatio(String cameraId)
Definition: DisplayRotationHelper.java:101
com
com.introlab.rtabmap.ARCoreSharedCamera
Definition: ARCoreSharedCamera.java:49
float
float
com.introlab.rtabmap.DisplayRotationHelper.viewportWidth
int viewportWidth
Definition: DisplayRotationHelper.java:38
com.introlab.rtabmap.DisplayRotationHelper.updateSessionIfNeeded
void updateSessionIfNeeded(ARCoreSharedCamera session)
Definition: DisplayRotationHelper.java:89
com.introlab.rtabmap.DisplayRotationHelper.cameraManager
final CameraManager cameraManager
Definition: DisplayRotationHelper.java:42


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