如何使用 Android CameraX ImageAnalysis 设置帧率?

问题描述 投票:0回答:1

我想增加图像分析能够获得的 FPS。我将其记录在控制台上,它的最高帧率约为 10 FPS。我怎样才能达到 30 FPS?

void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {

        final String TAG = "FPSCounter";
        final long[] startTime = {System.currentTimeMillis()};
        final int[] frameCount = {0};

        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                long elapsedTime = System.currentTimeMillis() - startTime[0];
                double fps = (double) frameCount[0] / (elapsedTime / 1000.0);
                Log.d(TAG, "FPS: " + String.format("%.2f", fps));
                frameCount[0] = 0;
                startTime[0] = System.currentTimeMillis();
            }
        }, 1000, 1000);

        Preview preview = new Preview.Builder().build();
        ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .build();

        imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), image -> {
            // This method will be called every time a new frame is available
            // You can use the 'image' object to get the current frame

            frameCount[0] = frameCount[0] + 1;

            image.close();
        });

        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();

        previewView = (PreviewView) findViewById(R.id.previewView);

        preview.setSurfaceProvider(previewView.getSurfaceProvider());

        Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, preview, imageAnalysis);
    }

我读到在 CameraX Image Analysis 中设置 FPS 没有一致的方法,但我希望有人能够帮助我。谢谢!

java android image frame-rate android-camerax
1个回答
0
投票

可能为时已晚,但对于任何为此苦苦挣扎的人,我记得可以通过 Camera2Interop Extender 实现。可以这样设置:

val ext = Camera2Interop.Extender(imageAnalysisConfig)
    ext.setCaptureRequestOption(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO)
    ext.setCaptureRequestOption(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, Range<Int>(30, 30))

在哪里

imageAnalysisConfig
是:

val imageAnalysisConfig = ImageAnalysis.Builder()
        .setTargetResolution(Size(PREVIEW_HEIGHT, PREVIEW_WIDTH))
        .apply {
            setImageQueueDepth(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
        }
© www.soinside.com 2019 - 2024. All rights reserved.