Android-在前后摄像头之间切换

Android-在前后摄像头之间切换,android,android-camera,Android,Android Camera,我想添加一个按钮,在前后摄像头之间切换 mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);//add + 1 to use front camera 如上面代码段所示,我可以通过简单地添加+1来更改相机 示例:mCamera=newvideocapture(Highgui.CV\u CAP\u ANDROID+1) 但是我想用一个按钮。我如何实现这一点 谢谢, private static final String TAG = "Sample

我想添加一个按钮,在前后摄像头之间切换

mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);//add + 1 to use front camera
如上面代码段所示,我可以通过简单地添加+1来更改相机

示例:
mCamera=newvideocapture(Highgui.CV\u CAP\u ANDROID+1)

但是我想用一个按钮。我如何实现这一点

谢谢,

private static final String TAG = "Sample::SurfaceView";
private SurfaceHolder       mHolder;
private VideoCapture        mCamera;
private FpsMeter            mFps;

public SampleCvViewBase(Context context) {
    super(context);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mFps = new FpsMeter();
    Log.i(TAG, "Instantiated new " + this.getClass());
}

public boolean openCamera() {
    Log.i(TAG, "openCamera");
    synchronized (this) {
        releaseCamera();
        mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);//add + 1 to use front camera
        if (!mCamera.isOpened()) {
            mCamera.release();
            mCamera = null;
            Log.e(TAG, "Failed to open native camera");
            return false;
        }
    }
    return true;
}

public void releaseCamera() {
    Log.i(TAG, "releaseCamera");
    synchronized (this) {
        if (mCamera != null) {
                mCamera.release();
                mCamera = null;
        }
    }
}

public void setupCamera(int width, int height) {
    Log.i(TAG, "setupCamera("+width+", "+height+")");
    synchronized (this) {
        if (mCamera != null && mCamera.isOpened()) {
            List<Size> sizes = mCamera.getSupportedPreviewSizes();
            int mFrameWidth = width;
            int mFrameHeight = height;

            // selecting optimal camera preview size
            {
                double minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - height) < minDiff) {
                        mFrameWidth = (int) size.width;
                        mFrameHeight = (int) size.height;
                        minDiff = Math.abs(size.height - height);
                    }
                }
            }

            mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
            mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
        }
    }

}

public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
    Log.i(TAG, "surfaceChanged");
    setupCamera(width, height);
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.i(TAG, "surfaceCreated");
    (new Thread(this)).start();
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.i(TAG, "surfaceDestroyed");
    releaseCamera();
}

protected abstract Bitmap processFrame(VideoCapture capture);

public void run() {
    Log.i(TAG, "Starting processing thread");
    mFps.init();

    while (true) {
        Bitmap bmp = null;

        synchronized (this) {
            if (mCamera == null)
                break;

            if (!mCamera.grab()) {
                Log.e(TAG, "mCamera.grab() failed");
                break;
            }

            bmp = processFrame(mCamera);

            mFps.measure();
        }

        if (bmp != null) {
            Canvas canvas = mHolder.lockCanvas();
            if (canvas != null) {
                canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
               // mFps.draw(canvas, (canvas.getWidth() - bmp.getWidth()) / 2, 0);
                mHolder.unlockCanvasAndPost(canvas);
            }
            bmp.recycle();
        }
    }

    Log.i(TAG, "Finishing processing thread");
}
private static final String TAG=“Sample::SurfaceView”;
私人地勤人员;
私有视频捕获mCamera;
私人电表;
公共SampleCvViewBase(上下文){
超级(上下文);
mHolder=getHolder();
mHolder.addCallback(this);
mFps=新FpsMeter();
Log.i(标记“实例化新”+this.getClass());
}
公共布尔openCamera(){
Log.i(标签“openCamera”);
已同步(此){
松开摄像头();
mCamera=newvideocapture(Highgui.CV_CAP_ANDROID);//添加+1以使用前置摄像头
如果(!mCamera.isOpened()){
mCamera.release();
mCamera=null;
Log.e(标记“无法打开本机摄像头”);
返回false;
}
}
返回true;
}
公共无效释放摄影机(){
Log.i(标签“释放摄像头”);
已同步(此){
if(mCamera!=null){
mCamera.release();
mCamera=null;
}
}
}
公共摄像机(整数宽度、整数高度){
Log.i(标签,“设置摄像头(“+宽度+”,“+高度+”);
已同步(此){
if(mCamera!=null&&mCamera.isOpened()){
列表大小=mCamera.getSupportedPreviewSizes();
int mFrameWidth=宽度;
int mframehight=高度;
//选择最佳相机预览尺寸
{
double minDiff=double.MAX_值;
用于(尺寸:尺寸){
if(数学绝对值(尺寸高度-高度)
}