Merge pull request #39 from andreidiaconu/fix_landscape_rotation

Fixed landscape rotation issues
pull/1/head
Dylan McIntyre 8 years ago committed by GitHub
commit 244c0594cd
  1. 30
      camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java
  2. 7
      camerakit/src/main/api16/com/flurgle/camerakit/ProcessStillTask.java
  3. 37
      camerakit/src/main/base/com/flurgle/camerakit/TextureViewPreview.java
  4. 1
      demo/src/main/AndroidManifest.xml

@ -221,7 +221,7 @@ public class Camera1 extends CameraImpl {
mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
new Thread(new ProcessStillTask(data, camera, mCameraInfo, new ProcessStillTask.OnStillProcessedListener() {
new Thread(new ProcessStillTask(data, camera, calculateCaptureRotation(), new ProcessStillTask.OnStillProcessedListener() {
@Override
public void onStillProcessed(final YuvImage yuv) {
mCameraListener.onPictureTaken(yuv);
@ -320,9 +320,7 @@ public class Camera1 extends CameraImpl {
mCameraParameters = mCamera.getParameters();
adjustCameraParameters();
mCamera.setDisplayOrientation(
calculateCameraRotation(mDisplayOrientation)
);
mCamera.setDisplayOrientation(calculatePreviewRotation());
mCameraListener.onCameraOpened();
}
@ -350,18 +348,29 @@ public class Camera1 extends CameraImpl {
}
}
private int calculateCameraRotation(int rotation) {
private int calculatePreviewRotation() {
if (mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
return ((mCameraInfo.orientation - mDisplayOrientation) + 360 + 180) % 360;
} else {
return (mCameraInfo.orientation - mDisplayOrientation + 360) % 360;
}
}
private int calculateCaptureRotation() {
int previewRotation = calculatePreviewRotation();
if (mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
return (360 - (mCameraInfo.orientation + rotation) % 360) % 360;
//Front is flipped
return (previewRotation + 180 + 2*mDisplayOrientation + 720) %360;
} else {
return (mCameraInfo.orientation - rotation + 360) % 360;
return previewRotation;
}
}
private void adjustCameraParameters() {
boolean invertPreviewSizes = mDisplayOrientation%180 != 0;
mPreview.setTruePreviewSize(
getPreviewResolution().getWidth(),
getPreviewResolution().getHeight()
invertPreviewSizes? getPreviewResolution().getHeight() : getPreviewResolution().getWidth(),
invertPreviewSizes? getPreviewResolution().getWidth() : getPreviewResolution().getHeight()
);
mCameraParameters.setPreviewSize(
@ -373,8 +382,7 @@ public class Camera1 extends CameraImpl {
getCaptureResolution().getWidth(),
getCaptureResolution().getHeight()
);
int rotation = (calculateCameraRotation(mDisplayOrientation)
+ (mFacing == CameraKit.Constants.FACING_FRONT ? 180 : 0) ) % 360;
int rotation = calculateCaptureRotation();
mCameraParameters.setRotation(rotation);
setFocus(mFocus);

@ -7,13 +7,13 @@ class ProcessStillTask implements Runnable {
private byte[] data;
private Camera camera;
private Camera.CameraInfo cameraInfo;
private int rotation;
private OnStillProcessedListener onStillProcessedListener;
public ProcessStillTask(byte[] data, Camera camera, Camera.CameraInfo cameraInfo, OnStillProcessedListener onStillProcessedListener) {
public ProcessStillTask(byte[] data, Camera camera, int rotation, OnStillProcessedListener onStillProcessedListener) {
this.data = data;
this.camera = camera;
this.cameraInfo = cameraInfo;
this.rotation = rotation;
this.onStillProcessedListener = onStillProcessedListener;
}
@ -22,7 +22,6 @@ class ProcessStillTask implements Runnable {
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
int rotation = cameraInfo.orientation;
byte[] rotatedData = new Rotation(data, width, height, rotation).getYuv();
int postWidth;

@ -2,7 +2,6 @@ package com.flurgle.camerakit;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;
@ -24,14 +23,12 @@ class TextureViewPreview extends PreviewImpl {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
setSize(width, height);
configureTransform();
dispatchSurfaceChanged();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
setSize(width, height);
configureTransform();
dispatchSurfaceChanged();
setTruePreviewSize(mTrueWidth, mTrueHeight);
}
@ -71,7 +68,6 @@ class TextureViewPreview extends PreviewImpl {
@Override
void setDisplayOrientation(int displayOrientation) {
mDisplayOrientation = displayOrientation;
configureTransform();
}
@Override
@ -98,37 +94,4 @@ class TextureViewPreview extends PreviewImpl {
}
}
void configureTransform() {
Matrix matrix = new Matrix();
if (mDisplayOrientation % 180 == 90) {
final int width = getWidth();
final int height = getHeight();
// Rotate the camera preview when the screen is landscape.
matrix.setPolyToPoly(
new float[]{
0.f, 0.f, // top left
width, 0.f, // top right
0.f, height, // bottom left
width, height, // bottom right
}, 0,
mDisplayOrientation == 90 ?
// Clockwise
new float[]{
0.f, height, // top left
0.f, 0.f, // top right
width, height, // bottom left
width, 0.f, // bottom right
} : // mDisplayOrientation == 270
// Counter-clockwise
new float[]{
width, 0.f, // top left
width, height, // top right
0.f, 0.f, // bottom left
0.f, height, // bottom right
}, 0,
4);
}
mTextureView.setTransform(matrix);
}
}

@ -12,7 +12,6 @@
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Loading…
Cancel
Save