2017-01-20 1 views
3

얼굴 추적기 앱은 Google Vision Face Tracker을 기반으로합니다. 기본적으로 Face Tracker는 후면/후면 카메라를 사용하지만 전면 카메라로 얼굴을 감지하고 싶습니다.Face Tracker CameraSource 안드로이드 : 어떻게 앞면 카메라의 화질을 밝게합니까?

package com.google.android.gms.samples.vision.face.facetracker.ui.camera; 

import android.content.Context; 
import android.content.res.Configuration; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.ViewGroup; 

import com.google.android.gms.common.images.Size; 
import com.google.android.gms.vision.CameraSource; 

import java.io.IOException; 

public class CameraSourcePreview extends ViewGroup { 
    private static final String TAG = "CameraSourcePreview"; 

    private Context mContext; 
    private SurfaceView mSurfaceView; 
    private boolean mStartRequested; 
    private boolean mSurfaceAvailable; 
    private CameraSource mCameraSource; 

    private GraphicOverlay mOverlay; 

    public CameraSourcePreview(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 
     mStartRequested = false; 
     mSurfaceAvailable = false; 

     mSurfaceView = new SurfaceView(context); 
     mSurfaceView.getHolder().addCallback(new SurfaceCallback()); 
     addView(mSurfaceView); 
    } 

    public void start(CameraSource cameraSource) throws IOException { 
     if (cameraSource == null) { 
      stop(); 
     } 

     mCameraSource = cameraSource; 

     if (mCameraSource != null) { 
      mStartRequested = true; 
      startIfReady(); 
     } 
    } 

    public void start(CameraSource cameraSource, GraphicOverlay overlay) throws IOException { 
     mOverlay = overlay; 
     start(cameraSource); 
    } 

    public void stop() { 
     if (mCameraSource != null) { 
      mCameraSource.stop(); 
     } 
    } 

    public void release() { 
     if (mCameraSource != null) { 
      mCameraSource.release(); 
      mCameraSource = null; 
     } 
    } 

    private void startIfReady() throws IOException { 
     if (mStartRequested && mSurfaceAvailable) { 
      mCameraSource.start(mSurfaceView.getHolder()); 
      if (mOverlay != null) { 
       Size size = mCameraSource.getPreviewSize(); 
       int min = Math.min(size.getWidth(), size.getHeight()); 
       int max = Math.max(size.getWidth(), size.getHeight()); 
       if (isPortraitMode()) { 
        // Swap width and height sizes when in portrait, since it will be rotated by 
        // 90 degrees 
        mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing()); 
       } else { 
        mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing()); 
       } 
       mOverlay.clear(); 
      } 
      mStartRequested = false; 
     } 
    } 

    private class SurfaceCallback implements SurfaceHolder.Callback { 
     @Override 
     public void surfaceCreated(SurfaceHolder surface) { 
      mSurfaceAvailable = true; 
      try { 
       startIfReady(); 
      } catch (IOException e) { 
       Log.e(TAG, "Could not start camera source.", e); 
      } 
     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder surface) { 
      mSurfaceAvailable = false; 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     } 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     int width = 640; 
     int height = 480; 
     if (mCameraSource != null) { 
      Size size = mCameraSource.getPreviewSize(); 
      if (size != null) { 
       width = size.getWidth(); 
       height = size.getHeight(); 
      } 
     } 

     // Swap width and height sizes when in portrait, since it will be rotated 90 degrees 
     if (isPortraitMode()) { 
      int tmp = width; 
      width = height; 
      height = tmp; 
     } 

     final int layoutWidth = right - left; 
     final int layoutHeight = bottom - top; 

     // Computes height and width for potentially doing fit width. 
     int childWidth = layoutWidth; 
     int childHeight = (int)(((float) layoutWidth/(float) width) * height); 

     // If height is too tall using fit width, does fit height instead. 
     if (childHeight > layoutHeight) { 
      childHeight = layoutHeight; 
      childWidth = (int)(((float) layoutHeight/(float) height) * width); 
     } 

     for (int i = 0; i < getChildCount(); ++i) { 
      getChildAt(i).layout(0, 0, childWidth, childHeight); 
     } 

     try { 
      startIfReady(); 
     } catch (IOException e) { 
      Log.e(TAG, "Could not start camera source.", e); 
     } 
    } 

    private boolean isPortraitMode() { 
     int orientation = mContext.getResources().getConfiguration().orientation; 
     if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      return false; 
     } 
     if (orientation == Configuration.ORIENTATION_PORTRAIT) { 
      return true; 
     } 

     Log.d(TAG, "isPortraitMode returning false by default"); 
     return false; 
    } 
} 

나는이 방법으로 카메라 소스를 호출 :

구글의 비전이 제공하는 CameraSourcePreview 코드입니다

private void startCameraSource() { 

     // check that the device has play services available. 
     int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
       getApplicationContext()); 
     if (code != ConnectionResult.SUCCESS) { 
      Dialog dlg = 
        GoogleApiAvailability.getInstance().getErrorDialog(this, code, RC_HANDLE_GMS); 
      dlg.show(); 
     } 

     if (mCameraSource != null) { 
      try { 
       mPreview.start(mCameraSource, mGraphicOverlay); 
      } catch (IOException e) { 
       Log.e(TAG, "Unable to start camera source.", e); 
       mCameraSource.release(); 
       mCameraSource = null; 
      } 
     } 
    } 

얼굴 추적기 전면 카메라 여전히 너무 어두운 기본 전화 카메라 응용 프로그램과 비교 .

얼굴 추적기 google 비전에서 앞면 카메라를 밝게하는 방법은 무엇입니까? 표면보기와 관련이 있습니까?

<com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview 
    android:id="@+id/preview" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1.00" 
    android:weightSum="1"> 

<com.google.android.gms.samples.vision.face.facetracker.ui.camera.GraphicOverlay 
    android:id="@+id/faceOverlay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="0.79" /> 

</com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview> 

답변

0

이것은 SurfaceView와 전혀 관련이 없습니다. 카메라 API 구성 오류입니다. CameraSource.java 파일에서 몇 가지 추가 변경을해야합니다. 찾을 수 있습니다. this GitHub repository

먼저 노출 문제인지 확인해야합니다. 카메라가 렌즈에서 수신 할 수있는 빛을 나타냅니다. 카메라가 노출 보정을 지원하는지 알아야합니다. Camera.Parameters 인스턴스에서 getMinExposureCompensation()getMaxExposureCompensation()을 쿼리해야합니다. 설명서에서 설명한 것처럼 두 방법 모두 0을 반환하면 노출 보정이 지원되지 않으며 수행 할 수있는 방법이 없습니다.

대부분의 경우이 특성은 모든 전화기에서 지원됩니다. 이제 getExposureCompensation()으로 전화하여 현재의 카메라 노출을 확인할 수 있습니다. 기본값은 노출 값이 조정되지 않았 음을 나타내는 0입니다. 이제 어두운 이미지을 방지하려면 setExposureCompensation()을 사용하여 최소값과 최대 값 사이의 새 노출을 설정하고 카메라에 매개 변수를 적용해야합니다.

마지막으로 setAutoExposureLock(), getAutoExposureLock() 및 그 중 가장 중요한 것을 사용하여 구성이 손실되지 않도록 노출을 잠글 수 있습니다. 노출 잠금을 설정하기 전에 isAutoExposureLockSupported()이 true로 반환되어야합니다.

행운을 빌어 요!

관련 문제