Android:单击button时切换相机

我有一个名为switchCamera的方法,我试图通过点击一个button,从一个平滑的过渡切换到前一个相机。 我调用这个方法时,我的应用程序冻结 – 我知道我没有做正确的事情。 有人可以帮我从这里出去吗?

任何帮助深表感谢。

public void switchCamera(){ int camNum = 0; camNum = Camera.getNumberOfCameras(); int camBackId = Camera.CameraInfo.CAMERA_FACING_BACK; int camFrontId = Camera.CameraInfo.CAMERA_FACING_FRONT; Camera.CameraInfo currentCamInfo = new Camera.CameraInfo(); //if camera is running if (camera != null){ //and there is more than one camera if (camNum > 1){ //stop current camera camera.stopPreview(); camera.setPreviewCallback(null); //camera.takePicture(null, null, PictureCallback); camera.release(); camera = null; //stop surfaceHolder? if (currentCamInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){ //switch camera to back camera camera=Camera.open(camBackId); } else{ //switch camera to front camera camera=Camera.open(camFrontId); } //switch camera back on //specify surface? try { camera.setPreviewDisplay(surfaceHolder); camera.setPreviewCallback((PreviewCallback) this); camera.startPreview(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 
 Button otherCamera = (Button) findViewById(R.id.OtherCamera); OtherCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (inPreview) { camera.stopPreview(); } //NB: if you don't release the current camera before switching, you app will crash camera.release(); //swap the id of the camera to be used if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){ currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT; } else { currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK; } camera = Camera.open(currentCameraId); setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera); try { camera.setPreviewDisplay(previewHolder); } catch (IOException e) { e.printStackTrace(); } camera.startPreview(); } 

如果要使相机图像以与显示相同的方向显示,可以使用以下代码。

 public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) { android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result); } 

首先你需要销毁前一个摄像头的SurfacePreview,然后需要创build一个新的摄像头对象(Back / Front)

 `//Code to destroy SurfacePreview mPreview.surfaceDestroyed(mPreview.getHolder()); mPreview.getHolder().removeCallback(mPreview); mPreview.destroyDrawingCache(); preview.removeView(mPreview); mCamera.stopPreview(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); //Now create new camera object if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { mCamera = Camera.open(camIdx); mPreview = new CameraPreview(CameraActivity.this, mCamera); preview.addView(mPreview); mCamera.setPreviewDisplay(mPreview.getHolder()); mCamera.startPreview(); }` 

尝试使用AsyncTask。 代码将运行在一个单独的线程,而不会干扰你的UI线程。 有几个如何在这个网站上使用AsyncTask的例子。