解决方法
从API级别21开始是可能的.从文档(
https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics.html#SENSOR_INFO_PHYSICAL_SIZE):
public static final Key SENSOR_INFO_PHYSICAL_SIZE
The physical dimensions of the full pixel array. […]
Units: Millimeters
我用这种代码.请注意,可能不仅仅是一台摄像头:
import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraManager; private SizeF getCameraResolution(int camNum) { SizeF size = new SizeF(0,0); CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String[] cameraIds = manager.getCameraIdList(); if (cameraIds.length > camNum) { CameraCharacteristics character = manager.getCameraCharacteristics(cameraIds[camNum]); size = character.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE); } } catch (CameraAccessException e) { Log.e("YourLogString",e.getMessage(),e); } return size; }
请注意,需要捕获Exception CameraAccessException.
别忘了添加< uses-sdk android:minSdkVersion =“21”/>到你的清单.