Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CameraX View in React Native - Unable to configure camera

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.

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 Checked this. But not sure exactly if the problem is with the re-layout.

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

Below is the snippet:

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

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());
        }
    }
like image 294
Balasubramanian Avatar asked Nov 07 '22 04:11

Balasubramanian


1 Answers

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

It worked after attaching the layout with root:

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

instead of this:

FrameLayout layout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.camera_layout, null);
like image 105
Balasubramanian Avatar answered Nov 14 '22 21:11

Balasubramanian