为什么摄像头的视频捕捉方向会自动改变?

时间:2017-11-07 06:21:16

标签: android video camera surfaceview android-videoview

我想打开camera并在SurfaceView上设置预览,以便录制视频。一旦完成录制,意图转到另一个活动,并将捕获的视频URI设置为{{1但我的问题是,视频方向会自动更改,这与相机预览不同。

这是相机预览(我希望视频捕捉看起来完全一样):

enter image description here

但最终拍摄的视频如下所示:

enter image description here

如您所见,视频以横向模式显示,而预览则处于纵向模式。

这是我在VideoView

surfaceCreated的代码
Camera

以下是我准备录像机的方法:

@Override
public void surfaceCreated(SurfaceHolder holder) {
    releaseCamera();
    cameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
    try{
        mCamera = Camera.open(cameraId);
    }catch (RuntimeException ex){
        ex.printStackTrace();
    }

    Camera.Parameters parameters;
    parameters = mCamera.getParameters();

    List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
    List<Camera.Size> mSupportedVideoSizes = parameters.getSupportedVideoSizes();
    optimalSize = getOptimalVideoSize(mSupportedVideoSizes,
            mSupportedPreviewSizes, videoSurface.getWidth(), videoSurface.getHeight());

    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    profile.videoFrameWidth = optimalSize.width;
    profile.videoFrameHeight = optimalSize.height;

    parameters.setPreviewFrameRate(20);
    parameters.setPreviewSize(profile.videoFrameWidth,profile.videoFrameHeight);
    mCamera.setParameters(parameters);
    mCamera.setDisplayOrientation(90);

    try{
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
        inPreview = true;
    }catch (Exception e){
        e.printStackTrace();
    }
}

所以我的问题是:

1)导致视频捕获方向旋转180度的问题是什么?

2)如何在表面视图中拍摄与摄影预览相同的视频?

提前致谢。

2 个答案:

答案 0 :(得分:1)

有些设备会在拍摄后旋转图像和视频。使用下面的代码来了解图像是否旋转:

public static int getRotation(Context context, Uri imageUri) {
    String[] columns = {MediaStore.Images.Media.ORIENTATION};
    Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
    if (cursor == null) return 0;

    cursor.moveToFirst();

    int orientationColumnIndex = cursor.getColumnIndex(columns[0]);
    return cursor.getInt(orientationColumnIndex);
}

如果结果为0则没有旋转,否则您需要将其旋转为原始。使用以下代码:

public static Bitmap rotate(Bitmap bm, int rotation) {
    if (rotation != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        Bitmap bmOut = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        return bmOut;
    }
    return bm;
}

答案 1 :(得分:0)

一些像三星这样的设备在捕获后旋转图像。但是通过代码我们可以获得旋转并在需要时旋转。

public static int getCameraPhotoOrientation(String imagePath) {
            int rotate = 0;
            try {
                File imageFile = new File(imagePath);
                ExifInterface exif = new ExifInterface(
                        imageFile.getAbsolutePath());
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rotate;
        }


we can use this method as below


    //Rotate if necessary
int rotate=AppUtil.getCameraPhotoOrientation(imagePath);
Matrix matrix = new Matrix();
matrix.postRotate(rotate);

// create a file object from path 
File imageFile = new File(path);

//handle Out of memory error
Bitmap bmp=AppUtil.decodeFile(imageFile,100,100);

//Rotate BMP
Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);