1 package com.visagetechnologies.showcasedemo;
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;
15 import java.nio.ByteBuffer;
17 public class VisageWrapper {
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;
28 static final int DISPLAY_POINT_QUALITY = 256;
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;
36 public enum TrackFrom {
41 private static volatile VisageWrapper instance = null;
43 private static final String TAG =
"VisageWrapper";
45 private VisageWorkerThread visageWorkerThread = null;
46 private Handler visageWorkerHandler = null;
50 private boolean trackerStarted =
false;
52 private TrackerView trackerView = null;
54 private Drawable logo;
56 static VisageWrapper
get(Context context) {
57 if (instance == null) {
58 synchronized (VisageWrapper.class) {
59 if (instance == null) {
60 instance =
new VisageWrapper(context);
67 private VisageWrapper(Context context) {
69 path = ctx.getFilesDir().getAbsolutePath();
71 trackFrom = TrackFrom.CAMERA;
72 trackerView =
new TrackerView(context,
this);
73 SetDisplayOptions(DISPLAY_DEFAULT);
76 logo = ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null);
81 if (visageWorkerThread == null) {
85 visageWorkerHandler.sendMessage(visageWorkerHandler.obtainMessage(VisageWorkerThread.MSG_INIT));
90 trackerView.onResume();
92 Log.d(TAG,
"backgroundHandler");
93 visageWorkerHandler.sendMessage(visageWorkerHandler.obtainMessage(VisageWorkerThread.MSG_START_TRACKER));
97 trackerView.onPause();
103 if (visageWorkerThread == null)
110 private void startVisageWorker() {
111 visageWorkerThread =
new VisageWorkerThread();
112 visageWorkerThread.start();
114 visageWorkerHandler =
new Handler(visageWorkerThread.getLooper()) {
116 public void handleMessage(Message msg) {
118 case VisageWorkerThread.MSG_START_TRACKER:
119 visageWorkerThread.startTracker();
121 case VisageWorkerThread.MSG_INIT:
122 visageWorkerThread.initialize();
124 case VisageWorkerThread.MSG_RELEASE:
125 visageWorkerThread.release();
128 throw new RuntimeException(
"Not known msg.what");
134 private void stopVisageWorker() {
135 visageWorkerThread.quitSafely();
137 visageWorkerThread.join();
138 visageWorkerThread = null;
139 visageWorkerHandler = null;
140 }
catch (InterruptedException e) {
141 Log.w(TAG,
"Error stopping background thread", e);
145 void switchToCamera() {
147 trackFrom = TrackFrom.CAMERA;
150 void switchToMedia() {
152 trackFrom = TrackFrom.MEDIA;
162 void toggleRefineLandmarks(
boolean enableOrDisable) {
163 ToggleRefineLandmarks(enableOrDisable);
165 void initAnalyser() { InitAnalyser();};
167 boolean isTrackingFromMedia() {
168 return trackFrom == TrackFrom.MEDIA;
171 View getTrackerView() {
175 private class VisageWorkerThread
extends HandlerThread {
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;
182 private VisageWorkerThread() {
183 super(
"VisageWorkerThread", Process.THREAD_PRIORITY_DISPLAY);
185 bitmapLogo = ((BitmapDrawable) logo).getBitmap();
189 TrackerInit(path,
"Facial Features Tracker.cfg");
191 WriteLogoImage(MediaLoader.ImageLoader.ConvertToByte(bitmapLogo,
true), bitmapLogo.getWidth(), bitmapLogo.getHeight());
194 void startTracker() {
198 trackerStarted =
true;
202 trackerStarted =
false;
205 public void release() {
207 trackerStarted =
false;
211 public native
void TrackerInit(String path, String configFilename);
213 public static native
void SetParameters(
int width,
int height,
int orientation,
int flip);
215 public static native
void SetParameters(
int width,
int height);
217 public native
void TrackerStop();
219 public native
void PauseTracker();
221 public native
void ResumeTracker();
223 public native
void SetDisplayOptions(
int displayO);
225 public native
void ToggleEars(String path);
227 public native
void ToggleIris();
229 public native
void ToggleRefineLandmarks(
boolean enableOrDisable);
231 public native
void TrackLoop();
233 public static native
void WriteFrameStream(ByteBuffer frameChannel0, ByteBuffer frameChannel1, ByteBuffer frameChannel2,
long timestampA,
long frameID,
int pixelStride);
235 public static native
void WriteFrameImage(byte[] frame,
int width,
int height);
237 public native
boolean DisplayTrackingStatus(
int width,
int height);
239 public static native
float GetAge();
241 public static native
int GetGender();
243 public static native
float[] GetEmotions();
245 public native
void DeallocateResources();
247 public native
void AllocateResources();
249 public native
void ResetTextures();
251 public native
void SendCoordinates(
float x,
float y);
253 public static native
boolean ShowInstruction();
255 public static native
void WriteLogoImage(byte[] logo,
int width,
int height);
257 public native
void InitAnalyser();