Camera2 获取摄像头数据,使用 MediaCodec 编码,图像旋转 90 度,怎么解决? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pursll
V2EX    Android

Camera2 获取摄像头数据,使用 MediaCodec 编码,图像旋转 90 度,怎么解决?

  •  
  •   pursll 2016-10-29 16:33:01 +08:00 21401 次点击
    这是一个创建于 3273 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要代码如下

     try { mMediaCodec = MediaCodec.createEncoderByType("video/avc"); } catch (IOExcepion 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 度的,应该怎么修改?

    9 条回复    2016-10-31 13:31:58 +08:00
    icylord
        1
    icylord  
       2016-10-29 17:13:59 +08:00
    lijun20020229
        3
    lijun20020229  
       2016-10-31 09:49:55 +08:00
    先要 camera.setDisplayOrientation(90);
    lijun20020229
        4
    lijun20020229  
       2016-10-31 09:57:07 +08:00
    看错了 上面那条是调整 preview 的 rawdata 会不会影响没测试过
    lijun20020229
        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 o the native
    // device orientation from the sensor's orientation 。。。
    lijun20020229
        6
    lijun20020229  
       2016-10-31 10:10:47 +08:00
    没猜错的话应该是 format.setInteger(MediaFormat.KEY_ROTATION, 90);转了 90 度的原因
    这条不用转 preview 的时候转 90 度就行了
    pursll
        7
    pursll  
    OP
       2016-10-31 11:38:30 +08:00
    @lijun20020229 format.setInteger(MediaFormat.KEY_ROTATION, 90); 这个加不加没差别
    lijun20020229
        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.
    lijun20020229
        9
    lijun20020229  
       2016-10-31 13:31:58 +08:00
    稍微看了下, google sample 里是用 Matrix 转换成 preview 合适的,它大小和角度都要调整
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5620 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 03:04 PVG 11:04 LAX 20:04 JFK 23:04
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86