主要代码如下
try {
mMediaCodec = MediaCodec.createEncoderByType("video/avc");
} catch (IOException e) {
e.printStackTrace();
return;
}
MediaFormat format = MediaFormat.createVideoFormat("video/avc", mVideoSize.getWidth(), mVideoSize.getHeight());
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, 1250000);
format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
format.setInteger(MediaFormat.KEY_ROTATION, 90);
format.setInteger(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, 1000 / 30);
mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mEncoderSurface = mMediaCodec.createInputSurface();
and Camera2 Configuration is
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
mPreviewBuilder.addTarget(mPreviewSurface);
if (isEncode) {
mPreviewBuilder.addTarget(mEncoderSurface);
}
setUpCaptureRequestBuilder(mPreviewBuilder);
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, mBackgroundHandler);
get encode data from MediaCodec
MediaCodec.BufferInfo mBufferInfo = new MediaCodec.BufferInfo();
while (true) {
int outputBufferIndex = venc.dequeueOutputBuffer(mBufferInfo, 0);
if (outputBufferIndex >= 0) {
// outputBuffer is ready to be processed or rendered.
ByteBuffer buffer = venc.getOutputBuffer(outputBufferIndex);
onEncodedFrame(buffer, mBufferInfo);
getOutputBuffer
获取的数据使用 RTMP 推流到服务器的时候是旋转 90 度的,应该怎么修改?
1
icylord 2016-10-29 17:13:59 +08:00
|
2
xingda920813 2016-10-29 17:30:05 +08:00
|
3
lijun20020229 2016-10-31 09:49:55 +08:00
先要 camera.setDisplayOrientation(90);
|
4
lijun20020229 2016-10-31 09:57:07 +08:00
看错了 上面那条是调整 preview 的 rawdata 会不会影响没测试过
|
5
lijun20020229 2016-10-31 10:01:07 +08:00
可以参考楼上的 Sample Camera2RawFragment.java 文件 1109 行
// Initially, output stream images from the Camera2 API will be rotated to the native // device orientation from the sensor's orientation 。。。 |
6
lijun20020229 2016-10-31 10:10:47 +08:00
没猜错的话应该是 format.setInteger(MediaFormat.KEY_ROTATION, 90);转了 90 度的原因
这条不用转 preview 的时候转 90 度就行了 |
7
pursll OP @lijun20020229 format.setInteger(MediaFormat.KEY_ROTATION, 90); 这个加不加没差别
|
8
lijun20020229 2016-10-31 13:16:56 +08:00
@pursll 那看来应该不是这条的问题
但为什么这条没起作用 是否是 api level 设置过低让这条没起作用 KEY_ROTATION Added in API level 23 String KEY_ROTATION A key describing the desired clockwise rotation on an output surface. This key is only used when the codec is configured using an output surface. The associated value is an integer, representing degrees. Supported values are 0, 90, 180 or 270. This is an optional field; if not specified, rotation defaults to 0. |
9
lijun20020229 2016-10-31 13:31:58 +08:00
稍微看了下, google sample 里是用 Matrix 转换成 preview 合适的,它大小和角度都要调整
|