19
19
import android .os .Environment ;
20
20
import android .provider .MediaStore ;
21
21
import android .util .Size ;
22
+ import android .view .OrientationEventListener ;
22
23
import android .view .ScaleGestureDetector ;
24
+ import android .view .Surface ;
23
25
import android .view .View ;
24
26
import android .view .ViewGroup ;
25
27
import android .view .WindowManager ;
@@ -112,6 +114,8 @@ public class TiCameraXActivity extends TiBaseActivity implements CameraXConfig.P
112
114
private ProcessCameraProvider cameraProvider ;
113
115
static int targetResolutionWidth = -1 ;
114
116
static int targetResolutionHeight = -1 ;
117
+ private OrientationEventListener orientationEventListener ;
118
+ private int lastDisplayOrientation = 0 ;
115
119
116
120
public static void takePicture ()
117
121
{
@@ -417,6 +421,30 @@ private void startCamera()
417
421
int rotation = getWindowManager ().getDefaultDisplay ().getRotation ();
418
422
419
423
Activity activity = TiApplication .getAppCurrentActivity ();
424
+
425
+ orientationEventListener = new OrientationEventListener (activity )
426
+ {
427
+ @ Override
428
+ public void onOrientationChanged (int orientation )
429
+ {
430
+ if (orientationEventListener == null || orientation == ORIENTATION_UNKNOWN ) {
431
+ return ;
432
+ }
433
+ int rotation = getWindowManager ().getDefaultDisplay ().getRotation ();
434
+ if (lastDisplayOrientation != rotation ) {
435
+ imageCapture .setTargetRotation (rotation );
436
+ lastDisplayOrientation = rotation ;
437
+ }
438
+
439
+ }
440
+ };
441
+ if (orientationEventListener .canDetectOrientation ()) {
442
+ orientationEventListener .enable ();
443
+ } else {
444
+ orientationEventListener .disable ();
445
+ orientationEventListener = null ;
446
+ }
447
+
420
448
ListenableFuture cameraProviderFuture = ProcessCameraProvider .getInstance (activity );
421
449
cameraProviderFuture .addListener (() -> {
422
450
try {
@@ -493,8 +521,13 @@ private void startCamera()
493
521
}
494
522
495
523
if (targetResolutionWidth != -1 && targetResolutionHeight != -1 ) {
496
- imageCaptureBuilder .setTargetResolution (
497
- new Size (targetResolutionWidth , targetResolutionHeight ));
524
+ if (rotation == Surface .ROTATION_0 || rotation == Surface .ROTATION_180 ) {
525
+ imageCaptureBuilder .setTargetResolution (
526
+ new Size (targetResolutionWidth , targetResolutionHeight ));
527
+ } else {
528
+ imageCaptureBuilder .setTargetResolution (
529
+ new Size (targetResolutionHeight , targetResolutionWidth ));
530
+ }
498
531
}
499
532
500
533
imageCapture = imageCaptureBuilder .build ();
@@ -573,6 +606,11 @@ protected void onDestroy()
573
606
if (camera != null ) {
574
607
camera = null ;
575
608
}
609
+ if (orientationEventListener != null ) {
610
+ orientationEventListener .disable ();
611
+ orientationEventListener = null ;
612
+ }
613
+
576
614
// Destroy this activity.
577
615
super .onDestroy ();
578
616
}
@@ -581,6 +619,10 @@ protected void onDestroy()
581
619
public void finish ()
582
620
{
583
621
overlayProxy = null ;
622
+ if (orientationEventListener != null ) {
623
+ orientationEventListener .disable ();
624
+ orientationEventListener = null ;
625
+ }
584
626
super .finish ();
585
627
}
586
628
0 commit comments