fixed preview orientation & size when device is rotated

pull/386/head
Mevin Dhunnooa 7 years ago
parent aec17d3e49
commit a86ad5d45b
  1. 24
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  2. 6
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java

@ -161,6 +161,19 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera
return isCameraAvailable() && mIsBound; return isCameraAvailable() && mIsBound;
} }
void setDeviceOrientation(int deviceOrientation) {
super.setDeviceOrientation(deviceOrientation);
if (mCamera != null && mPreview != null) {
if (this.getDeviceOrientation() == 270)
mCamera.setDisplayOrientation(0);
else if (this.getDeviceOrientation() == 90)
mCamera.setDisplayOrientation(180);
else if (this.getDeviceOrientation() == 0)
mCamera.setDisplayOrientation(90);
}
}
// To be called when the preview size is setup or changed. // To be called when the preview size is setup or changed.
private void startPreview(String log) { private void startPreview(String log) {
LOG.i(log, "Dispatching onCameraPreviewSizeChanged."); LOG.i(log, "Dispatching onCameraPreviewSizeChanged.");
@ -168,7 +181,16 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera
Size previewSize = getPreviewSize(REF_VIEW); Size previewSize = getPreviewSize(REF_VIEW);
boolean wasFlipped = flip(REF_SENSOR, REF_VIEW); boolean wasFlipped = flip(REF_SENSOR, REF_VIEW);
mPreview.setInputStreamSize(previewSize.getWidth(), previewSize.getHeight(), wasFlipped); int width;
int height;
if (this.getDeviceOrientation() == 270 || this.getDeviceOrientation() == 90){
width = Math.max(previewSize.getHeight(), previewSize.getWidth());
height = Math.min(previewSize.getHeight(), previewSize.getWidth());
}else{
width = Math.min(previewSize.getHeight(), previewSize.getWidth());
height = Math.max(previewSize.getHeight(), previewSize.getWidth());
}
mPreview.setInputStreamSize(width,height, wasFlipped);
Camera.Parameters params = mCamera.getParameters(); Camera.Parameters params = mCamera.getParameters();
mPreviewFormat = params.getPreviewFormat(); mPreviewFormat = params.getPreviewFormat();

@ -96,6 +96,10 @@ abstract class CameraController implements
mPreview.setSurfaceCallback(this); mPreview.setSurfaceCallback(this);
} }
public int getDeviceOrientation(){
return mDeviceOrientation;
}
//region Error handling //region Error handling
private static class NoOpExceptionHandler implements Thread.UncaughtExceptionHandler { private static class NoOpExceptionHandler implements Thread.UncaughtExceptionHandler {
@ -275,7 +279,7 @@ abstract class CameraController implements
} }
// This can be called multiple times. // This can be called multiple times.
final void setDeviceOrientation(int deviceOrientation) { void setDeviceOrientation(int deviceOrientation) {
mDeviceOrientation = deviceOrientation; mDeviceOrientation = deviceOrientation;
} }

Loading…
Cancel
Save