android - CameraSource(vision api) not detecting when phone is in landscape mode -
i have created app track eyes using vision api , working fine when phone portrait mode when tilde phone landscape position app pauses camera , went onmissing() method.
please give me suggestion app can work in both layouts or can sense eyes phone rotations (0,90,180,270)
code:
private void createcameraresources() { context context = getapplicationcontext(); // create , setup 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()); } }
manifest file:
<?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>
corresponding xml layout:
<?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>
you need check orientation change , like,
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 , height sizes when in portrait, since rotated // 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 , height sizes when in portrait, since 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 default"); return false; }
this official android vision github page.
Comments
Post a Comment