visageSDK
TrackerActivity.java
1 package com.visagetechnologies.visagetrackerdemo;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.ref.WeakReference;
6 
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.content.res.Configuration;
12 import android.graphics.Bitmap;
13 import android.graphics.Color;
14 import android.graphics.drawable.BitmapDrawable;
15 import android.graphics.drawable.Drawable;
16 import android.hardware.Camera;
17 import android.hardware.Camera.CameraInfo;
18 import android.net.Uri;
19 import android.os.Bundle;
20 import android.os.Handler;
21 import androidx.core.content.res.ResourcesCompat;
22 
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.Button;
26 import android.widget.FrameLayout;
27 import android.widget.LinearLayout;
28 import android.widget.RelativeLayout;
29 import android.widget.TextView;
30 import android.util.TypedValue;
31 import android.view.Display;
32 import android.view.Gravity;
33 import android.view.ViewGroup.LayoutParams;
34 import android.view.WindowManager;
35 
36 
39 public class TrackerActivity extends Activity
40 {
41  public final String TAG = "StartupActivity";
42 
43  final int TRACK_GREYSCALE = 2;
44  final int TRACK_RGB = 0;
45 
46  final int VISAGE_CAMERA_UP = 0;
47  final int VISAGE_CAMERA_DOWN = 1;
48  final int VISAGE_CAMERA_LEFT = 2;
49  final int VISAGE_CAMERA_RIGHT = 3;
50 
51  private Handler mHandler = new Handler();
52  private TextView tv;
53  private static boolean licenseMessageShown = false;
54 
55  public static float AverageFPS = 0;
56  public static int FPSCounter = 0;
57  public static TrackerActivity instance;
58 
59  public static boolean wait = false;
60  JavaCamTrackerView cpreview;
61  ImageTrackerView ipreview;
62 
63  int cameraId = -1;
64  int orientation;
65  int type;
66  boolean liveness = false;
67 
68  @Override
69  public void onCreate(Bundle savedInstanceState) {
70  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
71  super.onCreate(savedInstanceState);
72  Bundle bundle = getIntent().getExtras();
73 
74  Drawable logo = ResourcesCompat.getDrawable(this.getResources(), R.drawable.logo, null);
75  Bitmap bitmapLogo = ((BitmapDrawable)logo).getBitmap();
76  WriteLogoImage(ImageTrackerView.ConvertToByte(bitmapLogo, true), bitmapLogo.getWidth(), bitmapLogo.getHeight());
77 
78  Bitmap fontImage = null;
79  try {
80  fontImage = ImageTrackerView.LoadFromFile(this, Uri.fromFile(new File("/data/data/com.visagetechnologies.visagetrackerdemo/files/font.png")));
81  } catch (IOException e) {
82  e.printStackTrace();
83  }
84  WriteFontImage(ImageTrackerView.ConvertToByte(fontImage, true), fontImage.getWidth(), fontImage.getHeight());
85 
86  type = bundle.getInt("type");
87  switch(type)
88  {
89  case 0:
90  {
91  liveness = false;
92  try {
93  StartCam();
94  } catch (IOException e) {
95  e.printStackTrace();
96  }
97  break;
98  }
99  case 1:
100  {
101  liveness = false;
102  String uri = bundle.getString("imageURI");
103  Uri imageURI = Uri.parse(uri);
104  try {
105  StartImage(imageURI);
106  } catch (IOException e) {
107  e.printStackTrace();
108  }
109  break;
110  }
111  case 2:
112  {
113  liveness = true;
114  try {
115  StartCam();
116  } catch (IOException e) {
117  e.printStackTrace();
118  }
119  break;
120  }
121  }
122  TrackerActivity.instance = this;
123  licenseMessageShown = false;
124  AverageFPS = 0;
125  FPSCounter = 0;
126  }
127 
128  @Override
129  public void onDestroy() {
130  super.onDestroy();
131  if (cpreview != null && type == 0)
132  cpreview.closeCamera();
133  TrackerStop();
134  mHandler.removeCallbacksAndMessages(null);
135  }
136 
137 
138  @Override
139  public void onPause() {
140  super.onPause();
141  this.finish();
142  }
143 
144  @Override
145  public void onConfigurationChanged(Configuration newConfig) {
146  super.onConfigurationChanged(newConfig);
147  //Pause tracking while we change the orientation (image buffers, etc.)
148  //NOTE: We will restart tracking in the wrapper when the camera send the first changed frame
149  if (type !=1)
150  {
151  PauseTracker();
152  //
153  Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
154  int screenOrientation = display.getRotation();
155  CameraInfo cameraInfo = new CameraInfo();
156  Camera.getCameraInfo(cameraId, cameraInfo);
157  int orientation = cameraInfo.orientation;
158  //
159  int finalOrientation = 0;
160  // Calculate orientation value based on the screen and camera orientation
161  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
162  finalOrientation = (screenOrientation * 90 + orientation) % 360;
163  } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
164  finalOrientation = (orientation - screenOrientation*90 + 360)%360;
165  }
166 
167  setParameters(cpreview.previewWidth, cpreview.previewHeight, finalOrientation, 1);
168  }
169  }
174  private final class CameraThread implements Runnable {
175  public final String TAG = "CameraThread";
176 
177  private int Cwidth;
178  private int Cheight;
179  private int Corientation;
180  private int Cflip;
181 
182  protected CameraThread(int width, int height, int flip, int orientation) {
183  Cwidth = width;
184  Cheight = height;
185  Corientation = orientation;
186  Cflip = flip;
187  }
188 
189  @Override
190  public void run() {
191  setParameters(Cwidth, Cheight, Corientation, Cflip);
192  if (liveness)
193  PerformLiveness();
194  else
195  TrackFromCam();
196 
197  return;
198  }
199  }
200 
205  private final class ImageThread implements Runnable {
206  public final String TAG = "ImageThread";
207 
208  private int Iwidth;
209  private int Iheight;
210 
211  protected ImageThread(int width, int height) {
212  Iwidth = width;
213  Iheight = height;
214  }
215 
216  @Override
217  public void run() {
218  TrackFromImage(Iwidth, Iheight);
219  }
220  }
221 
222  private static final class DoneRunnable implements Runnable {
223  public final String TAG = "DoneRunnable";
224 
225  private final WeakReference<TextView> mTextViewRef;
226  private final WeakReference<Handler> mHandlerRef;
227 
228  protected DoneRunnable(TextView textView, Handler handler) {
229  mTextViewRef = new WeakReference<TextView>(textView);
230  mHandlerRef = new WeakReference<Handler>(handler);
231  }
232  @Override
233  public void run() {
234  final TextView tv = mTextViewRef.get();
235  if (tv!=null)
236  {
237  float fps = GetFps();
238  float displayFps = GetDisplayFps();
239  String state = GetStatus();
240 
241  int trackTime = GetTrackTime();
242 
243  if (!state.equals("OFF")) {
244  if (TrackerActivity.AverageFPS == 0) {
245  if (fps > 0 && fps < 100) {
246  TrackerActivity.AverageFPS = fps;
247  TrackerActivity.FPSCounter++;
248  }
249  }
250  else {
251  if (fps > 0 && fps < 80) {
252  TrackerActivity.FPSCounter++;
253  TrackerActivity.AverageFPS = TrackerActivity.AverageFPS + ((fps - TrackerActivity.AverageFPS) / TrackerActivity.FPSCounter);
254  }
255 
256  }
257 
258  tv.setText("FPS: " + String.format("%.2f", fps) + " (track " + String.format("%d", trackTime) + " ms)" + "\nDISPLAY FPS: " + String.format("%.2f", displayFps) + "\nStatus: " + state + "\nAvgFPS: " + String.format("%.2f", TrackerActivity.AverageFPS));
259  } else if (state.equals("OFF") && FPSCounter > 100 && licenseMessageShown == false) {
260  new AlertDialog.Builder(TrackerActivity.instance)
261  .setTitle("Warning")
262  .setMessage("You are using an unlicensed copy of visage|SDK\nTracking time is limited to one minute.\nPlease contact Visage Technologies to obtain a license key.\n30-day trial licenses are available.\nTracking will now stop.")
263  .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
264  public void onClick(DialogInterface dialog, int which) {
265  }
266  })
267  .setIcon(android.R.drawable.ic_dialog_alert)
268  .show();
269  tv.setText("");
270  licenseMessageShown = true;
271  }
272  }
273  final Handler handler = mHandlerRef.get();
274  if (handler != null)
275  handler.postDelayed(this, 200);
276  }
277  }
278 
282  public void StartCam() throws IOException {
283  TrackerInit(getFilesDir().getAbsolutePath(), "Facial Features Tracker.cfg");
284  if (liveness)
285  {
286  LivenessInit();
287  String pathToImage = "/data/data/com.visagetechnologies.visagetrackerdemo/files/faceBlank.png";
288  Bitmap faceContourImage = ImageTrackerView.LoadFromFile(this, Uri.fromFile(new File(pathToImage)));
289  WriteFaceContourImage(pathToImage, ImageTrackerView.ConvertToByte(faceContourImage, true), faceContourImage.getWidth(), faceContourImage.getHeight());
290  }
291  cameraId = getCameraId();
292  cpreview = new JavaCamTrackerView(this, this, cameraId);
293  FrameLayout layout = new FrameLayout(this);
294  layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
295 
296  LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 640);
297  layoutParams.gravity=Gravity.CENTER;
298  cpreview.setLayoutParams(layoutParams);
299 
300  layout.addView(cpreview);
301 
302  TrackerGLSurfaceView tGLView = new TrackerGLSurfaceView(this);
303  cpreview.setGLView(tGLView);
304  layout.addView(tGLView);
305 
306  if (!liveness)
307  {
308  final TextView tv = new TextView(this);
309  tv.setTextColor(Color.GREEN);
310  tv.setBackgroundColor(Color.BLACK);
311  LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
312  tv.setLayoutParams(lp);
313  layout.addView(tv);
314  mHandler.postDelayed(new DoneRunnable(tv, mHandler), 200);
315 
316  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
317 
318  setContentView(layout);
319  }
320  else
321  {
322 
323  RelativeLayout GameButtons;
324  GameButtons = new RelativeLayout(this);
325 
326  //set the properties for button
327  Button btnLiveness = new Button(this);
328  btnLiveness.setText("Restart liveness");
329  btnLiveness.setId(R.id.liveness_btn_id);
330 
331  RelativeLayout.LayoutParams b1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
332 
333  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
334 
335  GameButtons.setLayoutParams(params);
336 
337  GameButtons.addView(btnLiveness);
338 
339  b1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
340  b1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
341 
342  btnLiveness.setLayoutParams(b1);
343 
344  layout.addView(GameButtons);
345  setContentView(layout);
346 
347  btnLiveness.setOnClickListener(new View.OnClickListener() {
348  public void onClick(View v) {
349  RestartLiveness();
350  }
351  });
352  }
353 
354  setContentView(layout);
355  Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
356  int screenOrientation = display.getRotation();
357 
358  CameraInfo cameraInfo = new CameraInfo();
359  Camera.getCameraInfo(cameraId, cameraInfo);
360  int orientation = cameraInfo.orientation;
361  int flip = 0;
362  if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT){
363  flip = 1; // Mirror image from frontal camera
364  }
365  Thread CamThread;
366  if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT)
367  CamThread = new Thread(new CameraThread(cpreview.previewWidth, cpreview.previewHeight, flip, (screenOrientation*90 + orientation)%360));
368  else
369  CamThread = new Thread(new CameraThread(cpreview.previewWidth, cpreview.previewHeight, flip, (orientation - screenOrientation*90 + 360)%360));
370 
371  CamThread.start();
372 
373  }
374 
375  int getCameraId(){
376  int cameraId = -1;
377  int numberOfCameras = Camera.getNumberOfCameras();
378  for (int i = 0; i < numberOfCameras; i++) {
379  CameraInfo info = new CameraInfo();
380  Camera.getCameraInfo(i, info);
381  cameraId = i;
382  if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
383  break;
384  }
385  }
386  return cameraId;
387  }
388 
389 
394  public void StartImage(Uri imageURI) throws IOException {
395  TrackerInit(getFilesDir().getAbsolutePath(), "Facial Features Tracker.cfg");
396  ipreview = new ImageTrackerView(this, imageURI, this);
397  RelativeLayout layout = new RelativeLayout(this);
398 
399  layout.addView(ipreview);
400 
401  TrackerGLSurfaceView tGLView = new TrackerGLSurfaceView(this);
402  ipreview.setGLView(tGLView);
403  layout.addView(tGLView);
404 
405  tv = new TextView(this);
406  tv.setTextColor(Color.GREEN);
407  tv.setBackgroundColor(Color.BLACK);
408  layout.addView(tv);
409 
410  mHandler.postDelayed(new DoneRunnable(tv, mHandler), 200);
411 
412  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
413 
414  layout.setLayoutParams(params);
415  setContentView(layout);
416 
417  ipreview.SetOptimalBitmapSize();
418 
419  Thread ImageThread = new Thread(new ImageThread(ipreview.getBitmapWidth(), ipreview.getBitmapHeight()));
420  ImageThread.start();
421  }
422 
423  public void ShowDialog()
424  {
425  Intent intent = new Intent(this, WarningActivity.class);
426  startActivity(intent);
427  this.finish();
428  }
429 
430 
431  /*
432  * Method that is called if the product is unlicensed
433  */
434 
435  public void AlertDialogFunction(String message)
436  {
437  TextView title = new TextView(this);
438  title.setText("License warning");
439  title.setGravity(Gravity.CENTER);
440  title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
441 
442  TextView msg = new TextView(this);
443  msg.setText(message);
444  msg.setGravity(Gravity.CENTER);
445  msg.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
446 
447  new AlertDialog.Builder(this)
448  .setCustomTitle(title)
449  .setView(msg)
450  .setPositiveButton(android.R.string.yes, null)
451  .setIcon(android.R.drawable.ic_dialog_alert)
452  .show();
453  }
454 
455 
460  public native void TrackerInit(String pathToAssets, String configFilename);
461 
462  public native void LivenessInit();
463 
464  public native void PerformLiveness();
465 
466  public native void RestartLiveness();
467 
473  public native void TrackFromImage(int width, int height);
474 
478  public native void TrackFromCam();
479 
484  public static native float GetFps();
485 
490  public static native float GetDisplayFps();
491 
496  public static native String GetStatus();
497 
502  public static native int GetTrackTime();
503 
509  public static native void setParameters(int width, int height,
510  int orientation, int flip);
511 
514  public native void TrackerStop();
515  public native void PauseTracker();
516 
517  public native void WriteLogoImage(byte[] logo, int width, int height);
518  public native void WriteFaceContourImage(String path, byte[] faceContourImage, int width, int height);
519  public native void WriteFontImage(byte[] faceContourImage, int width, int height);
520 
521 
522  static {
523  System.loadLibrary("VisageVision");
524  System.loadLibrary("VisageWrapper");
525  }
526 }
void StartCam()
Starts tracking from camera and initializes surface for displaying tracking results.
static native float GetDisplayFps()
Interface to native method called to obtain rendering frame rate information.
Activity called to initiate and control tracking for either tracking from camera or image...
static Bitmap LoadFromFile(Activity activity, Uri uri)
Utility method called for loading the image from file into a Bitmap object.
void SetOptimalBitmapSize()
Utility method used to resize bitmap of image used for tracking.
native void TrackFromImage(int width, int height)
Interface to native method called to start tracking from image.
void StartImage(Uri imageURI)
Called to initialize and start tracking for image.
static native int GetTrackTime()
Interface to native method called to obtain pure tracking time.
void setGLView(TrackerGLSurfaceView tview)
Called to set GLSurfaceView used for rendering with OpenGL.
Implementation of GLSurfaceView class provided by Android.
native void TrackFromCam()
Prepare raw image interface to track from camera.
static byte [] ConvertToByte(Bitmap bitmap_, boolean opacity)
Utility method called to obtain raw pixel data from bitmap object in which image used for detection i...
static native String GetStatus()
Interface to native method called to obtain status of tracking information from tracker.
native void TrackerStop()
Interface to native method used to stop tracking.
native void TrackerInit(String pathToAssets, String configFilename)
Interface to native method called for initializing tracker.
static native float GetFps()
Interface to native method called to obtain frame rate information from tracker.
int getBitmapWidth()
Called to obtain width of bitmap used for tracking.
int getBitmapHeight()
Called to obtain height of bitmap used for tracking.
static native void setParameters(int width, int height, int orientation, int flip)
Interface to native method called to set camera frame parameteres.