V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
pursll
V2EX  ›  Android

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

  •  
  •   pursll · Oct 29, 2016 · 22652 views
    This topic created in 3467 days ago, the information mentioned may be changed or developed.

    主要代码如下

         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 replies    2016-10-31 13:31:58 +08:00
    icylord
        1
    icylord  
       Oct 29, 2016
    lijun20020229
        3
    lijun20020229  
       Oct 31, 2016
    先要 camera.setDisplayOrientation(90);
    lijun20020229
        4
    lijun20020229  
       Oct 31, 2016
    看错了 上面那条是调整 preview 的 rawdata 会不会影响没测试过
    lijun20020229
        5
    lijun20020229  
       Oct 31, 2016
    可以参考楼上的 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  
       Oct 31, 2016
    没猜错的话应该是 format.setInteger(MediaFormat.KEY_ROTATION, 90);转了 90 度的原因
    这条不用转 preview 的时候转 90 度就行了
    pursll
        7
    pursll  
    OP
       Oct 31, 2016
    @lijun20020229 format.setInteger(MediaFormat.KEY_ROTATION, 90); 这个加不加没差别
    lijun20020229
        8
    lijun20020229  
       Oct 31, 2016
    @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  
       Oct 31, 2016
    稍微看了下, google sample 里是用 Matrix 转换成 preview 合适的,它大小和角度都要调整
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5672 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 75ms · UTC 09:00 · PVG 17:00 · LAX 02:00 · JFK 05:00
    ♥ Do have faith in what you're doing.