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
 import android.os.Environment;
15
 import android.os.Environment;
16
 import android.util.Log;
16
 import android.util.Log;
17
 import android.media.MediaMetadataRetriever;
17
 import android.media.MediaMetadataRetriever;
18
+import 	android.graphics.Matrix;
19
+
18
 import java.util.UUID;
20
 import java.util.UUID;
19
 import java.io.File;
21
 import java.io.File;
20
 import java.io.OutputStream;
22
 import java.io.OutputStream;
21
 import java.io.FileOutputStream;
23
 import java.io.FileOutputStream;
22
 
24
 
25
+
23
 public class RNThumbnailModule extends ReactContextBaseJavaModule {
26
 public class RNThumbnailModule extends ReactContextBaseJavaModule {
24
 
27
 
25
   private final ReactApplicationContext reactContext;
28
   private final ReactApplicationContext reactContext;
39
     filePath = filePath.replace("file://","");
42
     filePath = filePath.replace("file://","");
40
     MediaMetadataRetriever retriever = new MediaMetadataRetriever();
43
     MediaMetadataRetriever retriever = new MediaMetadataRetriever();
41
     retriever.setDataSource(filePath);
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
     Bitmap image = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
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
     String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/thumb";
60
     String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/thumb";
45
 
61
 
46
     try {
62
     try {