Browse Source

Rotate for wrong orientation image

Hau Vo 7 years ago
parent
commit
3b52a6dfbf
1 changed files with 16 additions and 0 deletions
  1. 16
    0
      android/src/main/java/com/reactlibrary/RNThumbnailModule.java

+ 16
- 0
android/src/main/java/com/reactlibrary/RNThumbnailModule.java View File

@@ -15,11 +15,14 @@ import android.graphics.Bitmap;
15 15
 import android.os.Environment;
16 16
 import android.util.Log;
17 17
 import android.media.MediaMetadataRetriever;
18
+import 	android.graphics.Matrix;
19
+
18 20
 import java.util.UUID;
19 21
 import java.io.File;
20 22
 import java.io.OutputStream;
21 23
 import java.io.FileOutputStream;
22 24
 
25
+
23 26
 public class RNThumbnailModule extends ReactContextBaseJavaModule {
24 27
 
25 28
   private final ReactApplicationContext reactContext;
@@ -39,8 +42,21 @@ public class RNThumbnailModule extends ReactContextBaseJavaModule {
39 42
     filePath = filePath.replace("file://","");
40 43
     MediaMetadataRetriever retriever = new MediaMetadataRetriever();
41 44
     retriever.setDataSource(filePath);
45
+    int videoWidth = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
46
+    int videoHeight = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
47
+
42 48
     Bitmap image = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
43 49
 
50
+    int bitmapWidth = image.getWidth();
51
+    int bitmapHeight = image.getHeight();
52
+
53
+    if ((bitmapWidth > bitmapHeight) !== (videoWidth > videoHeight)) {
54
+      // we need to rotate image
55
+      Matrix matrix = new Matrix();
56
+      matrix.postRotate(-90);
57
+      image = Bitmap.createBitmap(image, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
58
+    }
59
+
44 60
     String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/thumb";
45 61
 
46 62
     try {