7
7
import android .media .MediaRecorder ;
8
8
import android .os .Build ;
9
9
import android .os .Handler ;
10
- import android .util .Log ;
11
10
import android .support .annotation .Nullable ;
11
+ import android .util .Log ;
12
12
import android .view .MotionEvent ;
13
13
import android .view .SurfaceHolder ;
14
14
import android .view .View ;
17
17
import java .io .IOException ;
18
18
import java .util .ArrayList ;
19
19
import java .util .HashSet ;
20
- import java .util .Iterator ;
21
20
import java .util .List ;
22
21
import java .util .Set ;
23
22
import java .util .TreeSet ;
@@ -51,6 +50,7 @@ public class Camera1 extends CameraImpl {
51
50
private boolean capturingImage = false ;
52
51
53
52
private int mDisplayOrientation ;
53
+ private int mDeviceOrientation ;
54
54
55
55
@ Facing
56
56
private int mFacing ;
@@ -85,7 +85,6 @@ public void onSurfaceChanged() {
85
85
});
86
86
87
87
mCameraInfo = new Camera .CameraInfo ();
88
-
89
88
}
90
89
91
90
// CameraImpl:
@@ -106,8 +105,9 @@ void stop() {
106
105
}
107
106
108
107
@ Override
109
- void setDisplayOrientation (int displayOrientation ) {
108
+ void setDisplayOrientation (int displayOrientation , int deviceOrientation ) {
110
109
this .mDisplayOrientation = displayOrientation ;
110
+ this .mDeviceOrientation = deviceOrientation ;
111
111
}
112
112
113
113
@ Override
@@ -221,6 +221,11 @@ void captureImage() {
221
221
// Set boolean to wait for image callback
222
222
capturingImage = true ;
223
223
224
+ // Set the captureRotation right before taking a picture so it's accurate
225
+ int captureRotation = calculateCaptureRotation ();
226
+ mCameraParameters .setRotation (captureRotation );
227
+ mCamera .setParameters (mCameraParameters );
228
+
224
229
mCamera .takePicture (null , null , null ,
225
230
new Camera .PictureCallback () {
226
231
@ Override
@@ -260,6 +265,7 @@ void startVideo() {
260
265
initMediaRecorder ();
261
266
prepareMediaRecorder ();
262
267
mMediaRecorder .start ();
268
+ mCameraListener .onVideoStarted ();
263
269
}
264
270
265
271
@ Override
@@ -351,6 +357,14 @@ boolean isCameraOpened() {
351
357
return mCamera != null ;
352
358
}
353
359
360
+ @ Override
361
+ boolean frontCameraOnly () {
362
+ Camera .CameraInfo cameraInfo = new Camera .CameraInfo ();
363
+ Camera .getCameraInfo (0 , cameraInfo );
364
+ boolean isFrontCameraOnly = (Camera .getNumberOfCameras () == 1 && cameraInfo .facing == Camera .CameraInfo .CAMERA_FACING_FRONT );
365
+ return isFrontCameraOnly ;
366
+ }
367
+
354
368
@ Nullable
355
369
@ Override
356
370
CameraProperties getCameraProperties () {
@@ -399,29 +413,39 @@ private void releaseCamera() {
399
413
400
414
private int calculatePreviewRotation () {
401
415
if (mCameraInfo .facing == Camera .CameraInfo .CAMERA_FACING_FRONT ) {
402
- return ((mCameraInfo .orientation - mDisplayOrientation ) + 360 + 180 ) % 360 ;
416
+ int result = (mCameraInfo .orientation + mDisplayOrientation ) % 360 ;
417
+ return (360 - result ) % 360 ;
403
418
} else {
404
419
return (mCameraInfo .orientation - mDisplayOrientation + 360 ) % 360 ;
405
420
}
406
421
}
407
422
408
423
private int calculateCaptureRotation () {
409
- int previewRotation = calculatePreviewRotation () ;
424
+ int captureRotation = 0 ;
410
425
if (mCameraInfo .facing == Camera .CameraInfo .CAMERA_FACING_FRONT ) {
411
- //Front is flipped
412
- return (previewRotation + 180 + 2 *mDisplayOrientation + 720 ) %360 ;
413
- } else {
414
- return previewRotation ;
426
+ captureRotation = (mCameraInfo .orientation + mDisplayOrientation ) % 360 ;
427
+ } else { // back-facing camera
428
+ captureRotation = (mCameraInfo .orientation - mDisplayOrientation + 360 ) % 360 ;
429
+ }
430
+
431
+ // Accommodate for any extra device rotation relative to fixed screen orientations
432
+ // (e.g. activity fixed in portrait, but user took photo/video in landscape)
433
+ if (mCameraInfo .facing == Camera .CameraInfo .CAMERA_FACING_FRONT ) {
434
+ captureRotation = ((captureRotation - (mDisplayOrientation - mDeviceOrientation )) + 360 ) % 360 ;
435
+ } else { // back-facing camera
436
+ captureRotation = (captureRotation + (mDisplayOrientation - mDeviceOrientation ) + 360 ) % 360 ;
415
437
}
438
+
439
+ return captureRotation ;
416
440
}
417
441
418
442
private void adjustCameraParameters () {
419
443
initResolutions ();
420
444
421
- boolean invertPreviewSizes = mDisplayOrientation % 180 ! = 0 ;
445
+ boolean invertPreviewSizes = ( mCameraInfo . orientation + mDisplayOrientation ) % 180 = = 0 ;
422
446
mPreview .setTruePreviewSize (
423
- invertPreviewSizes ? getPreviewResolution ().getHeight () : getPreviewResolution ().getWidth (),
424
- invertPreviewSizes ? getPreviewResolution ().getWidth () : getPreviewResolution ().getHeight ()
447
+ invertPreviewSizes ? getPreviewResolution ().getHeight () : getPreviewResolution ().getWidth (),
448
+ invertPreviewSizes ? getPreviewResolution ().getWidth () : getPreviewResolution ().getHeight ()
425
449
);
426
450
427
451
mCameraParameters .setPreviewSize (
@@ -483,7 +507,7 @@ private void initMediaRecorder() {
483
507
484
508
mVideoFile = new File (mPreview .getView ().getContext ().getExternalFilesDir (null ), "video.mp4" );
485
509
mMediaRecorder .setOutputFile (mVideoFile .getAbsolutePath ());
486
- mMediaRecorder .setOrientationHint (calculatePreviewRotation ());
510
+ mMediaRecorder .setOrientationHint (calculateCaptureRotation ());
487
511
mMediaRecorder .setVideoSize (mCaptureSize .getWidth (), mCaptureSize .getHeight ());
488
512
}
489
513
0 commit comments