如何告诉我在Android代码中运行的设备



我正在使用一些与相机相关的功能。对于一些设备(比如一个名为Vuzix的智能玻璃(,相机是倒置的,所以在传递一些属性时,我需要进行ROTATE.180,而对于其他设备,它没有翻转,所以我只需要通过ROTATE.NONE;我想知道是否有任何方法可以在if语句中翻转或不翻转正在运行的设备名称/摄像头,比如(if device.name=Vuzix(或类似的语句(if camera.contation==reversed(。现在,在每个设备上运行之前,我必须手动更改。

是的当然,你可以很容易地检测代码在什么设备上运行。请参阅此链接:如何在Android中以编程方式检测移动设备制造商和型号?

您可以获得设备名称和制造商如下:

String deviceName = android.os.Build.MODEL;
String deviceMan = android.os.Build.MANUFACTURER;

您当然可以检查制造商,但这不是最稳健的解决方案。Vuzix眼镜的传感器方向与许多其他供应商相比是颠倒的,但除了Vuzix之外,还有其他供应商使用相同的安装方式。

幸运的是,在Vuzix产品上,相机特性反映了这一点,因此您可以使用安卓开发者文档发布的示例代码。您可以在窗口管理器中查询显示方向和相机特性,以查找传感器安装。然后,无论使用什么设备,都可以使用相同的代码。

public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;  // compensate the mirror
} else {  // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);

}

相关内容

最新更新