s4eed
s4eed

Reputation: 7891

OpencvAndroidSDK : Switching between Front camera and Back camera at run-time?

I have this NativeCameraView :

<org.opencv.android.NativeCameraView
            android:id="@+id/tutorial1_activity_native_surface_view"
            android:layout_width="350px"
            android:layout_height="350px"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            opencv:camera_id="front" />

By changing opencv:camera_id="front" to opencv:camera_id="back", I can change from front camera to back camera at design time( Can I call it compile time ?! )
But now I need to switch between them at run-time ? Is it possible?

Upvotes: 1

Views: 4850

Answers (1)

Loki
Loki

Reputation: 51

Given access to the following variables in an activity class:

private CameraBridgeViewBase mOpenCvCameraView; //exists as is in opencv samples
private int mCameraId = 0; //add this one

The Following method will switch views. The trick is to disable the camera view, set the camera index (in my case 0 and 1 are both valid), then re-enable the camera view.

private void swapCamera() {
    mCameraId = mCameraId^1; //bitwise not operation to flip 1 to 0 and vice versa
    mOpenCvCameraView.disableView();
    mOpenCvCameraView.setCameraIndex(mCameraId);
    mOpenCvCameraView.enableView();
}

Upvotes: 5

Related Questions