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

7rtdyuoh  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(463)

我想增加图像分析能够得到的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图像分析中设置FPS没有一致的方法,但我希望有人能够帮助我。谢谢你!

gk7wooem

gk7wooem1#

可能太晚了,但对于任何人挣扎与此,我记得这是可能的通过Camera2Interop扩展器。可以这样设置:

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)
        }

相关问题