Better camera preview rotation

This commit is contained in:
Sylvain Berfini 2012-04-05 16:31:01 +02:00
parent ed53b8b62e
commit a2d010cfcc
2 changed files with 25 additions and 16 deletions

View file

@ -267,7 +267,7 @@ public class DialerActivity extends Activity implements LinphoneGuiListener {
if (mCamera == null) { if (mCamera == null) {
mCamera = Camera.open(mCurrentCameraId); mCamera = Camera.open(mCurrentCameraId);
} }
mVideoCaptureView.setCamera(mCamera); mVideoCaptureView.setCamera(mCamera, mCurrentCameraId);
mCamera.startPreview(); mCamera.startPreview();
} }
@ -281,10 +281,10 @@ public class DialerActivity extends Activity implements LinphoneGuiListener {
public void onClick(View v) { public void onClick(View v) {
mCurrentCameraId = (mCurrentCameraId + 1) % numberOfCameras; mCurrentCameraId = (mCurrentCameraId + 1) % numberOfCameras;
mCamera.release(); mCamera.release();
mVideoCaptureView.setCamera(null); mVideoCaptureView.setCamera(null, -1);
mCamera = Camera.open(mCurrentCameraId); mCamera = Camera.open(mCurrentCameraId);
mVideoCaptureView.switchCamera(mCamera); mVideoCaptureView.switchCamera(mCamera, mCurrentCameraId);
mCamera.startPreview(); mCamera.startPreview();
} }
}); });
@ -315,7 +315,7 @@ public class DialerActivity extends Activity implements LinphoneGuiListener {
if (mCamera != null) { if (mCamera != null) {
mCamera.release(); mCamera.release();
mVideoCaptureView.setCamera(null); mVideoCaptureView.setCamera(null, -1);
mCamera = null; mCamera = null;
} }
} }
@ -448,7 +448,7 @@ public class DialerActivity extends Activity implements LinphoneGuiListener {
if (mVideoCaptureView != null && mCamera == null && !LinphoneManager.getLc().isIncall()) if (mVideoCaptureView != null && mCamera == null && !LinphoneManager.getLc().isIncall())
{ {
mCamera = Camera.open(mCurrentCameraId); mCamera = Camera.open(mCurrentCameraId);
mVideoCaptureView.switchCamera(mCamera); mVideoCaptureView.switchCamera(mCamera, mCurrentCameraId);
mCamera.startPreview(); mCamera.startPreview();
} }
} }

View file

@ -3,10 +3,9 @@ package org.linphone.ui;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.linphone.core.Log;
import android.content.Context; import android.content.Context;
import android.hardware.Camera; import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Size; import android.hardware.Camera.Size;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.Display; import android.view.Display;
@ -46,17 +45,19 @@ public class CameraView extends ViewGroup implements SurfaceHolder.Callback {
Size mPreviewSize; Size mPreviewSize;
List<Size> mSupportedSizes; List<Size> mSupportedSizes;
Camera mCamera; Camera mCamera;
int mCameraId;
public void setCamera(Camera camera) { public void setCamera(Camera camera, int id) {
mCamera = camera; mCamera = camera;
mCameraId = id;
if (mCamera != null) { if (mCamera != null) {
mSupportedSizes = mCamera.getParameters().getSupportedPreviewSizes(); mSupportedSizes = mCamera.getParameters().getSupportedPreviewSizes();
requestLayout(); requestLayout();
} }
} }
public void switchCamera(Camera camera) { public void switchCamera(Camera camera, int id) {
setCamera(camera); setCamera(camera, id);
try { try {
camera.setPreviewDisplay(mHolder); camera.setPreviewDisplay(mHolder);
} catch (IOException exception) { } catch (IOException exception) {
@ -77,13 +78,12 @@ public class CameraView extends ViewGroup implements SurfaceHolder.Callback {
if (mSupportedSizes != null) { if (mSupportedSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedSizes, width, height); mPreviewSize = getOptimalPreviewSize(mSupportedSizes, width, height);
} }
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) { if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) {
Size tempSize = mPreviewSize; Size tempSize = mPreviewSize;
mPreviewSize.width = tempSize.height; mPreviewSize.width = tempSize.height;
mPreviewSize.height = tempSize.width; mPreviewSize.height = tempSize.width;
} else {
} }
} }
@ -166,17 +166,26 @@ public class CameraView extends ViewGroup implements SurfaceHolder.Callback {
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Camera.Parameters parameters = mCamera.getParameters(); Camera.Parameters parameters = mCamera.getParameters();
int rotation = 0;
if(display.getRotation() == Surface.ROTATION_90) { if(display.getRotation() == Surface.ROTATION_90) {
mCamera.setDisplayOrientation(270); rotation = 90;
} }
else if(display.getRotation() == Surface.ROTATION_270) { else if(display.getRotation() == Surface.ROTATION_270) {
mCamera.setDisplayOrientation(90); rotation = 270;
} }
else if (display.getRotation() == Surface.ROTATION_180) { else if (display.getRotation() == Surface.ROTATION_180) {
mCamera.setDisplayOrientation(180); rotation = 180;
} }
requestLayout();
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(mCameraId, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
mCamera.setDisplayOrientation((cameraInfo.orientation - rotation + 360) % 360);
} else {
mCamera.setDisplayOrientation((cameraInfo.orientation + rotation) % 360);
}
requestLayout();
mCamera.setParameters(parameters); mCamera.setParameters(parameters);
mCamera.startPreview(); mCamera.startPreview();
} }