Android的 - 之间的正面和背面摄像头开关 [英] Android - Switch Between front and back camera

查看:72
本文介绍了Android的 - 之间的正面和背面摄像头开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个按钮,正面和背面摄像头之间切换

I want to add a button to switch between front and back camera

mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);//add + 1 to use front camera

如在上述code段,我可以通过简单地改变相机的添加+1

例如: mCamera =新VideoCapture(Highgui.CV_CAP_ANDROID + 1)

不过,我想通过一个按钮来做到这一点。我怎样才能实现这个

But I want to do this by using a button. How can I implement this

感谢您,

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");
}

}

推荐答案

我觉得这链接对你有帮助的开关之间的正面和背面的摄像头。

I think this link is helpful to you for switching Between front and back camera.

检查的问题的另一个解决方案。希望这对您有所帮助。

Check another solution for problem. Hope this helpful to you.

这篇关于Android的 - 之间的正面和背面摄像头开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆