From 5e039d6539e04879a09cf58d6227fefc513d628c Mon Sep 17 00:00:00 2001 From: Leo Ma Date: Thu, 6 Apr 2017 11:35:31 +0800 Subject: [PATCH] Fix preview rotation bug Signed-off-by: Leo Ma --- app/build.gradle | 2 +- build.gradle | 2 +- .../java/net/ossrs/yasea/SrsCameraView.java | 27 +++++++++++-------- .../main/java/net/ossrs/yasea/SrsEncoder.java | 4 +-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d33f6ae..a901e9c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,6 +25,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:25.2.0' + compile 'com.android.support:appcompat-v7:25.3.1' compile project(path: ':library') } diff --git a/build.gradle b/build.gradle index 8998434..3a56163 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' + classpath 'com.android.tools.build:gradle:2.3.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/library/src/main/java/net/ossrs/yasea/SrsCameraView.java b/library/src/main/java/net/ossrs/yasea/SrsCameraView.java index ca38afb..bf85ba5 100644 --- a/library/src/main/java/net/ossrs/yasea/SrsCameraView.java +++ b/library/src/main/java/net/ossrs/yasea/SrsCameraView.java @@ -38,6 +38,7 @@ public class SrsCameraView extends GLSurfaceView implements GLSurfaceView.Render private int mPreviewWidth; private int mPreviewHeight; private boolean mIsEncoding; + private boolean mIsTorchOn = false; private float mInputAspectRatio; private float mOutputAspectRatio; private float[] mProjectionMatrix = new float[16]; @@ -199,17 +200,19 @@ public class SrsCameraView extends GLSurfaceView implements GLSurfaceView.Render mPreviewOrientation = orientation; Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(mCamId, info); - if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - mPreviewRotation = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? 270 : 90; - } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { - mPreviewRotation = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? 180 : 0; + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { + mPreviewRotation = info.orientation % 360; + mPreviewRotation = (360 - mPreviewRotation) % 360; // compensate the mirror + } else { // back-facing + mPreviewRotation = (info.orientation + 360) % 360; } - } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - mPreviewRotation = 90; - } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { - mPreviewRotation = 0; + } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { + mPreviewRotation = (info.orientation + 90) % 360; + mPreviewRotation = (360 - mPreviewRotation) % 360; // compensate the mirror + } else { // back-facing + mPreviewRotation = (info.orientation + 270) % 360; } } } @@ -293,7 +296,9 @@ public class SrsCameraView extends GLSurfaceView implements GLSurfaceView.Render List supportedFlashModes = params.getSupportedFlashModes(); if (supportedFlashModes != null && !supportedFlashModes.isEmpty()) { if (supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) { - params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); + if (mIsTorchOn) { + params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); + } } else { params.setFlashMode(supportedFlashModes.get(0)); } diff --git a/library/src/main/java/net/ossrs/yasea/SrsEncoder.java b/library/src/main/java/net/ossrs/yasea/SrsEncoder.java index 71128b8..23093e5 100644 --- a/library/src/main/java/net/ossrs/yasea/SrsEncoder.java +++ b/library/src/main/java/net/ossrs/yasea/SrsEncoder.java @@ -97,7 +97,7 @@ public class SrsEncoder { // Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK // Since Y component is quadruple size as U and V component, the stride must be set as 32x - if (!useSoftEncoder && (vOutWidth % 32 != 0 || vOutHeight % 32 != 0)) { + if (!useSoftEncoder && vOutWidth % 32 != 0 || vOutHeight % 32 != 0) { if (vmci.getName().contains("MTK")) { //throw new AssertionError("MTK encoding revolution stride must be 32x"); } @@ -284,7 +284,7 @@ public class SrsEncoder { // Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK // Since Y component is quadruple size as U and V component, the stride must be set as 32x - if (!useSoftEncoder && (vOutWidth % 32 != 0 || vOutHeight % 32 != 0)) { + if (!useSoftEncoder && vOutWidth % 32 != 0 || vOutHeight % 32 != 0) { if (vmci.getName().contains("MTK")) { //throw new AssertionError("MTK encoding revolution stride must be 32x"); }