获取camera最佳预览尺寸

//从底层拿camera支持的previewsize,完了和屏幕分辨率做差,diff最小的就是最佳预览分辨率
    private void getPreviewSize(int mCameraId) {
        try {
            int diffs = Integer.MAX_VALUE;
            WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            screenResolution = new Point(display.getWidth(), display.getHeight());

            CameraCharacteristics props = mCameraManager.getCameraCharacteristics(mCameraId);
            StreamConfigurationMap configurationMap = props.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            Size[] availablePreviewSizes = configurationMap.getOutputSizes(SurfaceTexture.class);

            for (Size previewSize : availablePreviewSizes) {
                Log.v(TAG, " PreviewSizes = " + previewSize);
                mCameraPreviewWidth = previewSize.getWidth();
                mCameraPreviewHeight = previewSize.getHeight();
                int newDiffs = Math.abs(mCameraPreviewWidth - screenResolution.x) + Math.abs(mCameraPreviewHeight - screenResolution.y);
                Log.v(TAG, "newDiffs = " + newDiffs);

                if (newDiffs == 0) {
                    bestPreviewWidth = mCameraPreviewWidth;
                    bestPreviewHeight = mCameraPreviewHeight;
                    break;
                }
                if (diffs > newDiffs) {
                    bestPreviewWidth = mCameraPreviewWidth;
                    bestPreviewHeight = mCameraPreviewHeight;
                    diffs = newDiffs;
                }
            }
        } catch (CameraAccessException cae) {

        }
    }

 

你可能感兴趣的:(camera,相机)