android camera2 获取摄像头支持的分辨率
41的for循环我注释了,代码是获取最匹配的分辨率。
private Size getMatchingSize2(){ Size selectSize = null; try { CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); for (final String cameraId : mCameraManager.getCameraIdList()) { CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(cameraId); StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); Size[] sizes = streamConfigurationMap.getOutputSizes(ImageFormat.JPEG); DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); //因为我这里是将预览铺满屏幕,所以直接获取屏幕分辨率 int deviceWidth = displayMetrics.widthPixels; //屏幕分辨率宽 int deviceHeigh = displayMetrics.heightPixels; //屏幕分辨率高 Log.e(TAG, "getMatchingSize2: 屏幕密度宽度=" + deviceWidth); Log.e(TAG, "getMatchingSize2: 屏幕密度高度=" + deviceHeigh); /** * 循环40次,让宽度范围从最小逐步增加,找到最符合屏幕宽度的分辨率, * 你要是不放心那就增加循环,肯定会找到一个分辨率,不会出现此方法返回一个null的Size的情况 * ,但是循环越大后获取的分辨率就越不匹配 */ // for (int j = 1; j < 41; j++) { for (int i = 0; i < sizes.length; i++) { //遍历所有Size Size itemSize = sizes[i]; Log.e(TAG, "当前itemSize 宽=" + itemSize.getWidth() + "高=" + itemSize.getHeight()); //判断当前Size高度小于屏幕宽度+j*5 && 判断当前Size高度大于屏幕宽度-j*5 && 判断当前Size宽度小于当前屏幕高度 if (itemSize.getHeight() < (deviceWidth ) && itemSize.getHeight() > (deviceWidth )) { if (selectSize != null) { //如果之前已经找到一个匹配的宽度 if (Math.abs(deviceHeigh - itemSize.getWidth()) < Math.abs(deviceHeigh - selectSize.getWidth())) { //求绝对值算出最接近设备高度的尺寸 selectSize = itemSize; continue; } } else { selectSize = itemSize; } } } if (selectSize != null) { //如果不等于null 说明已经找到了 跳出循环 break; } // } } } catch (CameraAccessException e) { e.printStackTrace(); } Log.e(TAG, "getMatchingSize2: 选择的分辨率宽度="+selectSize.getWidth()); Log.e(TAG, "getMatchingSize2: 选择的分辨率高度="+selectSize.getHeight()); return selectSize; }我的手机可以支持的:
当前itemSize 宽=3968高=2976 当前itemSize 宽=640高=480 当前itemSize 宽=320高=240 当前itemSize 宽=1280高=720 当前itemSize 宽=1920高=1080 当前itemSize 宽=3264高=1840 当前itemSize 宽=3264高=2448
camera的获取方法:
mCamera = Camera.open(mCamId); Camera.Parameters params = mCamera.getParameters();
List<Size> pictureSizes = params.getSupportedPictureSizes(); int length = pictureSizes.size(); for (int i = 0; i < length; i++) { Log.e("SupportedPictureSizes","SupportedPictureSizes : " + pictureSizes.get(i).width + "x" + pictureSizes.get(i).height); }
List<Size> previewSizes = params.getSupportedPreviewSizes(); length = previewSizes.size(); for (int i = 0; i < length; i++) { Log.e("SupportedPreviewSizes","SupportedPreviewSizes : " + previewSizes.get(i).width + "x" + previewSizes.get(i).height); }