Mediacodec Video Streaming From Camera Wrong Orientation & Color
I'm trying to stream video capturing directly from camera for android devices. So far I have been able to capture each frame from android camera's onPreviewFrame(byte[] data, Camer
Solution 1:
When I'm making an app which can real time broadcast camera frames via RTMP I have same problems in portrait mode but I can solve it by using TextureView. First of all, I didn't rotate frames on the sender side. But I rotate TextureView which is linked in mediaplayer and resizing textureview on the receiver side.
I coded like below.
<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextureViewandroid:id="@+id/videoView"android:layout_width="match_parent"android:layout_height="match_parent" /></FrameLayout>
privatevoidupdateTextureViewSize(int viewWidth, int viewHeight) {
intpivotPointX= viewWidth / 2;
intpivotPointY= viewHeight / 2;
Matrixmatrix=newMatrix();
if(isLandscapeOrientation) {
matrix.preRotate(0);
matrix.setScale(1.0f, 1.0f, pivotPointX, pivotPointY);
videoView.setTransform(matrix);
videoView.setLayoutParams(newFrameLayout.LayoutParams(viewWidth, viewHeight));
} else {
matrix.preRotate(0);
matrix.setScale(1.0f, 1.0f, pivotPointX, pivotPointY);
videoView.setRotation(90);
videoView.setTranslationX(-viewWidth / 2);
videoView.setTranslationY(-viewHeight / 2);
videoView.setLayoutParams(newFrameLayout.LayoutParams(viewWidth * 2, viewHeight * 2));
}
}
private TextureView.SurfaceTextureListenersurfaceTextureListener=newTextureView.SurfaceTextureListener() {
@OverridepublicvoidonSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surfaces=newSurface(surface);
if(mMediaPlayer != null) {
mMediaPlayer.setSurface(s);
DisplayMetricsdisplaymetrics=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
intsh= displaymetrics.heightPixels;
intsw= displaymetrics.widthPixels;
updateTextureViewSize(sw, sh);
}
}
@OverridepublicvoidonSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@OverridepublicbooleanonSurfaceTextureDestroyed(SurfaceTexture surface) {
returnfalse;
}
@OverridepublicvoidonSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
Solution 2:
When you rotated the image it has newWidth = oldHeight, and newHeigth = oldWidth, since source isnt a square picture. So you have a choice either to use cropping and adjust rotation cycle respectively or to use different preview size for dispaying, now you surfaces pitch just smaller then pitch of surfaceview or whatever component you use for display
Post a Comment for "Mediacodec Video Streaming From Camera Wrong Orientation & Color"