React Native 中的 CameraX 视图 - 无法配置相机 [英] CameraX View in React Native - Unable to configure camera

查看:52
本文介绍了React Native 中的 CameraX 视图 - 无法配置相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过桥接为 React Native 创建 cameraX 视图.(普通视图在 RN 中渲染.桥接流程很好.)使用 PreviewView 显示 CameraX 预览的相机源.我已成功实现预览,但不确定为什么预览未在 UI 上呈现.它只是一个空白屏幕.

I am creating cameraX view for react native through bridging.(Normal Views are rendering in RN. Bridging flow is fine.) Using PreviewView for display camera feed for CameraX’s preview. I have successfully implemented the preview but not sure why preview is not rendering on the UI. It just a blank screen.

让我知道 UI 原生组件的正确实现方式.我在活动中厌倦了同样的事情并且工作正常.我是 android 平台的新手.提前致谢!

Let me know the right way of implementation for UI native component. I have tired samething in activity and working fine. I am new to the android platform. Thanks in advance!

https://groups.google.com/a/android.com/g/camerax-developers/c/G9jKs1Bo_CE检查了这个.但不确定问题是否出在重新布局上.

https://groups.google.com/a/android.com/g/camerax-developers/c/G9jKs1Bo_CE Checked this. But not sure exactly if the problem is with the re-layout.

从 NativeCameraView 类登录:

Log from NativeCameraView class:

23:21:45.415 8026-8026/com.app D/PreviewView: Surface requested by Preview.
11-11 23:21:45.415 8026-8784/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
11-11 23:21:45.415 8026-8784/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
11-11 23:21:45.416 8026-8784/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
11-11 23:21:45.416 8026-8784/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
11-11 23:21:45.417 8026-8784/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
11-11 23:21:50.459 8026-8785/com.app E/Camera2CameraImpl: Unable to configure camera 0, timeout!

来自相机活动的日志:

11-12 22:55:40.545 6445-6445/com.app D/ReactVideoEvents: onHostPause
11-12 22:55:40.592 6445-7585/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
11-12 22:55:40.593 6445-7585/com.app D/UseCaseAttachState: Active and attached use case: [] for camera: 0
11-12 22:55:40.593 6445-7585/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
11-12 22:55:40.601 6445-7585/com.app I/CameraManager: Using legacy camera HAL.
11-12 22:55:40.604 6445-6445/com.app D/PreviewView: Surface requested by Preview.
11-12 22:55:40.611 6445-6445/com.app D/TextureViewImpl: SurfaceTexture available. Size: 1440x1080
11-12 22:55:40.612 6445-6445/com.app D/TextureViewImpl: Surface set on Preview.
11-12 22:55:40.791 6445-7585/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
11-12 22:55:40.793 6445-7585/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
11-12 22:55:40.793 6445-7585/com.app D/CaptureSession: Opening capture session.
11-12 22:55:40.795 6445-7585/com.app I/CameraDeviceState: Legacy camera service transitioning to state CONFIGURING
11-12 22:55:40.795 6445-8660/com.app I/RequestThread-0: Configure outputs: 1 surfaces configured.
11-12 22:55:40.795 6445-8660/com.app D/Camera: app passed NULL surface
11-12 22:55:40.819 6445-7585/com.app I/CameraDeviceState: Legacy camera service transitioning to state IDLE
11-12 22:55:40.820 6445-7578/com.app D/CaptureSession: Attempting to send capture request onConfigured
11-12 22:55:40.820 6445-7578/com.app D/CaptureSession: Issuing request for session.
11-12 22:55:40.822 6445-7578/com.app I/RequestQueue: Repeating capture request set.
11-12 22:55:40.822 6445-7578/com.app D/CaptureSession: CameraCaptureSession.onConfigured() mState=OPENED
11-12 22:55:40.823 6445-7585/com.app D/CaptureSession: CameraCaptureSession.onReady() OPENED

以下是片段:

public class myNativeCameraView extends FrameLayout {
...

public myNativeCameraView(ThemedReactContext context) {
    super(context);
    Log.d("ReactSpoCameraManager", "myNativeCameraView 1");
    mContext = context;

    FrameLayout layout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.camera_layout, null);
    mPreviewView = layout.findViewById(R.id.previewView);
    startCamera();
    setupLayoutHack();
}

private void startCamera() {
    final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(mContext);
    cameraProviderFuture.addListener(new Runnable() {
        @Override
        public void run() {
            try {
                ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                bindPreview(cameraProvider);
            } catch (ExecutionException | InterruptedException e) {}
        }
    }, ContextCompat.getMainExecutor(mContext));
}

void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
mPreviewView.setPreferredImplementationMode(PreviewView.ImplementationMode.SURFACE_VIEW);
    Preview preview = new Preview.Builder().build();
    CameraSelector cameraSelector = new CameraSelector.Builder()
            .requireLensFacing(CameraSelector.LENS_FACING_BACK)
            .build();
    ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
            .build();
    ImageCapture.Builder builder = new ImageCapture.Builder();

    preview.setSurfaceProvider(mPreviewView.createSurfaceProvider());
    Camera camera = cameraProvider.bindToLifecycle((AppCompatActivity) mContext.getCurrentActivity(), cameraSelector, preview, imageAnalysis);
}
}

我在构造函数中的 startCamera 之后调用这个 setupLayoutHack.确定问题是否与 React Native 中的重新布局有关.但仍然面临同样的错误.

I am calling this setupLayoutHack after startCamera in constructor. To make sure if the problem is with re-layout in React Native. But Still facing same error.

void setupLayoutHack() {
        Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
            @Override
            public void doFrame(long frameTimeNanos) {
//                Log.d(TAG, "setupLayoutHack");
                manuallyLayoutChildren();
                getViewTreeObserver().dispatchOnGlobalLayout();
                Choreographer.getInstance().postFrameCallback(this);
            }
        });
    }

    void manuallyLayoutChildren() {
        for (int i = 0; i < getChildCount(); i++) {
            Log.d(TAG, "manuallyLayoutChildren");
            View child = getChildAt(i);
            child.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
            child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
        }
    }

推荐答案

我发现 PreviewView 没有添加到父级中,因为 getChildCount 返回 0.

I found that PreviewView is not added in the parent because getChildCount was returning 0.

在用 root 附加布局后它起作用了:

It worked after attaching the layout with root:

FrameLayout layout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.camera_layout, this, true);

而不是这个:

FrameLayout layout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.camera_layout, null);

这篇关于React Native 中的 CameraX 视图 - 无法配置相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆