开发平台基本信息
芯片: SDM450
版本: Android 9.0
kernel: msm-4.9
问题描述
有个国外的客户,在我们的设备上装了政府的应用进行扫码,结果发现扫码预览界面旋转了180度,由于应用是政府开发的,客户也没有源码可以修改,所以,只能我们改固件,修改camera 默认的角度。如果是mipi摄像头的话,可以修改摄像头的config文件或者kernel层;但是因为我们的设备用的是usb摄像头,所以,只能改framework层或者hal层;我这里选择的是改hal层,感觉这样更加直接。
解决方法
上面提到通过修改framework层的方法可以参考这篇文章【Android系统源码修改】修改framework Camera的API,旋转摄像头。我没试过,不过从原理上看应该是能行得通的;
hal层修改的话,Android默认是两个camera,如果你的设备有多个camera的默认角度要设置,可以根据ID去设置角度,修改如下:
路径:hardware/qcom/camera/
diff --git a/QCamera2/QCamera2Factory.cpp b/QCamera2/QCamera2Factory.cpp
index a4e9b5a..2b73827 100755
--- a/QCamera2/QCamera2Factory.cpp
+++ b/QCamera2/QCamera2Factory.cpp
@@ -372,11 +372,23 @@ int QCamera2Factory::getCameraInfo(int camera_id, struct camera_info *info)
if(camera_id >= mNumOfCSICameras) {
rc = usbcam_get_camera_info(camera_id, info);
if (camera_id == 0) {
- info->orientation = 90;
+ char backValue[512] = {0};
+ char backOrientation[] = "persist.sys.back.orientation";
+ property_get(backOrientation,backValue,"90");
+ int backOri = atoi(backValue);
+ info->orientation = backOri;
info->facing = CAMERA_FACING_BACK;
+ LOGI("back====camera_id: %d, info->orientation: %d, \n",
+ camera_id,info->orientation);
} else {
- info->orientation = 270;
+ char frontValue[512] = {0};
+ char frontOrientation[] = "persist.sys.front.orientation";
+ property_get(frontOrientation,frontValue,"270");
+ int frontOri = atoi(frontValue);
+ info->orientation = frontOri;
info->facing = CAMERA_FACING_FRONT;
+ LOGI("front====camera_id: %d, info->orientation: %d, \n",
+ camera_id,info->orientation);
}
return rc;
}
后续如果有其他客户需要默认旋转摄像头角度,可以通过persist.sys.back.orientation=270
去设置角度。
注:这个方式只能修改默认角度,如果客户的app通过Camera.Parameters去强制设置角度,那预览角度还是会以最终设置为准。如果要使设置角度无效,可以参考上面链接framework的修改方式。