安卓下, 获取相机所支持的分辨率的完整列表的正确方法:
下面的方法,不能获取到完整列表,只能获取到部分列表,尤其是在高端分辨率上。
private ArrayAdapter<String> getCameraSupportedSizeList(int id) {
ArrayAdapter<String> result = new ArrayAdapter<String>(this, R.layout.spinner_item);
try {
Camera camera = Camera.open(id);
Camera.Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
for (Camera.Size size : sizes) {
result.add(String.format("%d万像素: %d×%d", size.width * size.height / 10000, size.width, size.height));
}
camera.release();
return result;
} catch (Exception ex) {
String s = "启动时获取相机分辨率信息失败,硬件故障或摄像头问题: " + ex.getMessage();
Log.e(TAG, s);
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
return result;
}
}
下面的方法可以获取到超过1300万像素的分辨率列表:
private ArrayAdapter<String> getCameraSupportedSizeList(int id) {
ArrayAdapter<String> result = new ArrayAdapter<String>(this, R.layout.spinner_item);
try {
android.hardware.camera2.CameraManager cameraManager = (android.hardware.camera2.CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics cc = cameraManager.getCameraCharacteristics(String.valueOf(id));
StreamConfigurationMap streamConfigurationMap = cc.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] sizes = streamConfigurationMap.getOutputSizes(ImageFormat.JPEG);
for (Size size : sizes) {
result.add(String.format("%d万像素: %d×%d", size.getWidth() * size.getHeight() / 10000, size.getWidth(), size.getHeight()));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
sizes = streamConfigurationMap.getHighResolutionOutputSizes(ImageFormat.JPEG);
for (Size size : sizes) {
result.add(String.format("%d万像素: %d×%d", size.getWidth() * size.getHeight() / 10000, size.getWidth(), size.getHeight()));
}
}
return result;
} catch (Exception ex) {
String s = "启动时获取相机分辨率信息失败,硬件故障或摄像头问题: " + ex.getMessage();
Log.e(TAG, s);
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
return result;
}
}