检测视频是否以人像/风景拍摄 [英] Detecting if video was taken in portrait/landscape
问题描述
我想确认我所做的确实是正确的方法,因为某些元素的行为异常.
I would like to confirm that what I am doing is indeed the correct way as some elements behave unexpected.
首先,据我了解,我有横向和纵向布局,这样做会自动检测手机是否处于纵向/横向模式:
First, I have a landscape and portrait layout, as I understand, doing this will automatically detect if the phone is in portrait/landscape mode:
- layout
- activity_video_player.xml
- layout-land
- activity_video_player.xml
然后,当用户从图库中选择一个视频时,通过执行以下操作(在OnCreate
内部),我会检查该视频是横向拍摄还是纵向拍摄:
Then when the user selects a video from the gallery, I check if the video was taking in landscape or portrait, by doing this (inside OnCreate
):
int w;
int h;
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(this, videoURI);
String height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
w = Integer.parseInt(width);
h = Integer.parseInt(height);
if (w > h) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
我已经对此进行了测试,并且可以正常工作,但是我注意到我的某些xml元素(播放按钮)放置不正确.
I have tested this and it works fine, but I noticed some of my xml elements (play button) is placed incorrectly.
所以我的应用程序流程是:
So my app flow is:
MainActivity --> SelectvidButton --> Gallery Intent --> VideoPlayActivity
我的问题
这是正确的方法吗?如果是,是否有某些原因导致某些xml元素放置不正确?
Is this the correct way of doing this and if it is, is there any reason why some of the xml elements get placed incorrectly?
我注意到只有在首次启动活动时才会发生这种情况,如果我按后退"按钮并再次选择同一视频,则布局完全像我想要的那样.
I noticed that this only happens when the activity is launched for the first time, if I press the back button and select the same video again, the layout is perfectly like I want it to be.
我还注意到,只有在上一个活动(MainActivity)的方向与所选视频的方向相同时,这种情况才会发生.
I have also noticed that this only happens if the previous activity (MainActivity) was in the same orientation than what the selected video is.
推荐答案
这就是我最终要做的事情.
Here is what I ended up doing.
我不是使用MediaMetadataRetriever
来获取宽度和高度,而是首先从视频文件中检索Bitmap
,然后根据Bitmap
的宽度和高度来设置方向,如下所示:>
Instead of using MediaMetadataRetriever
to get the width and height, I first retrieve a Bitmap
from the video file and then setting the orientation according to the width and hight of the Bitmap
, as shown below:
private void rotateScreen() {
try {
//Create a new instance of MediaMetadataRetriever
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//Declare the Bitmap
Bitmap bmp;
//Set the video Uri as data source for MediaMetadataRetriever
retriever.setDataSource(this, mVideoUri);
//Get one "frame"/bitmap - * NOTE - no time was set, so the first available frame will be used
bmp = retriever.getFrameAtTime();
//Get the bitmap width and height
videoWidth = bmp.getWidth();
videoHeight = bmp.getHeight();
//If the width is bigger then the height then it means that the video was taken in landscape mode and we should set the orientation to landscape
if (videoWidth > videoHeight) {
//Set orientation to landscape
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
//If the width is smaller then the height then it means that the video was taken in portrait mode and we should set the orientation to portrait
if (videoWidth < videoHeight) {
//Set orientation to portrait
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
} catch (RuntimeException ex) {
//error occurred
Log.e("MediaMetadataRetriever", "- Failed to rotate the video");
}
}
这篇关于检测视频是否以人像/风景拍摄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!