Search code examples
javaandroidopencvandroid-camera

How to display OpenCV JavaCameraView front camera in mirror image (Android)?


I am currently experimenting with the openCV's java example on face detection in android. However, the view created by the camera is not in mirror imaging.I tried setting the android:screenOrientation to reverseLandscape but it did not work. I would like to try to achieve this, any suggestions?

Code in layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<org.opencv.android.JavaCameraView
    android:id="@+id/fd_activity_surface_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:screenOrientation="reverseLandscape"
    />

instantiating

private CameraBridgeViewBase mOpenCvCameraView;

on OpenCV loaded

mOpenCvCameraView.setCameraIndex(1);
mOpenCvCameraView.enableView();

onCreate() method

mOpenCvCameraView = findViewById(R.id.fd_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);

mOpenCvCameraView did not contain a setDisplayOrientation() method and setRotation(180) returned me with a black display.


Solution

  • The minimal change to make the mirror is to add the following code to your CvCameraViewListener2:

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Mat rgba = inputFrame.rgba();
        Core.flip(rgba, rgba, 1);
        return rgba;
    }
    

    This is not the most efficient way, but probably acceptable. On my rather weak test device, this reduces FPS from 7.17 to 7.15.