visageSDK
VisageWrapper.java
1 package com.visagetechnologies.showcasedemo;
2 
3 import android.content.Context;
4 import android.graphics.Bitmap;
5 import android.graphics.drawable.BitmapDrawable;
6 import android.graphics.drawable.Drawable;
7 import android.os.Handler;
8 import android.os.HandlerThread;
9 import android.os.Message;
10 import android.os.Process;
11 import androidx.core.content.res.ResourcesCompat;
12 import android.util.Log;
13 import android.view.View;
14 
15 import java.nio.ByteBuffer;
16 
17 public class VisageWrapper {
18 
19  static final int DISPLAY_FEATURE_POINTS = 1;
20  static final int DISPLAY_SPLINES = 2;
21  static final int DISPLAY_GAZE = 4;
22  static final int DISPLAY_IRIS = 8;
23  static final int DISPLAY_AXES = 16;
24  static final int DISPLAY_FRAME = 32;
25  static final int DISPLAY_WIRE_FRAME = 64;
26  static final int DISPLAY_TRACKING_QUALITY = 128;
27 
28  static final int DISPLAY_POINT_QUALITY = 256;
29 
30  static final int DISPLAY_AGE = 512;
31  static final int DISPLAY_GENDER = 1024;
32  static final int DISPLAY_EMOTIONS = 2048;
33  static final int DISPLAY_DEFAULT = DISPLAY_FEATURE_POINTS + DISPLAY_SPLINES + DISPLAY_GAZE + DISPLAY_IRIS + DISPLAY_AXES + DISPLAY_TRACKING_QUALITY + DISPLAY_POINT_QUALITY;
34  public static final int NUM_EMOTIONS = 6; //should be 6 because neutral emotion won't be displayed
35 
36  public enum TrackFrom {
37  CAMERA,
38  MEDIA
39  };
40 
41  private static volatile VisageWrapper instance = null;
42  private Context ctx;
43  private static final String TAG = "VisageWrapper";
44 
45  private VisageWorkerThread visageWorkerThread = null;
46  private Handler visageWorkerHandler = null;
47 
48  private String path;
49  TrackFrom trackFrom;
50  private boolean trackerStarted = false;
51 
52  private TrackerView trackerView = null;
53 
54  private Drawable logo;
55 
56  static VisageWrapper get(Context context) {
57  if (instance == null) {
58  synchronized (VisageWrapper.class) {
59  if (instance == null) {
60  instance = new VisageWrapper(context);
61  }
62  }
63  }
64  return instance;
65  }
66 
67  private VisageWrapper(Context context) {
68  ctx = context;
69  path = ctx.getFilesDir().getAbsolutePath();
70 
71  trackFrom = TrackFrom.CAMERA;
72  trackerView = new TrackerView(context, this);
73  SetDisplayOptions(DISPLAY_DEFAULT);
74 
75 
76  logo = ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null);
77  }
78 
79 
80  void onCreate() {
81  if (visageWorkerThread == null) {
82  startVisageWorker();
83  }
84 
85  visageWorkerHandler.sendMessage(visageWorkerHandler.obtainMessage(VisageWorkerThread.MSG_INIT));
86  }
87 
88  void onResume() {
89 
90  trackerView.onResume();
91 
92  Log.d(TAG, "backgroundHandler");
93  visageWorkerHandler.sendMessage(visageWorkerHandler.obtainMessage(VisageWorkerThread.MSG_START_TRACKER));
94  }
95 
96  void onPause() {
97  trackerView.onPause();
98  PauseTracker();
99  }
100 
101  void onDestroy() {
102  TrackerStop();
103  if (visageWorkerThread == null)
104  return;
105 
106  stopVisageWorker();
107 
108  }
109 
110  private void startVisageWorker() {
111  visageWorkerThread = new VisageWorkerThread();
112  visageWorkerThread.start();
113 
114  visageWorkerHandler = new Handler(visageWorkerThread.getLooper()) {
115  @Override
116  public void handleMessage(Message msg) {
117  switch (msg.what) {
118  case VisageWorkerThread.MSG_START_TRACKER:
119  visageWorkerThread.startTracker();
120  break;
121  case VisageWorkerThread.MSG_INIT:
122  visageWorkerThread.initialize();
123  break;
124  case VisageWorkerThread.MSG_RELEASE:
125  visageWorkerThread.release();
126  break;
127  default:
128  throw new RuntimeException("Not known msg.what");
129  }
130  }
131  };
132  }
133 
134  private void stopVisageWorker() {
135  visageWorkerThread.quitSafely();
136  try {
137  visageWorkerThread.join();
138  visageWorkerThread = null;
139  visageWorkerHandler = null;
140  } catch (InterruptedException e) {
141  Log.w(TAG, "Error stopping background thread", e);
142  }
143  }
144 
145  void switchToCamera() {
146  PauseTracker();
147  trackFrom = TrackFrom.CAMERA;
148  }
149 
150  void switchToMedia() {
151  PauseTracker();
152  trackFrom = TrackFrom.MEDIA;
153  }
154 
155  void toggleEars() {
156  ToggleEars(path);
157  }
158 
159  void toggleIris() {
160  ToggleIris();
161  }
162  void toggleRefineLandmarks(boolean enableOrDisable) {
163  ToggleRefineLandmarks(enableOrDisable);
164  }
165  void initAnalyser() { InitAnalyser();};
166 
167  boolean isTrackingFromMedia() {
168  return trackFrom == TrackFrom.MEDIA;
169  }
170 
171  View getTrackerView() {
172  return trackerView;
173  }
174 
175  private class VisageWorkerThread extends HandlerThread {
176 
177  private static final int MSG_START_TRACKER = 1;
178  private static final int MSG_INIT = 2;
179  private static final int MSG_RELEASE = 3;
180  private Bitmap bitmapLogo;
181 
182  private VisageWorkerThread() {
183  super("VisageWorkerThread", Process.THREAD_PRIORITY_DISPLAY);
184 
185  bitmapLogo = ((BitmapDrawable) logo).getBitmap();
186  }
187 
188  void initialize() {
189  TrackerInit(path, "Facial Features Tracker.cfg");
190 
191  WriteLogoImage(MediaLoader.ImageLoader.ConvertToByte(bitmapLogo, true), bitmapLogo.getWidth(), bitmapLogo.getHeight());
192  }
193 
194  void startTracker() {
195  if (trackerStarted)
196  return;
197 
198  trackerStarted = true;
199 
200  TrackLoop();
201 
202  trackerStarted = false;
203  }
204 
205  public void release() {
206  TrackerStop();
207  trackerStarted = false;
208  }
209  }
210 
211  public native void TrackerInit(String path, String configFilename);
212 
213  public static native void SetParameters(int width, int height, int orientation, int flip);
214 
215  public static native void SetParameters(int width, int height);
216 
217  public native void TrackerStop();
218 
219  public native void PauseTracker();
220 
221  public native void ResumeTracker();
222 
223  public native void SetDisplayOptions(int displayO);
224 
225  public native void ToggleEars(String path);
226 
227  public native void ToggleIris();
228 
229  public native void ToggleRefineLandmarks(boolean enableOrDisable);
230 
231  public native void TrackLoop();
232 
233  public static native void WriteFrameStream(ByteBuffer frameChannel0, ByteBuffer frameChannel1, ByteBuffer frameChannel2, long timestampA, long frameID, int pixelStride);
234 
235  public static native void WriteFrameImage(byte[] frame, int width, int height);
236 
237  public native boolean DisplayTrackingStatus(int width, int height);
238 
239  public static native float GetAge();
240 
241  public static native int GetGender();
242 
243  public static native float[] GetEmotions();
244 
245  public native void DeallocateResources();
246 
247  public native void AllocateResources();
248 
249  public native void ResetTextures();
250 
251  public native void SendCoordinates(float x, float y);
252 
253  public static native boolean ShowInstruction();
254 
255  public static native void WriteLogoImage(byte[] logo, int width, int height);
256 
257  public native void InitAnalyser();
258 }