8000 Merge pull request #146 from LinusU/android-frame-orientation · sinkync/flutter-webrtc@769d66a · GitHub
[go: up one dir, main page]

Skip to content

Commit 769d66a

Browse files
authored
Merge pull request flutter-webrtc#146 from LinusU/android-frame-orientation
Fix Android frame orientation
2 parents 0ccf6d8 + 2c14bb6 commit 769d66a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/record/FrameCapturer.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.cloudwebrtc.webrtc.record;
22

3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
35
import android.graphics.ImageFormat;
6+
import android.graphics.Matrix;
47
import android.graphics.Rect;
58
import android.graphics.YuvImage;
69
import android.os.Handler;
@@ -66,9 +69,12 @@ public void onFrame(VideoFrame videoFrame) {
6669
videoTrack.removeSink(this);
6770
});
6871
try {
69-
if (!file.exists())
72+
if (!file.exists()) {
73+
//noinspection ResultOfMethodCallIgnored
74+
file.getParentFile().mkdirs();
7075
//noinspection ResultOfMethodCallIgnored
7176
file.createNewFile();
77+
}
7278
} catch (IOException io) {
7379
callback.error("IOException", io.getLocalizedMessage(), io);
7480
return;
@@ -79,6 +85,23 @@ public void onFrame(VideoFrame videoFrame) {
7985
100,
8086
outputStream
8187
);
88+
switch (videoFrame.getRotation()) {
89+
case 0:
90+
break;
91+
case 90:
92+
case 180:
93+
case 270:
94+
Bitmap original = BitmapFactory.decodeFile(file.toString());
95+
Matrix matrix = new Matrix();
96+
matrix.postRotate(videoFrame.getRotation());
97+
Bitmap rotated = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);
98+
FileOutputStream rotatedOutputStream = new FileOutputStream(file);
99+
rotated.compress(Bitmap.CompressFormat.JPEG, 100, rotatedOutputStream);
100+
break;
101+
default:
102+
// Rotation is checked to always be 0, 90, 180 or 270 by VideoFrame
103+
throw new RuntimeException("Invalid rotation");
104+
}
82105
callback.success(null);
83106
} catch (IOException io) {
84107
callback.error("IOException", io.getLocalizedMessage(), io);
@@ -89,4 +112,4 @@ public void onFrame(VideoFrame videoFrame) {
89112
}
90113
}
91114

92-
}
115+
}

0 commit comments

Comments
 (0)
0