Search code examples
javaandroidopencvcameraface-detection

Button to switch between front and back camera in my camera app in android studio and face detection


I'm developping a camera app on android studio using openCv 3.0.0.It's my first time doing this and I'm facing some problems. But I have 2 issues: 1) I want to add a button to switch between the front camera and the back camera. But I can't seem to find a way to switch.

Here is my onCreate method:

private Camera mCamera;
private CameraPreview mPreview;
private static int number = Camera.CameraInfo.CAMERA_FACING_FRONT;

 @Override
 public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.face_detect_surface_view);

     // Create an instance of Camera
        mCamera = getCameraInstance(number);// This funtion opens the camera

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, number , mCamera);
        final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

        preview.addView(mPreview);


        // Add a listener to the Capture button
        ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "Image captured!", Toast.LENGTH_SHORT).show();
                        // get an image from the camera
                        mCamera.takePicture(null, null, mPicture);
                    }
                }
        );

// Add a listener to the Change button
        ImageButton changeButton = (ImageButton) findViewById(R.id.button_change);
        changeButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (number == Camera.CameraInfo.CAMERA_FACING_FRONT)
                            number = Camera.CameraInfo.CAMERA_FACING_BACK;
                        else
                            number = Camera.CameraInfo.CAMERA_FACING_FRONT;
                       //HERE SHOULD BE THE STEPS TO SWITCH
                        Toast.makeText(getApplicationContext(), "Camera changed!", Toast.LENGTH_SHORT).show();
                        // get an image from the camera

                    }
                }
        ); 
}

2)I want to use openCv for face detection on the image that's captures but I don't know whether it's possible. I couldn't find anything on the web. I've already tried the example faceDetect from openCv 3.0.0 and it worked when I was using the camera. That's what I wanted to do at first but after I changed my layout to contain a frame layout instead of org.opencv.android.JavaCameraView it doesn't work anymore. So if anyone has an idea why I'd be very grateful.


Solution

  • All *CameraView classes have disableView and enableView methods. You need to disable view, set mCameraIndex field of View object and enable view again. mCameraView is protected method, so the only solution is to implement view subclass and setters/getters. See tutorial-3 example for more details.

    <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" />