13

我使用下面的代码录制了一个视频,它可以完美地录制,但是当它播放视频时,它会颠倒播放。

mrec.setOrientationHint(180)我之前尝试过设置,mrec.prepare();但没有用。有什么提示吗?

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

/**
 * @author SANA HASSAN
 */
public class CameraSurfaceView extends Activity {

    private Preview mPreview;
    private MediaRecorder mrec = new MediaRecorder();
    private int cameraId = 0;
    private Camera mCamera;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        mPreview = new Preview(this);
        setContentView(mPreview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Start");
        menu.add(0, 1, 0, "Stop");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                try {
                    startRecording();
                } 
                catch (Exception e) {
                    e.printStackTrace();
                    mrec.release();
                }
                break;

            case 1: 
                mrec.stop();
                mrec.release();
                mrec = null;
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void startRecording() throws IOException  {

        mrec = new MediaRecorder();
        mrec.setCamera(mCamera);
        mCamera.unlock();
        File directory = new File(Environment.getExternalStorageDirectory()+"/NICUVideos");
        directory.mkdirs();
        mrec.setAudioSource( MediaRecorder.AudioSource.MIC);
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mrec.setOutputFile(Environment.getExternalStorageDirectory()+"/NICUVideos/"+System.currentTimeMillis()+".mp4"); 
        mrec.setPreviewDisplay(mPreview.getHolder().getSurface());
        mrec.setVideoSize(640, 480);

        Method[] methods = mrec.getClass().getMethods();
        for (Method method: methods){
            try{
                if(method.getName().equals("setAudioEncodingBitRate")){
                    method.invoke(mrec, 12200);
                }
                else if(method.getName().equals("setVideoEncodingBitRate")){
                    method.invoke(mrec, 800000);
                }
                else if(method.getName().equals("setAudioSamplingRate")){
                    method.invoke(mrec, 8000);
                }
                else if(method.getName().equals("setVideoFrameRate")){
                    method.invoke(mrec, 20);
                }
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            } 
            catch (IllegalAccessException e) {
                e.printStackTrace();
            } 
            catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mrec.setMaxDuration(60000); // 60 seconds
        mrec.setMaxFileSize(10000000); // Approximately 10 megabytes
        mrec.prepare();
        mrec.start();
    }

    protected void stopRecording() {
        mrec.stop();
        mrec.release();
        mCamera.release();
    }

    class Preview extends SurfaceView implements SurfaceHolder.Callback {
        SurfaceHolder mHolder;
        Activity activity;

        Preview(Activity activity) {
            super(activity);
            mHolder = getHolder();
            mHolder.addCallback(this);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        public void surfaceCreated(SurfaceHolder holder) {

            Camera.CameraInfo info=new Camera.CameraInfo();
            for (int i=0; i < Camera.getNumberOfCameras(); i++) {
                Camera.getCameraInfo(i, info);
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    mCamera=Camera.open(i);
                    cameraId = i;
                }
            }

            try {
               mCamera.setPreviewDisplay(holder);
            } catch (IOException exception) {
                mCamera.release();
                mCamera = null;
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            setCameraDisplayOrientation(mCamera);
            mCamera.startPreview();
        }

        public void setCameraDisplayOrientation(Camera camera) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, info);

            int rotation = CameraSurfaceView.this.getWindowManager().getDefaultDisplay().getRotation();
            int degrees = 0;
            switch (rotation) {
                case Surface.ROTATION_0: degrees = 0; break;
                case Surface.ROTATION_90: degrees = 90; break;
                case Surface.ROTATION_180: degrees = 180; break;
                case Surface.ROTATION_270: degrees = 270; break;
            }

            int result;
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360;  // compensate the mirror
            } else {  // back-facing
                result = (info.orientation - degrees + 360) % 360;
            }
            Log.d(Vars.TAG, "Result = "+result);
            camera.setDisplayOrientation(result);
        }
    }

}
4

10 回答 10

15

这个问题是由于Android通过设置一些元数据而不是实际旋转视频来处理旋转,并且一些播放软件然后忽略了该设置。

文档中所述

请注意,某些视频播放器可能会选择在播放期间忽略视频中的合成矩阵。

您的选择是使用不同的播放软件来理解正在设置的元数据,或者在将视频录制到正确的方向后重新编码。从您的描述中不清楚哪一个是您的情况下更好的解决方案。

于 2012-07-23T21:58:37.360 回答
7

这应该在方法之前mrec.prepare();调用

setOrientationHint(degrees);

关联

编辑:试试mCamera.setDisplayOrientation(degrees);

0 for landscape
90 for portrait
180 & 270 don't work very well and give weird results.

一些较老的播放器和编码器不解释此标志,这就是视频播放颠倒的原因。

于 2012-07-11T05:49:35.697 回答
2

我知道你的问题,

视频使用Media RecorderCamera,所以你需要rotate Media Recorder。使用以下代码应该可以解决您的问题。

/**
 * 
 * @param mMediaRecorder
 * @return
 */
public static MediaRecorder rotateBackVideo(MediaRecorder mMediaRecorder) {
    /**
     * Define Orientation of video in here,
     * if in portrait mode, use value = 90,
     * if in landscape mode, use value = 0
     */
    switch (CustomCamera.current_orientation) {
        case 0:
            mMediaRecorder.setOrientationHint(90);
            break;
        case 90:
            mMediaRecorder.setOrientationHint(180);
            break;
        case 180:
            mMediaRecorder.setOrientationHint(270);
            break;
        case 270:
            mMediaRecorder.setOrientationHint(0);
            break;
    }

    return mMediaRecorder;
}

应该在prepare()方法之前添加:

// Step 5: Set the preview output
    /**
     * Define Orientation of image in here,
     * if in portrait mode, use value = 90,
     * if in landscape mode, use value = 0
     */
    CustomCamera.mMediaRecorder = Utils.rotateBackVideo(CustomCamera.mMediaRecorder);
    CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

谢谢

于 2015-03-31T10:24:27.273 回答
2

我有同样的问题,我注意到相机预览方向角度和录制视频角度不一样。所以我使用这种方法来改变视频录制的方向:

public static int getVideoOrientationAngle(Activity activity, int cameraId) { //The param cameraId is the number of the camera.
    int angle;
    Display display = activity.getWindowManager().getDefaultDisplay();
    int degrees = display.getRotation();
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    switch (degrees) {
        case Surface.ROTATION_0: 
            angle = 90; 
            break;
        case Surface.ROTATION_90:
            angle = 0;
            break;
        case Surface.ROTATION_180:
            angle = 270;
            break;
        case Surface.ROTATION_270:
            angle = 180;
            break;
        default:
            angle = 90;
            break;
    }
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
        angle = (angle + 180) % 360;

    return angle;
}

并且用于更改相机预览的方向:

 public static int setCameraDisplayOrientation(Activity activity,
                                              int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
    return result;
}

请注意,了解相机是前置还是后置很重要。

于 2017-07-28T08:40:43.183 回答
1

使用OrientationEventListener并在此处旋转设备代码时跟踪旋转值。此代码将旋转应用于相机,但对于录制,您需要将旋转应用于MediaRecorder。当您开始录制mMediaRecorder.setOrientationHint(rotation)之前 mMediaRecorder.prepare()。这解决了我的问题。

于 2016-05-30T13:34:12.520 回答
0

最后我发现摩托罗拉手机在播放以纵向模式录制的视频时存在问题。

为了克服视频的旋转,我采用的最佳解决方案是将视频上传到服务器并使用命令运行ffmpeg onffmpeg -i input.mp4 -c:v mpeg4 -c:a copy -c:s copy -vf "transpose=2" output.mp4

如果您觉得还有其他方法,请告诉我。

于 2012-07-26T02:27:57.937 回答
0

另一种解决方案是旋转您的活动,使其方向与传感器方向相同。即后置摄像头为横向,前置摄像头为倒置人像。这个顺便说一句不会修复前置摄像头的镜面效果。另一个困难是您必须以遵循活动方向的方式在那些旋转的活动中实现您的 UI。

于 2013-04-23T16:16:17.647 回答
0

旋转您的 MediaRecorder 如下所示,对应于您在摄像头显示方向中用于前置摄像头视频录制的度数

Display display = ((WindowManager)getContext().getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        parameters.setPreviewSize(height, width);
        if(display.getRotation() == Surface.ROTATION_0)
        {

            mCamera.setDisplayOrientation(90);
            mMediaRecorder.setOrientationHint(270);
        }

        if(display.getRotation() == Surface.ROTATION_90)
        {
            mMediaRecorder.setOrientationHint(180);
        }

        if(display.getRotation() == Surface.ROTATION_180)
        {
            mMediaRecorder.setOrientationHint(270);
        }

        if(display.getRotation() == Surface.ROTATION_270)
        {
            mCamera.setDisplayOrientation(180);
            mMediaRecorder.setOrientationHint(0);
        }
于 2016-11-11T14:18:34.547 回答
0

这是自定义人像相机的代码,将设置图片和视频的正确旋转:


private OrientationEventListener orientationEventListener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...
    orientationEventListener = new OrientationEventListener(this) {
        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == ORIENTATION_UNKNOWN) return;

            flashButton.setRotation(-(orientation));
            cameraButton.setRotation(-(orientation));

            if (camera != null) {
                Parameters parameters = camera.getParameters();
                CameraInfo info = new CameraInfo();
                Camera.getCameraInfo(selectedCamera, info);
                orientation = (orientation + 45) / 90 * 90;
                int rotation = 0;
                if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                    rotation = (info.orientation - orientation + 360) % 360;
                } else {  // back-facing camera
                    rotation = (info.orientation + orientation) % 360;
                }
                parameters.setRotation(rotation);
                if (!isRecording) {
                    mediaRecorder.setOrientationHint(rotation);
                }

                camera.setParameters(parameters);
            }
        }
    };
}


@Override
protected void onResume() {
    super.onResume();
    //...
    orientationEventListener.enable();
}

@Override
protected void onPause() {
    super.onPause();
    orientationEventListener.disable();
    //...
}

纵向测试。请记住将其放入清单中以测试代码。我不知道是否在景观中工作。

<activity android:name=".activities.CameraActivity"
          android:screenOrientation="portrait">
于 2016-11-18T13:49:02.197 回答
0

对于纵向模式,设置您的 mediaRecorder.setOrientationHint(90); 度数与您的相机方向相同 myCamera.setDisplayOrientation(90);

于 2017-09-01T12:17:10.290 回答