get video dimensions from bitmap instead of meta data

This commit is contained in:
Daniel Gultsch 2018-04-30 09:22:59 +02:00
parent 287c416dac
commit 2390839c21

View file

@ -950,11 +950,29 @@ public class FileBackend {
return getVideoDimensions(mediaMetadataRetriever); return getVideoDimensions(mediaMetadataRetriever);
} }
private static Dimensions getVideoDimensionsOfFrame(MediaMetadataRetriever mediaMetadataRetriever) {
Bitmap bitmap = null;
try {
bitmap = mediaMetadataRetriever.getFrameAtTime();
return new Dimensions(bitmap.getHeight(), bitmap.getWidth());
} catch (Exception e) {
return null;
} finally {
if (bitmap != null) {
bitmap.recycle();;
}
}
}
private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever) throws NotAVideoFile { private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever) throws NotAVideoFile {
String hasVideo = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO); String hasVideo = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
if (hasVideo == null) { if (hasVideo == null) {
throw new NotAVideoFile(); throw new NotAVideoFile();
} }
Dimensions dimensions = getVideoDimensionsOfFrame(metadataRetriever);
if (dimensions != null) {
return dimensions;
}
int rotation = extractRotationFromMediaRetriever(metadataRetriever); int rotation = extractRotationFromMediaRetriever(metadataRetriever);
boolean rotated = rotation == 90 || rotation == 270; boolean rotated = rotation == 90 || rotation == 270;
int height; int height;
@ -977,25 +995,19 @@ public class FileBackend {
} }
private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) { private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) {
int rotation; String r = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
if (Build.VERSION.SDK_INT >= 17) { try {
String r = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION); return Integer.parseInt(r);
try { } catch (Exception e) {
rotation = Integer.parseInt(r); return 0;
} catch (Exception e) {
rotation = 0;
}
} else {
rotation = 0;
} }
return rotation;
} }
private static class Dimensions { private static class Dimensions {
public final int width; public final int width;
public final int height; public final int height;
public Dimensions(int height, int width) { Dimensions(int height, int width) {
this.width = width; this.width = width;
this.height = height; this.height = height;
} }