|
26 | 26 |
|
27 | 27 | import android.annotation.TargetApi; |
28 | 28 | import android.graphics.Bitmap; |
| 29 | +import android.graphics.Matrix; |
29 | 30 | import android.media.MediaMetadataRetriever; |
30 | 31 | import android.net.Uri; |
31 | 32 | import android.os.Build; |
32 | 33 | import android.util.Base64; |
| 34 | +import android.util.Log; |
33 | 35 |
|
34 | 36 | import com.facebook.react.bridge.Arguments; |
35 | 37 | import com.facebook.react.bridge.Promise; |
@@ -58,15 +60,37 @@ public static void getPreviewImages(String path, Promise promise, ReactApplicati |
58 | 60 | int duration = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)); |
59 | 61 | int width = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); |
60 | 62 | 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; |
61 | 66 |
|
62 | | - int aspectRatio = Math.round(width / height); |
63 | 67 | 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); |
65 | 86 |
|
66 | 87 | 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); |
68 | 92 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
69 | | - currBmp.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream); |
| 93 | + normalizedBmp.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream); |
70 | 94 | byte[] byteArray = byteArrayOutputStream .toByteArray(); |
71 | 95 | String encoded = "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT); |
72 | 96 | images.pushString(encoded); |
|
0 commit comments