gpt4 book ai didi

java - Android CameraX 分析图像流

转载 作者:行者123 更新时间:2023-12-01 16:21:53 25 4
gpt4 key购买 nike

我想分析图像帧流并对其进行一些计算。然而,对于 CameraX,ImageAnalysis 实现类似乎只被调用一次 - 在相机绑定(bind)时。

我的问题是:如何对连续的图像流运行分析 - 概念上类似于视频流?

以下是我的相机、预览和分析设置代码:

private void setPreview() {
ListenableFuture<ProcessCameraProvider> instance = ProcessCameraProvider.getInstance(this);

Activity self = this;
instance.addListener(() -> {

try {
ProcessCameraProvider cameraProvider = instance.get();
Preview preview = new Preview.Builder().build();


ImageAnalysis imageAnalysis = new ImageAnalysis.Builder().build();
imageAnalysis.setAnalyzer(Executors.newFixedThreadPool(1),
new ImageAnalyser(new CameraLogListener() {
@Override
public void log(String msg) {
Log.e("Camera log", msg);
}
}));

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

cameraProvider.unbindAll();
Camera camera = cameraProvider.bindToLifecycle(((LifecycleOwner)self), cameraSelector, preview, imageAnalysis);

preview.setSurfaceProvider(
((PreviewView)findViewById(R.id.cameraTextureView)).createSurfaceProvider(camera.getCameraInfo()));

} catch (ExecutionException e) {
e.printStackTrace();
Log.e("TAG", "Use case binding failed", e);
} catch (InterruptedException e) {
e.printStackTrace();
Log.e("TAG", "Use case binding failed", e);
}

}, ContextCompat.getMainExecutor(this));

}

以下是我的 ImageAnalysis 实现类:

private class ImageAnalyser implements ImageAnalysis.Analyzer {

CameraLogListener listener;
public ImageAnalyser(CameraLogListener listener) {
this.listener = listener;
}

@Override
public void analyze(@NonNull ImageProxy image) {
ByteBuffer imageBuffer = image.getPlanes()[0].getBuffer();
StringBuilder sb = new StringBuilder();
sb.append("format:" + image.getFormat()).append("\n")
.append(image.getWidth() + " x " + image.getHeight()).append("\n\n");

for (int i=0; i<image.getPlanes().length; i++) {
sb.append("pixel stride:").append(image.getPlanes()[i].getPixelStride())
.append("\nrow stride:").append(image.getPlanes()[i].getRowStride())
.append("\n");
}

listener.log(sb.toString());

}
}

最佳答案

我发现了问题。在最后

  public void analyze(@NonNull ImageProxy image) {}

方法,你需要调用image.close()。引用自 documentation :

Before returning from analyze(), close the image reference by calling image.close() to avoid blocking the production of further images (causing the preview to stall) and to avoid potentially dropping images. The method must complete analysis or make a copy instead of passing the image reference beyond the analysis method.

关于java - Android CameraX 分析图像流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62249878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com