@@ -4,8 +4,6 @@ import android.annotation.SuppressLint
4
4
import android.app.AlertDialog
5
5
import android.content.Context
6
6
import android.content.SharedPreferences
7
- import android.hardware.camera2.CameraMetadata
8
- import android.hardware.camera2.CaptureRequest
9
7
import android.net.Uri
10
8
import android.os.Build
11
9
import android.provider.MediaStore
@@ -18,9 +16,9 @@ import android.view.animation.Animation
18
16
import android.view.animation.LinearInterpolator
19
17
import android.widget.Button
20
18
import androidx.annotation.StringRes
21
- import androidx.camera.camera2.interop.Camera2Interop
22
19
import androidx.camera.core.AspectRatio
23
20
import androidx.camera.core.Camera
21
+ import androidx.camera.core.CameraInfo
24
22
import androidx.camera.core.CameraSelector
25
23
import androidx.camera.core.ImageAnalysis
26
24
import androidx.camera.core.ImageCapture
@@ -218,6 +216,10 @@ class CamConfig(private val mActivity: MainActivity) {
218
216
219
217
var lastCapturedItem: CapturedItem ? = null
220
218
219
+ private var frontCameraInfo : CameraInfo ? = null
220
+
221
+ private var rearCameraInfo : CameraInfo ? = null
222
+
221
223
init {
222
224
if (mActivity !is SecureActivity ) {
223
225
CapturedItems .init (mActivity, this )
@@ -429,7 +431,7 @@ class CamConfig(private val mActivity: MainActivity) {
429
431
430
432
var enableEIS: Boolean
431
433
get() {
432
- return mActivity.settingsDialog.enableEISToggle.isChecked
434
+ return isStabilizationSupported() && mActivity.settingsDialog.enableEISToggle.isChecked
433
435
}
434
436
set(value) {
435
437
val editor = commonPref.edit()
@@ -532,6 +534,10 @@ class CamConfig(private val mActivity: MainActivity) {
532
534
camera!! .cameraInfo.isZslSupported
533
535
}
534
536
537
+ fun isStabilizationSupported () : Boolean {
538
+ return Recorder .getVideoCapabilities(getCurrentCameraInfo()).isStabilizationSupported
539
+ }
540
+
535
541
fun shouldShowGyroscope (): Boolean {
536
542
return isInPhotoMode && gSuggestions
537
543
}
@@ -894,6 +900,11 @@ class CamConfig(private val mActivity: MainActivity) {
894
900
startCamera(true )
895
901
}
896
902
903
+ private fun getCurrentCameraInfo () : CameraInfo {
904
+ return if (lensFacing == CameraSelector .LENS_FACING_BACK ) rearCameraInfo!!
905
+ else frontCameraInfo!!
906
+ }
907
+
897
908
fun toggleCameraSelector () {
898
909
899
910
// Manually switch to the opposite lens facing
@@ -937,6 +948,12 @@ class CamConfig(private val mActivity: MainActivity) {
937
948
return
938
949
}
939
950
951
+ // Select a single camera for front/rear facing
952
+ for (cameraInfo in cameraProvider!! .availableCameraInfos) {
953
+ if (cameraInfo.lensFacing == CameraSelector .LENS_FACING_FRONT ) frontCameraInfo = cameraInfo
954
+ else if (cameraInfo.lensFacing == CameraSelector .LENS_FACING_BACK ) rearCameraInfo = cameraInfo
955
+ }
956
+
940
957
// Manually switch to the other lens facing (if the default lens facing isn't
941
958
// supported for the current device)
942
959
if (! isLensFacingSupported(lensFacing)) {
@@ -963,11 +980,11 @@ class CamConfig(private val mActivity: MainActivity) {
963
980
}
964
981
965
982
private fun isLensFacingSupported (lensFacing : Int ) : Boolean {
966
- val tCameraSelector = CameraSelector . Builder ()
967
- .requireLensFacing(lensFacing)
968
- .build()
969
-
970
- return cameraProvider?.hasCamera(tCameraSelector) ? : false
983
+ return when (lensFacing) {
984
+ CameraSelector . LENS_FACING_FRONT -> frontCameraInfo != null
985
+ CameraSelector . LENS_FACING_BACK -> rearCameraInfo != null
986
+ else -> false
987
+ }
971
988
}
972
989
973
990
// Start the camera with latest hard configuration
@@ -992,7 +1009,15 @@ class CamConfig(private val mActivity: MainActivity) {
992
1009
if (mActivity.isDestroyed || mActivity.isFinishing) return
993
1010
994
1011
cameraSelector = CameraSelector .Builder ()
995
- .requireLensFacing(lensFacing)
1012
+ .addCameraFilter {
1013
+ return @addCameraFilter listOf (
1014
+ if (lensFacing == CameraSelector .LENS_FACING_BACK ) {
1015
+ rearCameraInfo
1016
+ } else {
1017
+ frontCameraInfo
1018
+ }
1019
+ )
1020
+ }
996
1021
.build()
997
1022
998
1023
val builder = ImageCapture .Builder ()
@@ -1111,7 +1136,6 @@ class CamConfig(private val mActivity: MainActivity) {
1111
1136
ResolutionSelector .Builder ().setAspectRatioStrategy(aspectRatioStrategy).build()
1112
1137
)
1113
1138
1114
- @androidx.camera.camera2.interop.ExperimentalCamera2Interop
1115
1139
if (isVideoMode) {
1116
1140
previewBuilder.setPreviewStabilizationEnabled(enableEIS)
1117
1141
}
0 commit comments