android studio 和人脸检测中我的相机应用程序中用于在前后摄像头之间切换的按钮

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

我正在使用 openCv 3.0 在 android studio 上开发相机应用程序。0.It这是我第一次这样做,我遇到了一些问题。但我有两个问题: 1)我想添加一个按钮来切换前置摄像头和后置摄像头。但我似乎找不到切换的方法。

这是我的 onCreate 方法:

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)我想使用openCv对拍摄的图像进行人脸检测,但我不知道是否可行。我在网上找不到任何东西。我已经尝试过来自 openCv 3.0.0 的示例 faceDetect,并且在我使用相机时它起作用了。那是我一开始想做的,但是在我将布局更改为包含框架布局而不是 org.opencv.android.JavaCameraView 之后,它不再起作用了。所以如果有人知道为什么我会非常感激。

所有 *CameraView 类 都有 disableViewenableView 方法。您需要禁用视图,设置视图对象的 mCameraIndex 字段并再次启用视图。 mCameraView 是受保护的方法,因此唯一的解决方案是实现视图子类和setters/getters。有关详细信息,请参阅 tutorial-3 示例。

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