2016-02-18 80 views
4

在我的相機應用程序中,我有一個按鈕可將相機朝前或朝後改變,我可以使用後置相機捕捉和保存圖像,但是當我切換到前置相機時,我無法捕捉圖像。這是我如何切換相機前面或後面。相機2 - API - 使用前置攝像頭時,圖像捕捉不起作用?

ImageView switch_camera =(ImageView) rootview.findViewById(R.id.imageView7); 
     switch_camera.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


     //  facing = characteristics.get(CameraCharacteristics.LENS_FACING); 

       if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) { 
        //isfrontcam=true; 
        try { 

         //manager.openCamera(getBackFacingCameraId(manager), mStateCallback, mBackgroundHandler); 
         closeCamera(); 
         openCamera(mTextureView.getWidth(), mTextureView.getHeight(),"0"); 
         Log.e("opening ","BackCam"); 
         facing = 1; 

        } catch (SecurityException e) { 
         e.printStackTrace(); 

        } catch (Exception e) { 
         e.printStackTrace(); 

        } 
       } else if (facing != null && facing == CameraCharacteristics.LENS_FACING_BACK) { 
        // isfrontcam = true; 
        try { 
         //manager.openCamera(getFrontFacingCameraId(manager), mStateCallback, mBackgroundHandler); 

         // characteristics = manager.getCameraCharacteristics("1"); 

         closeCamera(); 
         openCamera(mTextureView.getWidth(), mTextureView.getHeight(),"1"); 

         Log.e("opening ", "FrontCam"); 
         String str = getBackFacingCameraId(manager); 
         facing= 0; 
         Log.e("str", "id" + str); 

        } catch (SecurityException e) { 
         e.printStackTrace(); 

        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 

在捕獲按鈕點擊,我打電話這個功能捕獲圖像;

private void lockFocus() { 
     try { 
      // This is how to tell the camera to lock focus. 
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, 
        CameraMetadata.CONTROL_AF_TRIGGER_START); 
      // Tell #mCaptureCallback to wait for the lock. 
      mState = STATE_WAITING_LOCK; 
      mCaptureSession.capture(mPreviewRequestBuilder.build(),mCaptureCallback, 
        mBackgroundHandler); 
     } catch (CameraAccessException e) { 

      e.printStackTrace(); 
     } 
    } 

回答

11

檢查CameraCaptureSession.CaptureCallback: 可能是相機的狀態CONTROL_AF_STATE_INACTIVE。而且,因爲它正在等待焦點......照片永遠不會被拍攝。

它應該是這樣的

  case STATE_WAITING_LOCK: { 
       Integer afState = result.get(CaptureResult.CONTROL_AF_STATE); 
       if (afState == null) { 
        captureStillPicture(); 
       } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || 
         CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState || 
         CaptureResult.CONTROL_AF_STATE_INACTIVE == afState /*add this*/) { 
        // CONTROL_AE_STATE can be null on some devices 
        Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); 
        if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) { 
         mState = STATE_PICTURE_TAKEN; 
         captureStillPicture(); 
        } else { 
         runPrecaptureSequence(); 
        } 
       } 
       break; 
      } 
+0

爲我工作:)感謝 –

+0

也許你知道如何解決我的問題,我怎麼能拍攝照片只有當相機對焦?有我的問題http://stackoverflow.com/questions/40185407/how-to-take-picture-only-if-image-in-focus-camera-2-api在此先感謝! –

+1

這個答案允許我在標準模擬器中運行Camera2basic示例。謝謝 :) – Isolin