2015-10-14 18 views
7

カメラ2のコードはhttps://github.com/googlesamples/android-Camera2Basicです。カメラ2の前面のカメラ画像が取り込まれていない

私が直面している問題は、フロントカメラに向けると写真を撮ることができませんが、バックカメラでは問題なく動作します。

Camera2 APIを実装した人は誰ですか?

ここでは、コードスニペットがあります:カメラ2 APIの

private void setUpCameraOutputs(int width, int height) { 
    Activity activity = getActivity(); 
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); 
    try { 
     for (String cameraId : manager.getCameraIdList()) { 
      CameraCharacteristics characteristics 
        = manager.getCameraCharacteristics(cameraId); 

      // We don't use a front facing camera in this sample. 
      int facing = characteristics.get(CameraCharacteristics.LENS_FACING); 
      Log.i(TAG,"Front Cam ID: "+ facing); 
      if (characteristics.get(CameraCharacteristics.LENS_FACING)==CameraCharacteristics.LENS_FACING_FRONT) 
      { 



       StreamConfigurationMap map = characteristics.get(
         CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 


       // For still image captures, we use the largest available size. 
       Size largest = Collections.max(
         Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), 
         new CompareSizesByArea()); 
       mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), 
         ImageFormat.JPEG, /*maxImages*/2); 
       mImageReader.setOnImageAvailableListener(
         mOnImageAvailableListener, mBackgroundHandler); 

       // Danger, W.R.! Attempting to use too large a preview size could exceed the camera 
       // bus' bandwidth limitation, resulting in gorgeous previews but the storage of 
       // garbage capture data. 
       mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), 
         width, height, largest); 

       // We fit the aspect ratio of TextureView to the size of preview we picked. 
       int orientation = getResources().getConfiguration().orientation; 
       if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 
        mTextureView.setAspectRatio(
          mPreviewSize.getWidth(), mPreviewSize.getHeight()); 
       } else { 
        mTextureView.setAspectRatio(
          mPreviewSize.getHeight(), mPreviewSize.getWidth()); 
       } 

       mCameraId = cameraId; 
       return; 
      } 
      else 
      { 
       onActivityCreated(Bundle.EMPTY); 
      } 
     } 
    } catch (CameraAccessException e) { 
     e.printStackTrace(); 
    } catch (NullPointerException e) { 
     // Currently an NPE is thrown when the Camera2API is used but not supported on the 
     // device this code runs. 
     ErrorDialog.newInstance(getString(R.string.camera_error)) 
       .show(getChildFragmentManager(), FRAGMENT_DIALOG); 
    } 
} 
+0

あなたは本当のデバイスまたはエミュレーター上でテストするのですか? –

+0

リアルデバイスMoto G2 – Veeresh

+0

私を助けてください – Veeresh

答えて

10

、ビデオのためのGoogleの例では、フロントとバックカメラの両方のための素晴らしい作品が、撮影のために、Googleの例では、唯一のバックカメラ用ではなく、フロントカメラのために働きます。私の作品
ソリューションはlockFocus()方法で

で、

captureStillPicture(); 

でライン

mCaptureSession.capture(mPreviewRequestBuilder.build(), 
                mCaptureCallback, mBackgroundHandler); 

を交換、これが役立つことを願っています!

lockFocus()メソッドで
+1

これはうまくいきます!キャプチャされた画像は180度回転していますが、この問題の解決策は何ですか? – Gibs

+0

ありがとう、このソリューションは私のために働いた。 – dotfury

+0

@jaxon:デバイスに基づいてイメージを回転する必要があります。 Googleの例ではコード内にこれがあると思います。 – dotfury

0

、あなたが

/** *変換画面の回転からJPEGに使用することができますライン

mCaptureSession.capture(mPreviewRequestBuilder.build(), 
               mCaptureCallback, mBackgroundHandler); 

captureStillPicture(); 

と、不要な方向性を防ぐために置き換えますオリエンテーション。あなたはまた、バックカメラのフォーカス機能を無効になるのでArvindSinghのソリューション@ */

static { 
     ORIENTATIONS.append(Surface.ROTATION_0, 270); 
     ORIENTATIONS.append(Surface.ROTATION_90, 0); 
     ORIENTATIONS.append(Surface.ROTATION_180, 180); 
     ORIENTATIONS.append(Surface.ROTATION_270, 270); 
    } 
2

は、最高のものではありません。よりよい解決策はそのようtakePicture内のチェックが直面しているカメラを行うには、次のようになります。このソリューションでは

private void takePicture() { 
    if (CameraCharacteristics.LENS_FACING_FRONT == mSelectedFacing) { 
     // front camera selected, so take a picture without focus 
     captureStillPicture(); 
    } else { 
     // back camera selected, trigger the focus before creating an image 
     lockFocus(); 
    } 
} 

あなただけの電流を保存する必要がどこかここmSelectedFacing

0
Plz try this code for capture 


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; 
      } 
16

のような問題に直面して使用多くの正面カメラは固定焦点距離のを持っています。したがって、lockFocus()のオートフォーカストリガーの後で、オートフォーカス状態(CONTROL_AF_STATE)はINACTIVEのままで、オートフォーカストリガーは何もしません。

したがって、動作させるには、オートフォーカスがサポートされているかどうかを確認する必要があります。そのためには、setUpCameraOutputs()に次の行を追加します。あなたは写真を撮りたいときにサポートされていない場合

int[] afAvailableModes = characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES); 

if (afAvailableModes.length == 0 || (afAvailableModes.length == 1 
     && afAvailableModes[0] == CameraMetadata.CONTROL_AF_MODE_OFF)) { 
    mAutoFocusSupported = false; 
} else { 
    mAutoFocusSupported = true; 
} 

は、最後にフォーカスをロックしない:

private void takePicture() { 
    if (mAutoFocusSupported) { 
     lockFocus(); 
    } else { 
     captureStillPicture(); 
    } 
} 
+0

これは正解とマークする必要があります。 –

+0

私のために働いた。 TKS –

関連する問題