技术背景
我们在做Android平台gb28181设备接入模块的时候,遇到这样的情况,比如横竖屏分辨率不锁定,采集摄像头的时候,可以实现,横屏状态采集横屏,竖屏状态采集竖屏,简单来说,横屏状态比如采集的1280*720的,竖屏状态,采集的数据源成了720*1280。
这块对我们来说,已经轻车熟路了,老早就已经处理,分辨率发生变化的时候,会重启encoder,确保正常兼容,不幸的是,好多gb28181平台侧,对分辨率切换,支持的并不友好,分辨率发生变化的时候,有些国标平台播放上来的视频流的时候,会直接crash。
分辨率变化,其实多数国标平台厂商都可以兼容,或者说应该兼容,如果国标平台侧不兼容的话,我们可以在android平台gb28181设备接入侧做一定的处理。
技术实现
以Android的camera2为例,我们做了“锁定图像方向”选项:
对应的代码实现如下:
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实现如下:
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:
/** Github: https://github.com/daniulive/SmarterStreaming */class OnOriChangedListener implements OrientationDetector.OriginListener {@Overridepublic 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_);elselayer_post_thread_.updateVideoSize(video_width_, video_height_);}}}
总结
值得一提的是,如果视频分辨率发生变化,本地录像的话,一般来说会切换新的录像文件,国标平台侧如果做的比较好的话,一般都会兼容这种分辨率变化的场景,起码确保分辨率切换的时候,不至于直接crash。
实际上,比如一些执法记录仪等场景下,只需要一个固定的采集方向即可,这种情况下,锁定方向,反而是最好的。