Skip to content

Commit 6fa1562

Browse files
author
Shahen Hovhannisyan
committed
fix(TrimmerManager): Get preview images with correct orientation
1 parent e015701 commit 6fa1562

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

android/src/main/java/com/shahenlibrary/Trimmer/Trimmer.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626

2727
import android.annotation.TargetApi;
2828
import android.graphics.Bitmap;
29+
import android.graphics.Matrix;
2930
import android.media.MediaMetadataRetriever;
3031
import android.net.Uri;
3132
import android.os.Build;
3233
import android.util.Base64;
34+
import android.util.Log;
3335

3436
import com.facebook.react.bridge.Arguments;
3537
import com.facebook.react.bridge.Promise;
@@ -58,15 +60,37 @@ public static void getPreviewImages(String path, Promise promise, ReactApplicati
5860
int duration = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
5961
int width = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
6062
int height = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
63+
int orientation = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
64+
65+
float aspectRatio = width / height;
6166

62-
int aspectRatio = Math.round(width / height);
6367
int resizeWidth = 200;
64-
int resizeHeight = resizeWidth / aspectRatio;
68+
int resizeHeight = Math.round(resizeWidth / aspectRatio);
69+
70+
float scaleWidth = ((float) resizeWidth) / width;
71+
float scaleHeight = ((float) resizeHeight) / height;
72+
73+
Log.d(TrimmerManager.REACT_PACKAGE, "getPreviewImages: \n\tduration: " + duration +
74+
"\n\twidth: " + width +
75+
"\n\theight: " + height +
76+
"\n\torientation: " + orientation +
77+
"\n\taspectRatio: " + aspectRatio +
78+
"\n\tresizeWidth: " + resizeWidth +
79+
"\n\tresizeHeight: " + resizeHeight
80+
);
81+
82+
Matrix mx = new Matrix();
83+
84+
mx.postScale(scaleWidth, scaleHeight);
85+
mx.postRotate(orientation - 360);
6586

6687
for (int i = 0; i < duration; i += duration / 10) {
67-
Bitmap currBmp = Bitmap.createScaledBitmap(retriever.getFrameAtTime(i * 1000), resizeWidth, resizeHeight, false);
88+
Bitmap frame = retriever.getFrameAtTime(i * 1000);
89+
Bitmap currBmp = Bitmap.createScaledBitmap(frame, resizeWidth, resizeHeight, false);
90+
91+
Bitmap normalizedBmp = Bitmap.createBitmap(currBmp, 0, 0, resizeWidth, resizeHeight, mx, true);
6892
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
69-
currBmp.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream);
93+
normalizedBmp.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream);
7094
byte[] byteArray = byteArrayOutputStream .toByteArray();
7195
String encoded = "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT);
7296
images.pushString(encoded);

0 commit comments

Comments
 (0)