如何将后置摄像头更改为前置摄像头?请帮助和扩展步骤



如何将后置摄像头更改为前置摄像头?

          public void setPreview() throws IOException {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                mCamera.setPreviewTexture( new SurfaceTexture(10) );
            else
             mCamera.setPreviewDisplay(null);
     }

如何更换和使用前置摄像头?

        public boolean openCamera() {
            Log.i(TAG, "openCamera");
            releaseCamera();
            mCamera = Camera.open();
            if(mCamera == null) {
             Log.e(TAG, "Can't open camera!");
             return false;
            }
               }

如何更换相机?

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

您可以通过传递用于打开前置摄像头的布尔值来使用此方法 True 或 false 对于后置摄像头,请在调用此方法之前重置摄像头。

private void opnenCamera(boolean isFront){
           int cameraCount = 0;
            Camera cam = null;
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            cameraCount = Camera.getNumberOfCameras();
           for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
                Camera.getCameraInfo( camIdx, cameraInfo );
                if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT && isFront ) {
                    try {
                        cam = Camera.open( camIdx );
                    } catch (RuntimeException e) {
                        Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                    }
                }else if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK && !isFront ) {
                    try {
                        cam = Camera.open( camIdx );
                    } catch (RuntimeException e) {
                        Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                    }
                }
            }
}

相关内容

  • 没有找到相关文章