Android的CameraX獲得相機支援解析度的方法

West Continent發表於2024-04-08

如題,方法如下:

androidx.camera.core.Camera camera1 = cameraProvider.bindToLifecycle(/* lifecycleOwner= */ this, cameraSelector, analysisUseCase);
        Camera2CameraInfo camera2Info = Camera2CameraInfo.from(camera1.getCameraInfo());
        Size[] size = camera2Info.getCameraCharacteristic(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
        logger.info("Get size");

  Size的陣列即為支援的解析度,設定解析度的方法如下:

ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
        ResolutionSelector.Builder resolutionBuilder = new ResolutionSelector.Builder();
        builder.setResolutionSelector(resolutionBuilder.setResolutionStrategy(new ResolutionStrategy(new Size(1920, 1080), ResolutionStrategy.FALLBACK_RULE_NONE)).build());
        analysisUseCase = builder.build();

  Preview的時候:

Preview.Builder builder = new Preview.Builder();
        ResolutionSelector.Builder resolutionBuilder = new ResolutionSelector.Builder();
        builder.setResolutionSelector(resolutionBuilder.setResolutionStrategy(new ResolutionStrategy(new Size(1920, 1080), ResolutionStrategy.FALLBACK_RULE_NONE)).build());
        //builder.setResolutionSelector(resolutionBuilder.setResolutionStrategy(ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY).build());
        previewUseCase = builder.build();

  

相關文章