前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android平台GB28181设备接入模块分辨率发生变化怎么办?

Android平台GB28181设备接入模块分辨率发生变化怎么办?

原创
作者头像
音视频牛哥
发布2022-12-20 16:50:52
2500
发布2022-12-20 16:50:52
举报

​技术背景

我们在做Android平台gb28181设备接入模块的时候,遇到这样的情况,比如横竖屏分辨率不锁定,采集摄像头的时候,可以实现,横屏状态采集横屏,竖屏状态采集竖屏,简单来说,横屏状态比如采集的1280*720的,竖屏状态,采集的数据源成了720*1280。

这块对我们来说,已经轻车熟路了,老早就已经处理,分辨率发生变化的时候,会重启encoder,确保正常兼容,不幸的是,好多gb28181平台侧,对分辨率切换,支持的并不友好,分辨率发生变化的时候,有些国标平台播放上来的视频流的时候,会直接crash。

分辨率变化,其实多数国标平台厂商都可以兼容,或者说应该兼容,如果国标平台侧不兼容的话,我们可以在android平台gb28181设备接入侧做一定的处理。

技术实现

以Android的camera2为例,我们做了“锁定图像方向”选项:

对应的代码实现如下:

代码语言:javascript
复制
    class ButtonCameraImageOrientationLockListener implements View.OnClickListener {
        public void onClick(View v) {
            is_need_lock_image_orientation_ = !is_need_lock_image_orientation_;

            if (cur_image_orientation_ >= 0)
                cur_image_orientation_ = orientation_;

            if(camera2Helper != null)
                cameraImageRotationDegree_ = camera2Helper.getCameraImageRotationDegree(cur_image_orientation_ < 0 ?orientation_ : cur_image_orientation_);

            btnCameraImageOrientationLock.setText(is_need_lock_image_orientation_?"解锁图像方向" : "锁定图像方向");
        }
    }

其中,getCameraImageRotationDegree实现如下:

代码语言:javascript
复制
    public int getCameraImageRotationDegree(int device_degree) {
        if (device_degree < 0)
            return -1;

        String camera_id = getCameraId();

        int degree;
        if (CAMERA_ID_BACK.equals(camera_id)) {
            degree = (mSensorOrientation + device_degree + 360) % 360;
            Log.i(TAG, "getCameraImageRotationDegree BACK device_degree:" + device_degree
                    + " SENSOR_ORIENTATION:" + mSensorOrientation + " degree:" + degree);
        }
        else {
            degree = (mSensorOrientation - device_degree + 360) % 360;
            Log.i(TAG, "getCameraImageRotationDegree FRONT device_degree:" + device_degree
                    + " SENSOR_ORIENTATION:" + mSensorOrientation + " degree:" + degree);
        }

        return degree;
    }

针对摄像头rotation变化监测listener:

代码语言:javascript
复制
 /*
  * Github: https://github.com/daniulive/SmarterStreaming   
  */
 class OnOriChangedListener implements OrientationDetector.OriginListener {

        @Override
        public void onOrientationChanged(int orientation, int rotation) {
            orientation_ = orientation;

            if (!is_need_lock_image_orientation_ || cur_image_orientation_ < 0)
                cur_image_orientation_ = orientation_;

            Log.i(TAG, "onOrientationChanged:" + orientation + " rotation:" + rotation + " rotation.toString:" + Camera2Helper.getRotationString(rotation));

            if(camera2Helper != null) {
                camera2Helper.updateOrientation(orientation);
                cameraImageRotationDegree_ = camera2Helper.getCameraImageRotationDegree(cur_image_orientation_ < 0 ?orientation_ : cur_image_orientation_);
            }

            if (layer_post_thread_ != null ) {
                int rotation_degree = cameraImageRotationDegree_;
                if (rotation_degree < 0)
                    layer_post_thread_.updateVideoSize(0, 0);
                else if (90 == rotation_degree || 270 == rotation_degree)
                    layer_post_thread_.updateVideoSize(video_height_, video_width_);
                else
                    layer_post_thread_.updateVideoSize(video_width_, video_height_);
            }
        }
    }

总结

值得一提的是,如果视频分辨率发生变化,本地录像的话,一般来说会切换新的录像文件,国标平台侧如果做的比较好的话,一般都会兼容这种分辨率变化的场景,起码确保分辨率切换的时候,不至于直接crash。

实际上,比如一些执法记录仪等场景下,只需要一个固定的采集方向即可,这种情况下,锁定方向,反而是最好的。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ​技术背景
  • 技术实现
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档