2017-08-18 3 views
-2

J'ai créé une application pour suivre les yeux en utilisant Vision api et son fonctionnement bien lorsque le téléphone est en mode portrait, mais quand je tilde le téléphone en position paysage puis l'application pauses la caméra et est allé à la méthode onMissing().CameraSource (vision api) ne pas détecter lorsque le téléphone est en mode paysage

S'il vous plaît me donner quelques suggestions afin que l'application peut fonctionner dans les deux mises en page ou il peut sentir les yeux de toute rotation de téléphone (0,90,180,270)

code:

private void createCameraResources() { 
    Context context = getApplicationContext(); 

    // create and setup the face detector 
    mFaceDetector = new FaceDetector.Builder(context) 
      .setProminentFaceOnly(true) 
      .setTrackingEnabled(true) 
      .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS) 
      .setMode(FaceDetector.FAST_MODE) 
      .build(); 

    mFaceDetector.setProcessor(new LargestFaceFocusingProcessor(mFaceDetector, new FaceTracker())); 

    if (!mFaceDetector.isOperational()) { 
     Log.w(TAG, "createCameraResources: detector NOT operational"); 
    } else { 
     Log.d(TAG, "createCameraResources: detector operational"); 
    } 
    mCameraSource = new CameraSource.Builder(this, mFaceDetector) 
      .setRequestedPreviewSize(640, 480) 
      .setFacing(CameraSource.CAMERA_FACING_FRONT) 
      .setRequestedFps(30f) 
      .build(); 
} 



public class FaceTracker extends Tracker<Face> { 

private static final float PROB_THRESHOLD = 0.7f; 
private static final String TAG = FaceTracker.class.getSimpleName(); 
private boolean leftClosed; 
private boolean rightClosed; 

@Override 
public void onUpdate(Detector.Detections<Face> detections, Face face) { 
    if (leftClosed && face.getIsLeftEyeOpenProbability() > PROB_THRESHOLD) { 
     leftClosed = false; 
    } else if (!leftClosed && face.getIsLeftEyeOpenProbability() < PROB_THRESHOLD){ 
     leftClosed = true; 
    } 
    if (rightClosed && face.getIsRightEyeOpenProbability() > PROB_THRESHOLD) { 
     rightClosed = false; 
    } else if (!rightClosed && face.getIsRightEyeOpenProbability() < PROB_THRESHOLD) { 
     rightClosed = true; 
    } 

    if (leftClosed && !rightClosed) { 
     EventBus.getDefault().post(new LeftEyeClosedEvent()); 
    } else if (rightClosed && !leftClosed) { 
     EventBus.getDefault().post(new RightEyeClosedEvent()); 
    } else if (!leftClosed && !rightClosed) { 
     EventBus.getDefault().post(new NeutralFaceEvent()); 
    } 
} 

fichier Manifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.android.eyetoggle"> 

<uses-permission android:name="android.permission.CAMERA" /> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

Mise en page XML correspondant:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.percent.PercentFrameLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="20dp" 
tools:context="com.android.eyetoggle.MainActivity"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:orientation="horizontal"> 

    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/switchButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/emoticon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/emoji_neutral" /> 
</LinearLayout> 


<View 
    android:id="@+id/light" 
    android:layout_gravity="center" 
    android:background="@color/green" 
    app:layout_aspectRatio="100%" 
    app:layout_widthPercent="75%" /> 

</android.support.percent.PercentFrameLayout> 
+0

Avez-vous pensé à google pour 'android orientation'? –

+0

J'ai déjà googlé pour ce problème mais je n'ai rien trouvé. Pour l'api hardware.Camera il y a un moyen de régler l'orientation de l'affichage. – Puneet

+0

Il est Android de base ... –

Répondre

0

Vous devez vérifier un changement d'orientation et faire quelque chose comme,

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

@Override 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    int width = 320; 
    int height = 240; 
    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; 
    } 

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

C'est de l'Android officielle Vision github page.

+0

Merci @Shanky .. va vérifier ce code/lien et mettre à jour les résultats ici – Puneet

+0

comme ci-dessus le code, nous devons montrer l'aperçu de la caméra, puis seulement il va travailler pour le support de surface. Mais dans mon cas, l'aperçu de la caméra est faux, la caméra fonctionne en arrière-plan et, grâce à l'activité de service, elle fonctionne de manière continue, donc dans mon code, il n'y a pas de support de vue de surface. – Puneet