Android 为什么摄像机拍摄的视频方向会自动改变?
我想打开相机并在SurfaceView上设置预览,以便录制视频。录制完成后,我将尝试其他活动,并在视频视图中设置捕获的视频URI。但我的问题是,视频方向会自动更改,这与相机预览不同 这是摄像头预览,希望视频捕获显示完全相同: 但最终捕获的视频显示如下: 如您所见,视频以横向模式显示,而预览以纵向模式显示 这是我打开相机时创建表面的代码 所以我的问题是: 1导致捕获的视频方向旋转180度的问题是什么 2如何拍摄与曲面视图中的摄影机预览方向相同的视频Android 为什么摄像机拍摄的视频方向会自动改变?,android,video,camera,surfaceview,android-videoview,Android,Video,Camera,Surfaceview,Android Videoview,我想打开相机并在SurfaceView上设置预览,以便录制视频。录制完成后,我将尝试其他活动,并在视频视图中设置捕获的视频URI。但我的问题是,视频方向会自动更改,这与相机预览不同 这是摄像头预览,希望视频捕获显示完全相同: 但最终捕获的视频显示如下: 如您所见,视频以横向模式显示,而预览以纵向模式显示 这是我打开相机时创建表面的代码 所以我的问题是: 1导致捕获的视频方向旋转180度的问题是什么 2如何拍摄与曲面视图中的摄影机预览方向相同的视频 提前感谢。有些设备在拍摄后会旋转图像和视频。使用
提前感谢。有些设备在拍摄后会旋转图像和视频。使用以下代码了解图像是否已旋转:
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;
}
一些像三星这样的设备在拍摄后会旋转图像。但是通过代码我们可以得到旋转,如果需要的话可以旋转
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);
所以我需要把videoUri放入getRotation?所以第二个函数中的旋转是第一个函数返回的值??第二个函数我不理解..因为现在我正在处理视频,视频也有位图??是的,第二个函数中的旋转是第一个函数返回的值。不,视频没有位图,也可以自定义视频。兄弟,但这是视频,不是图像,还是一样吗??
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;
}
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);