V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pursll
V2EX  ›  Android

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

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

    主要代码如下

         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 度的,应该怎么修改?

    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 to 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   ·   我们的愿景   ·   实用小工具   ·   3480 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:36 · PVG 18:36 · LAX 03:36 · JFK 06:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.