Video plays upside down when recorded from Front Camera in Android

 ̄綄美尐妖づ 提交于 2020-01-05 18:06:23

问题


I am doing android Video Camera Application. It should have capability of recording with back and front camera. I am done with back camera and everything is fine. But when I record with front camera, video plays upside down. I have to play the videos recorded in my application itself. I am using VideoView, How to set orientation or make it play correctly?. Video when played in default media player also playing upside down. Tried sending the video to iPhone and checked but still it plays upside down. I have setOrientationHint to MediaRecorder but no solution.

All My Code is similar to agargenta's Video Capture Demo in which I have done customization.

Issues seems to be strange and I am struck.

this.mediaRecorder = new MediaRecorder();
        this.mediaRecorder.setCamera(this.mCamera);
        this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        if (Build.VERSION.SDK_INT <= 10) {
            CamcorderProfile camcorderProfile = CamcorderProfile
                    .get(CamcorderProfile.QUALITY_HIGH);
            camcorderProfile.videoFrameWidth = 320;
            camcorderProfile.videoFrameHeight = 480;
            // camcorderProfile.videoFrameRate = 15;
            camcorderProfile.videoCodec = MediaRecorder.VideoEncoder.H264;
            // camcorderProfile.audioCodec = MediaRecorder.AudioEncoder.DEFAULT;
            camcorderProfile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
            this.mediaRecorder.setProfile(camcorderProfile);
mediaRecorder.setOrientationHint(90);
        } else {
            if (tgbSwitchCamera.isChecked()) {
                mediaRecorder
                        .setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                // mediaRecorder.setVideoSize(320, 480);
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                CameraInfo cameraInfo = new CameraInfo();
                Camera.getCameraInfo(CameraInfo.CAMERA_FACING_FRONT, cameraInfo);
                rotation = (cameraInfo.orientation - 180 + 360) % 360;
                mediaRecorder.setOrientationHint(rotation);

            } else {
                CamcorderProfile camcorderProfile = CamcorderProfile
                        .get(CamcorderProfile.QUALITY_LOW);
                camcorderProfile.videoFrameWidth = 640;
                camcorderProfile.videoFrameHeight = 480;
                camcorderProfile.videoCodec = MediaRecorder.VideoEncoder.H264;
                // camcorderProfile.audioCodec =
                // MediaRecorder.AudioEncoder.DEFAULT;
                camcorderProfile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
                this.mediaRecorder.setProfile(camcorderProfile);
mediaRecorder.setOrientationHint(90);
            }
        }
        this.mediaRecorder.setOutputFile(this.initFile().getAbsolutePath());
        this.mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

        try {
            this.mediaRecorder.prepare();

            // start the actual recording
            // throws IllegalStateException if not prepared
            Handler h = new Handler();
            h.postDelayed(new Runnable() {

                @Override
                public void run() {
                    this.mediaRecorder.start();
                    pgv.setStart(true);
                    pgv.invalidate();
                    CountDownTimer cdt = new CountDownTimer(5000, 1000) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                        }

                        @Override
                        public void onFinish() {
                            if (isRecording) {
                                handler.removeMessages(STOP);
                                stopRecording();
                            }
                        }
                    };
                    cdt.start();
                }
            }, 1000);

            // enable the stop button by indicating that we are recording
        } catch (Exception e) {
            Toast.makeText(this, "cannot record", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            this.releaseMediaRecorder();
        }

回答1:


This line will specifically rotate the video 180 degrees:

rotation = (cameraInfo.orientation - 180 + 360) % 360;

Unless you have a reason for doing this that I'm not seeing, that's why your video is upside-down.



来源:https://stackoverflow.com/questions/17863359/video-plays-upside-down-when-recorded-from-front-camera-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!