纵向模式下的MediaRecorder视频捕获 [英] MediaRecorder video capturing in portrait mode

查看:104
本文介绍了纵向模式下的MediaRecorder视频捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作自定义视频应用. Iwork仅使用清单 2.2(API 8)中的设置.

I'm try to make custom video app. Iwork using settings in manifest 2.2 only (API 8).

一切顺利,但我不明白为什么肖像模式视频与lanscape模式视频没有区别.

All goes well but I don't understand why portrait mode video does not differ from lanscape one.

要检测设备更改的方向,我在surfaceChanged()中使用此代码

To make detection of device changed orientation I use this code within surfaceChanged()

        if (mCamera != null) {

        Camera.Parameters p = mCamera.getParameters();

        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // TODO: handle exception
        }

        int previewWidth = 0;
        int previewHeight = 0;

        if (mPreviewSize != null) {
            Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            int rotation = display.getRotation();

            switch (rotation) {
            case Surface.ROTATION_0:
                previewWidth = mPreviewSize.height;
                previewHeight = mPreviewSize.width;
                mCamera.setDisplayOrientation(90);
                break;

            case Surface.ROTATION_90:
                previewWidth = mPreviewSize.width;
                previewHeight = mPreviewSize.height;
                mCamera.setDisplayOrientation(0);
                break;

            case Surface.ROTATION_180:
                previewWidth = mPreviewSize.height;
                previewHeight = mPreviewSize.width;
                mCamera.setDisplayOrientation(270);
                break;

            case Surface.ROTATION_270:
                previewWidth = mPreviewSize.width;
                previewHeight = mPreviewSize.height;
                mCamera.setDisplayOrientation(180);
                break;
            }

            p.setPreviewSize(previewWidth, previewHeight);
            mCamera.setParameters(p);
        }
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    } catch (Exception e) {
        Log.d(TAG, "Cannot start preview.", e);
    }
}

像魅力一样工作.如果我旋转设备表面更改方向,请调用surfaceChanged,将相机设置为适当的DisplayRotation.

Works like a charm. If I rotate device surface change orientation, calling surfaceChanged, where camera is set to appropriate DisplayRotation.

问题是如何稍后确定是以lanscape模式还是纵向模式捕获的视频.当我得到所有视频时,都是以横向拍摄的.它不依赖于setDisplayOrientation,后者仅影响预览过程.

The question is how to determine later if the video captured either in lanscape mode or in portrait one. As I got all the videos are captured in landscape orientation. It does not depend of setDisplayOrientation which affect only preview process.

我也发现了这个问题,我发现如果使用标准的Camera应用程序,它会向视频文件中写入特殊标签(在MediaInfo中可见):旋转:90 用于拍摄人像.

Also exploring the problem I noticed that if to use standard Camera app it writes special tag to video file (seen in MediaInfo): Rotation : 90 for the portrait captured videos.

但是MediaRecorder类没有.

But MediaRecorder class does not.

似乎是问题所在.有人要解决这个问题吗?

Seems that is the problem. Anybody got to solve this?

推荐答案

找到了! 确实,您可以更改预览,可以为视频添加标签,但是无法实际更改视频...(可能是速度问题或其他原因)

Found it ! Indeed, you can change the preview, you can tag the video, but there's no way to actually change the video... (maybe a speed issue or something)

camera.setDisplayOrientation(90);

要旋转预览,然后

recorder.setOrientationHint(90);

要将视频标记为旋转90°,那么手机在阅读时会自动旋转视频.

To tag the video as having a 90° rotation, then the phone will automatically rotate it when reading.

所以您要做的就是

            camera = Camera.open();
        //Set preview with a 90° ortientation
        camera.setDisplayOrientation(90);
        camera.unlock();

        holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        recorder = new MediaRecorder();
        recorder.setCamera(camera);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        recorder.setOutputFile(getVideoFolder()+rnd.nextString()+".mp4");
        recorder.setPreviewDisplay(holder.getSurface());
        //Tags the video with a 90° angle in order to tell the phone how to display it
        recorder.setOrientationHint(90);

        if (recorder != null) {
            try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                Log.e("IllegalStateException", e.toString());
            } catch (IOException e) {
                Log.e("IOException", e.toString());
            }
        }

        recorder.start();

希望有帮助;-)

这篇关于纵向模式下的MediaRecorder视频捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆