MediaRecorder以纵向模式捕获视频

时间:2012-08-16 18:19:35

标签: android video orientation mediarecorder

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

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

为了检测设备更改的方向,我在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,其中camera设置为适当的DisplayRotation。

问题是如何确定视频是以景观模式还是以纵向模式捕获的。因为我得到的所有视频都是横向拍摄的。它不依赖于setDisplayOrientation,它只影响预览过程。

同时探索这个问题我注意到如果要使用标准相机应用程序,它会将特殊标签写入视频文件(在MediaInfo中看到):旋转:90 用于拍摄肖像。

但是MediaRecorder类没有。

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

2 个答案:

答案 0 :(得分:28)

发现它! 实际上,您可以更改预览,可以标记视频,但无法实际更改视频...(可能是速度问题或其他)

camera.setDisplayOrientation(90);

要旋转预览,请

recorder.setOrientationHint(90);

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

所以你要做的就是

            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();

希望它有所帮助; - )

答案 1 :(得分:7)

camera.setDisplayOrientation(90)无法在所有设备中使用。以下解决方案在不同设备中完美地工作,并且还处理棉花糖运行时许可。

  

参见setCameraRotation方法

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    public static int rotate;
    private Context mContext;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mContext = context;
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            // create the surface and start camera preview
            if (mCamera != null) {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
        } catch (IOException e) {
            Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void refreshCamera(Camera camera) {
        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }
        // stop preview before making changes
        stopPreview();
        // set preview size and make any resize, rotate or
        // reformatting changes here
        setCamera(camera);

        // start preview with new settings
        startPreview();
    }

    public void stopPreview(){
        try {
            if(mCamera != null)
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
            e.printStackTrace();
        }
    }

    public void startPreview(){
        try {
            if(mCamera != null) {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
            }else{
                Log.d(VIEW_LOG_TAG, "Error starting camera preview: " );
            }
        } catch (Exception e) {
            Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        refreshCamera(mCamera);
    }

    public void setCamera(Camera camera) {
        //method to set a camera instance
        mCamera = camera;
        /**
         * add camera orientation and display rotation according to screen landscape or portrait
         */
        setCameraRotation();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if(mCamera != null){
            mCamera.release();
        }

    }

    public void setCameraRotation() {
        try {

            Camera.CameraInfo camInfo = new Camera.CameraInfo();

            if (VideoCaptureActivity.cameraId == 0)
                Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, camInfo);
            else
                Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_FRONT, camInfo);
            int cameraRotationOffset = camInfo.orientation;
            // ...

            Camera.Parameters parameters = mCamera.getParameters();


            int rotation = ((Activity)mContext).getWindowManager().getDefaultDisplay().getRotation();
            int degrees = 0;
            switch (rotation) {
                case Surface.ROTATION_0:
                    degrees = 0;
                    break; // Natural orientation
                case Surface.ROTATION_90:
                    degrees = 90;
                    break; // Landscape left
                case Surface.ROTATION_180:
                    degrees = 180;
                    break;// Upside down
                case Surface.ROTATION_270:
                    degrees = 270;
                    break;// Landscape right
            }
            int displayRotation;
            if (camInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                displayRotation = (cameraRotationOffset + degrees) % 360;
                displayRotation = (360 - displayRotation) % 360; // compensate
                // the
                // mirror
            } else { // back-facing
                displayRotation = (cameraRotationOffset - degrees + 360) % 360;
            }

            mCamera.setDisplayOrientation(displayRotation);


            if (camInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                rotate = (360 + cameraRotationOffset + degrees) % 360;
            } else {
                rotate = (360 + cameraRotationOffset - degrees) % 360;
            }

            parameters.set("orientation", "portrait");
            parameters.setRotation(rotate);
            mCamera.setParameters(parameters);

        } catch (Exception e) {

        }
    }



}

现在准备好正确旋转的录像机,以便录制的视频以正确的方向播放。

  

mediaRecorder.setOrientationHint(CameraPreview.rotate);

private boolean prepareMediaRecorder(){

    mediaRecorder = new MediaRecorder();

    mCamera.unlock();
    mediaRecorder.setCamera(mCamera);

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOrientationHint(CameraPreview.rotate);

    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

    mediaRecorder.setOutputFile(filePath);
    mediaRecorder.setMaxDuration(15000); // Set max duration 15 sec.
    mediaRecorder.setMaxFileSize(10000000); // Set max file size 1M

    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;

}

您可以下载完整的示例https://github.com/umesh-kushwaha/Android_Video_Recording_portrait