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

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

问题描述

我正在创建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!

从摄像机活动"中登录:

Log from Camera Activity:

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天全站免登陆