下面列出了android.view.Surface#ROTATION_0 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int getDeviceOrientation() {
int orientation = 0;
if (mContext != null) {
WindowManager wm = (WindowManager)mContext.getSystemService(
Context.WINDOW_SERVICE);
switch(wm.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
orientation = 90;
break;
case Surface.ROTATION_180:
orientation = 180;
break;
case Surface.ROTATION_270:
orientation = 270;
break;
case Surface.ROTATION_0:
default:
orientation = 0;
break;
}
}
return orientation;
}
/**
* Gets the current display rotation in angles.
*
* @param activity
* @return
*/
public static int getDisplayRotation(Activity activity) {
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
switch (rotation) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
}
return 0;
}
@Override
public void alignCameraAndDisplayOrientation(WindowManager windowManager) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(id, info);
int rotation = windowManager.getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
/**
* カメラの取り付けられた向きと画面の向きから縦横のスワップが必要か確認します.
*
* @return スワップが必要な場合はtrue、それ以外はfalse
*/
public boolean isSwappedDimensions() {
int sensorOrientation = getSensorOrientation();
switch (getDisplayRotation()) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
if (sensorOrientation == 90 || sensorOrientation == 270) {
return true;
}
break;
case Surface.ROTATION_90:
case Surface.ROTATION_270:
if (sensorOrientation == 0 || sensorOrientation == 180) {
return true;
}
break;
default:
if (DEBUG) {
Log.w(TAG, "Display rotation is invalid.");
}
break;
}
return false;
}
private void updateOrientation() {
SensorManager.getRotationMatrix(this.mRotationMatrix, null, this.mAccelerationValues, this.mMagneticFieldValues);
// TODO Use dont't use identical matrixes in remapCoordinateSystem, due to performance reasons.
switch (this.mDisplayRotation) {
case Surface.ROTATION_0:
/* Nothing. */
break;
case Surface.ROTATION_90:
SensorManager.remapCoordinateSystem(this.mRotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, this.mRotationMatrix);
break;
// case Surface.ROTATION_180:
// SensorManager.remapCoordinateSystem(this.mRotationMatrix, SensorManager.AXIS_?, SensorManager.AXIS_?, this.mRotationMatrix);
// break;
// case Surface.ROTATION_270:
// SensorManager.remapCoordinateSystem(this.mRotationMatrix, SensorManager.AXIS_?, SensorManager.AXIS_?, this.mRotationMatrix);
// break;
}
final float[] values = this.mValues;
SensorManager.getOrientation(this.mRotationMatrix, values);
for (int i = values.length - 1; i >= 0; i--) {
values[i] = values[i] * MathConstants.RAD_TO_DEG;
}
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if (x<5 && x>-5 && y > 5)
mOrientation = Surface.ROTATION_0; // portrait
else if (x<-5 && y<5 && y>-5)
mOrientation = Surface.ROTATION_270; // right
else if (x<5 && x>-5 && y<-5)
mOrientation = Surface.ROTATION_180; // upside down
else if (x>5 && y<5 && y>-5)
mOrientation = Surface.ROTATION_90; // left
if (mListener != null) {
mListener.orientationEvent();
}
}
private int getDeviceOrientation() {
int orientation = 0;
WindowManager wm = (WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE);
switch (wm.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
orientation = 90;
break;
case Surface.ROTATION_180:
orientation = 180;
break;
case Surface.ROTATION_270:
orientation = 270;
break;
case Surface.ROTATION_0:
default:
orientation = 0;
break;
}
return orientation;
}
private void startPreview() {
mCamera = Camera.open();
if (mCamera == null) {
// Seeing this on Nexus 7 2012 -- I guess it wants a rear-facing camera, but
// there isn't one. TODO: fix
throw new RuntimeException("Default camera not available");
}
try {
mCamera.setPreviewTexture(mSurfaceTexture);
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0) {
mCamera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_270) {
mCamera.setDisplayOrientation(180);
}
mCamera.startPreview();
} catch (IOException ioe) {
// Something bad happened
Log.e(TAG,"Exception starting preview", ioe);
}
}
private int getCurrentOrientation() {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_90:
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
default:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_270:
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
default:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
}
@Override
public void onSurfaceChanged(int width, int height) {
int rotation = Surface.ROTATION_0;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
rotation = Surface.ROTATION_90;
}
mSession.setDisplayGeometry(rotation, width, height);
}
/**
* 解像度の縦横のサイズをスワップするか判断します.
*
* @return スワップする場合は true、それ以外は false
*/
public boolean isSwappedDimensions() {
switch (getDisplayRotation()) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
return false;
default:
return true;
}
}
public static int setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera, int displayRotation) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int degrees = 0;
switch (displayRotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int camRotationDegree = 0;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
camRotationDegree = (info.orientation + degrees) % 360;
camRotationDegree = (360 - camRotationDegree) % 360; // compensate the mirror
} else {
camRotationDegree = (info.orientation - degrees + 360) % 360;
}
if (camera != null) {
camera.setDisplayOrientation(camRotationDegree);
}
return camRotationDegree;
}
public static int getCameraDisplayOrientation(Context context, int cameraId) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int rotation = wm.getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else {
// back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
@Test
public void getSupportedRecordingSizeTooSmall() {
NativeCamera mockCamera = createCameraWithMockParameters(640, 480, 0, 0);
final CameraWrapper wrapper = new CameraWrapper(mockCamera, Surface.ROTATION_0);
RecordingSize supportedRecordingSize = wrapper.getSupportedRecordingSize(320, 240);
assertEquals(supportedRecordingSize.width, 640);
assertEquals(supportedRecordingSize.height, 480);
}
@Test
public void releaseCameraWhenCameraNotNull() {
NativeCamera mockCamera = mock(NativeCamera.class);
doReturn(mock(Camera.class)).when(mockCamera).getNativeCamera();
final CameraWrapper wrapper = new CameraWrapper(mockCamera, Surface.ROTATION_0);
wrapper.releaseCamera();
verify(mockCamera, times(1)).releaseNativeCamera();
}
private boolean remapCoordinates(float[] inR, float[] outR) {
int xDir = SensorManager.AXIS_X;
int yDir = SensorManager.AXIS_Y;
int curRotation = getScreenRotation();
if (lastRotation != curRotation) {
logger.log(Level.FINE, "Device Rotation changed to: {0}", curRotation);
}
lastRotation = curRotation;
// logger.log(Level.FINE, "Screen Rotation: {0}", getScreenRotation());
switch (getScreenRotation()) {
// device natural position
case Surface.ROTATION_0:
xDir = SensorManager.AXIS_X;
yDir = SensorManager.AXIS_Y;
break;
// device rotated 90 deg counterclockwise
case Surface.ROTATION_90:
xDir = SensorManager.AXIS_Y;
yDir = SensorManager.AXIS_MINUS_X;
break;
// device rotated 180 deg counterclockwise
case Surface.ROTATION_180:
xDir = SensorManager.AXIS_MINUS_X;
yDir = SensorManager.AXIS_MINUS_Y;
break;
// device rotated 270 deg counterclockwise
case Surface.ROTATION_270:
xDir = SensorManager.AXIS_MINUS_Y;
yDir = SensorManager.AXIS_X;
break;
default:
break;
}
return SensorManager.remapCoordinateSystem(inR, xDir, yDir, outR);
}
@Test
public void prepareCameraWhenRuntimeException() {
NativeCamera mockCamera = mock(NativeCamera.class);
doThrow(new RuntimeException()).when(mockCamera).unlockNativeCamera();
final CameraWrapper wrapper = new CameraWrapper(mockCamera, Surface.ROTATION_0);
try {
wrapper.prepareCameraForRecording();
fail("Missing exception");
} catch (final PrepareCameraException e) {
assertEquals("Unable to use camera for recording", e.getMessage());
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (camera == null)
return;
// �nderung des Surface f�r Kameravorschau ber�cksichtigen
// Bilddrehung
// Systemdienstinstanz erhalten
WindowManager windowManager = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
// Rotationskonstante Surface.ROTATION_...
int rotation = windowManager.getDefaultDisplay().getRotation();
// According to API Documentation
// http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
// // Annahme, dass Kamera 90� verdreht zu Standardausrichtung
// platziert
// // ist. Tats�chlicher Wert erst ab Android 2.3 abrufbar. TODO
// int cameraRotation = (90 - degrees + 360) % 360;
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(cameraId, info);
int cameraRotation;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
cameraRotation = (info.orientation + degrees) % 360;
cameraRotation = (360 - cameraRotation) % 360; // compensate the
// mirror
} else { // back-facing
cameraRotation = (info.orientation - degrees + 360) % 360;
}
camera.stopPreview(); // Fix for 2.3.3 Bug?
camera.setDisplayOrientation(cameraRotation);
// Bildgr��e
Camera.Parameters parameters = camera.getParameters();
Size bestSize = getOptimalPreviewSize(
parameters.getSupportedPreviewSizes(),
cameraSizeHintWidth != 0 ? cameraSizeHintWidth : width,
cameraSizeHintHeight != 0 ? cameraSizeHintHeight : height);
parameters.setPreviewSize(bestSize.width, bestSize.height);
// Update static camera settings fields
if (cameraRotation == 0 || cameraRotation == 180) {
RealityCamera.setViewportSize(bestSize);
RealityCamera.setFovY(parameters.getVerticalViewAngle());
// RealityCamera.setAspect(parameters.getHorizontalViewAngle()
// / parameters.getVerticalViewAngle());
// TODO merge updates
} else {
RealityCamera.setViewportSize(bestSize.height, bestSize.width);
RealityCamera.setFovY(parameters.getHorizontalViewAngle());
// RealityCamera.setAspect(parameters.getVerticalViewAngle()
// / parameters.getHorizontalViewAngle());
// TODO merge updates
}
camera.setParameters(parameters);
// Neustart der Vorschaudarstellung
camera.startPreview();
requestLayout();
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void lockScreenOrientation(Activity activity)
{
WindowManager windowManager =
(WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
Configuration configuration = activity.getResources().getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
// Search for the natural position of the device
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && (
rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
|| configuration.orientation == Configuration.ORIENTATION_PORTRAIT && (
rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)) {
// Natural position is Landscape
switch (rotation) {
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
} else {
// Natural position is Portrait
switch (rotation) {
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
}
}
public void setCameraDisplayOrientation(int cameraId) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if (Build.MODEL.equalsIgnoreCase("Nexus 6") && flag == 1) {
rotation = Surface.ROTATION_180;
}
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else {
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}