2015-10-14 22 views
7

Ho il codice per la Camera2 da https://github.com/googlesamples/android-Camera2Basic.Fotocamera anteriore in Camera2 non cattura immagine

Il problema che sto affrontando è che quando accendo la mia fotocamera frontale, non riesco a scattare foto ma funziona bene con la fotocamera posteriore.

Qualcuno ha implementato la Camera2 Api, per favore aiuto!

Ecco il frammento di codice:

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

si fa a testare il vero e proprio dispositivo o un emulatore? –

+0

Real Device Moto G2 – Veeresh

+0

Potete aiutarmi per favore – Veeresh

risposta

10

Per Camera2 Api, l'esempio di Google per il video grandi opere sia per la fotocamera anteriore e posteriore, ma per la cattura delle immagini, l'esempio di Google funziona solo per fotocamera posteriore e non per fotocamera frontale .
soluzione che funziona per me è

In lockFocus() metodo, sostituire la linea

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

con

captureStillPicture(); 

Spero che questo vi aiuterà !!

+1

Hai questo funziona bene !! ma l'immagine catturata viene ruotata di 180 gradi, Qualche soluzione per questo problema ?? – Gibs

+0

Grazie, questa soluzione ha funzionato per me. – dotfury

+0

@jaxon: dovrai ruotare l'immagine in base al dispositivo. Penso che gli esempi di Google abbiano questo nel codice. – dotfury

0

In lockFocus() metodo, sostituire la linea

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

con

captureStillPicture(); 

E per evitare che l'orientamento indesiderato è possibile utilizzare

/** * La conversione da rotazione dello schermo in JPEG orientamento. */

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

@ soluzione ArvindSingh non è il migliore, perché si dovrebbe anche disattivare la funzionalità di messa a fuoco per la fotocamera posteriore. Una soluzione migliore sarebbe quella di fare un fronte di controllo telecamera all'interno della takePicture così:

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(); 
    } 
} 

Per questa soluzione è sufficiente per salvare la corrente usata rivolto qualche parte come qui in 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

Il problema è che molte fotocamere frontali hanno una distanza di messa a fuoco fissa . Quindi, dopo il trigger della messa a fuoco automatica nello lockFocus() lo stato di messa a fuoco automatica (CONTROL_AF_STATE) rimane INACTIVE e il trigger della messa a fuoco automatica non fa nulla.

Quindi, per farlo funzionare è necessario verificare se l'autofocus è supportato o meno. A tale scopo, aggiungere il seguente al 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; 
} 

Infine non mi blocco messa a fuoco se non supportato quando si vuole scattare una foto:

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

Questo dovrebbe essere contrassegnato come risposta corretta. –

+0

ha funzionato per me. tks –

Problemi correlati