0

我希望能够在启动应用程序时使用此代码从后到前(或从前到后)切换相机,第一个开关可以工作,但其他开关不能:

  public void changeCameraOnFly(View v)
  {
       if (isPreview) {camera.stopPreview();}
       camera.release();

           System.out.println("Camera ID before : " + String.valueOf(cameraID));
       if(cameraID == 0) {cameraID++;}
       if(cameraID == 1) {cameraID--;}
       System.out.println("Camera ID after: " + String.valueOf(cameraID));

           camera = Camera.open(cameraID);
           try {
                //this step is critical or preview on new camera will no know where to render to
                camera.setPreviewDisplay(surfaceCamera.getHolder());
           }
           catch (IOException e) { e.printStackTrace(); }
       camera.startPreview();
  }

调试日志:

06-17 18:27:43.594: I/System.out(10551): Camera ID before : 1
06-17 18:27:43.594: I/System.out(10551): Camera ID after: 0
06-17 18:27:46.074: I/System.out(10551): Camera ID before : 0
06-17 18:27:46.074: I/System.out(10551): Camera ID after: 0
4

1 回答 1

2

何时cameraId为 0,您的第一个 if 语句为真。因此cameraId递增到 1。

然后你的应用程序继续到下一行,在那里你检查是否cameraId为 1。确实是,所以cameraId递减回 0。

尝试改用 if-else。例如:

if (cameraId == 0) {
    cameraId++;
} else if (cameraId == 1) {
    cameraId--;
} else {
    // Default: Do nothing
}
于 2013-06-17T16:41:53.563 回答