2017-01-20 17 views
3

顔トラッカーアプリはGoogle Vision Face Trackerに基づいています。デフォルトでは、Face Trackerはリア/バックカメラを使用しますが、フロントカメラで顔を検出したいと思います。Face Tracker CameraSource Android:どのように前面のカメラの品質を明るくするには?

これは、Googleのビジョンが提供するCameraSourcePreviewためのコードです:

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

私はこの方法でカメラソースを呼び出す:

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

フェイストラッカーフロントカメラはまだ暗すぎるデフォルトの電話のカメラアプリとの比較。

face tracker google visionでフロントカメラを明るくするにはどうすればいいですか?サーフェスビューに関連していますか?

<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()を使用して最小値と最大値の間で新しい露出を設定し、Camera.Parametersをカメラに適用するだけです。

最後に、あなたはsetAutoExposureLock()getAutoExposureLock()を使用して設定を失い、最も重要避けるために露出を固定することができます:露出ロックを設定する前に、あなたはisAutoExposureLockSupported()が真を返していることを確認する必要があります。

Good Luck!

関連する問題