遵循此https://stackoverflow.com/a/44324039/1427037 https://inducesmile.com/android/android-camera2-api-example-tutorial/ 但仍然 camera2 API 在三星设备 android 9.0 操作系统中提供了拉伸预览。许多 stackoverflow 解决方案已经尝试过,但没有优势。有什么帮助吗? 注意:不能将相机 API 与 SurfaceHolder 一起使用。
问问题
208 次
1 回答
1
检查这个
@Override
public void setVideoURI(Uri uri) {
super.setVideoURI(uri);
MediaMetadataRetriever retr = new MediaMetadataRetriever();
retr.setDataSource(uri.getPath());
String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
try {
videoRealH = Integer.parseInt(height);
videoRealW = Integer.parseInt(width);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
if (height > width) {
if (videoRealH > videoRealW) {
mVideoHeight = height;
mVideoWidth = width;
} else {
mVideoWidth = width;
float r = videoRealH / (float) videoRealW;
mVideoHeight = (int) (mVideoWidth * r);
}
} else {
if (videoRealH > videoRealW) {
mVideoHeight = height;
float r = videoRealW / (float) videoRealH;
mVideoWidth = (int) (mVideoHeight * r);
} else {
mVideoHeight = height;
mVideoWidth = width;
}
}
if (videoRealH == videoRealW && videoRealH == 1) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else {
setMeasuredDimension(mVideoWidth, mVideoHeight);
}
}
于 2020-01-13T10:37:12.157 回答