1 package com.visagetechnologies.visagetrackerdemo;
4 import java.io.IOException;
5 import java.lang.ref.WeakReference;
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;
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;
41 public final String TAG =
"StartupActivity";
43 final int TRACK_GREYSCALE = 2;
44 final int TRACK_RGB = 0;
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;
51 private Handler mHandler =
new Handler();
53 private static boolean licenseMessageShown =
false;
55 public static float AverageFPS = 0;
56 public static int FPSCounter = 0;
59 public static boolean wait =
false;
66 boolean liveness =
false;
69 public void onCreate(Bundle savedInstanceState) {
70 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
71 super.onCreate(savedInstanceState);
72 Bundle bundle = getIntent().getExtras();
74 Drawable logo = ResourcesCompat.getDrawable(this.getResources(), R.drawable.logo, null);
75 Bitmap bitmapLogo = ((BitmapDrawable)logo).getBitmap();
78 Bitmap fontImage = null;
80 fontImage =
ImageTrackerView.
LoadFromFile(
this, Uri.fromFile(
new File(
"/data/data/com.visagetechnologies.visagetrackerdemo/files/font.png")));
81 }
catch (IOException e) {
86 type = bundle.getInt(
"type");
94 }
catch (IOException e) {
102 String uri = bundle.getString(
"imageURI");
103 Uri imageURI = Uri.parse(uri);
106 }
catch (IOException e) {
116 }
catch (IOException e) {
123 licenseMessageShown =
false;
129 public void onDestroy() {
131 if (cpreview != null && type == 0)
132 cpreview.closeCamera();
134 mHandler.removeCallbacksAndMessages(null);
139 public void onPause() {
145 public void onConfigurationChanged(Configuration newConfig) {
146 super.onConfigurationChanged(newConfig);
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;
159 int finalOrientation = 0;
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;
167 setParameters(cpreview.previewWidth, cpreview.previewHeight, finalOrientation, 1);
174 private final class CameraThread
implements Runnable {
175 public final String TAG =
"CameraThread";
179 private int Corientation;
182 protected CameraThread(
int width,
int height,
int flip,
int orientation) {
185 Corientation = orientation;
205 private final class ImageThread
implements Runnable {
206 public final String TAG =
"ImageThread";
211 protected ImageThread(
int width,
int height) {
222 private static final class DoneRunnable
implements Runnable {
223 public final String TAG =
"DoneRunnable";
225 private final WeakReference<TextView> mTextViewRef;
226 private final WeakReference<Handler> mHandlerRef;
228 protected DoneRunnable(TextView textView, Handler handler) {
229 mTextViewRef =
new WeakReference<TextView>(textView);
230 mHandlerRef =
new WeakReference<Handler>(handler);
234 final TextView tv = mTextViewRef.get();
243 if (!state.equals(
"OFF")) {
245 if (fps > 0 && fps < 100) {
251 if (fps > 0 && fps < 80) {
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) {
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) {
267 .setIcon(android.R.drawable.ic_dialog_alert)
270 licenseMessageShown =
true;
273 final Handler handler = mHandlerRef.get();
275 handler.postDelayed(
this, 200);
283 TrackerInit(getFilesDir().getAbsolutePath(),
"Facial Features Tracker.cfg");
287 String pathToImage =
"/data/data/com.visagetechnologies.visagetrackerdemo/files/faceBlank.png";
289 WriteFaceContourImage(pathToImage,
ImageTrackerView.
ConvertToByte(faceContourImage,
true), faceContourImage.getWidth(), faceContourImage.getHeight());
291 cameraId = getCameraId();
293 FrameLayout layout =
new FrameLayout(
this);
294 layout.setLayoutParams(
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
296 LinearLayout.LayoutParams layoutParams=
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 640);
297 layoutParams.gravity=Gravity.CENTER;
298 cpreview.setLayoutParams(layoutParams);
300 layout.addView(cpreview);
304 layout.addView(tGLView);
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);
314 mHandler.postDelayed(
new DoneRunnable(tv, mHandler), 200);
316 RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
318 setContentView(layout);
323 RelativeLayout GameButtons;
324 GameButtons =
new RelativeLayout(
this);
327 Button btnLiveness =
new Button(
this);
328 btnLiveness.setText(
"Restart liveness");
329 btnLiveness.setId(R.id.liveness_btn_id);
331 RelativeLayout.LayoutParams b1 =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
333 RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
335 GameButtons.setLayoutParams(params);
337 GameButtons.addView(btnLiveness);
339 b1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
340 b1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
342 btnLiveness.setLayoutParams(b1);
344 layout.addView(GameButtons);
345 setContentView(layout);
347 btnLiveness.setOnClickListener(
new View.OnClickListener() {
348 public void onClick(View v) {
354 setContentView(layout);
355 Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
356 int screenOrientation = display.getRotation();
358 CameraInfo cameraInfo =
new CameraInfo();
359 Camera.getCameraInfo(cameraId, cameraInfo);
360 int orientation = cameraInfo.orientation;
362 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT){
366 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT)
367 CamThread =
new Thread(
new CameraThread(cpreview.previewWidth, cpreview.previewHeight, flip, (screenOrientation*90 + orientation)%360));
369 CamThread =
new Thread(
new CameraThread(cpreview.previewWidth, cpreview.previewHeight, flip, (orientation - screenOrientation*90 + 360)%360));
377 int numberOfCameras = Camera.getNumberOfCameras();
378 for (
int i = 0; i < numberOfCameras; i++) {
379 CameraInfo info =
new CameraInfo();
380 Camera.getCameraInfo(i, info);
382 if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
395 TrackerInit(getFilesDir().getAbsolutePath(),
"Facial Features Tracker.cfg");
397 RelativeLayout layout =
new RelativeLayout(
this);
399 layout.addView(ipreview);
403 layout.addView(tGLView);
405 tv =
new TextView(
this);
406 tv.setTextColor(Color.GREEN);
407 tv.setBackgroundColor(Color.BLACK);
410 mHandler.postDelayed(
new DoneRunnable(tv, mHandler), 200);
412 RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
414 layout.setLayoutParams(params);
415 setContentView(layout);
423 public void ShowDialog()
425 Intent intent =
new Intent(
this, WarningActivity.class);
426 startActivity(intent);
435 public void AlertDialogFunction(String message)
437 TextView title =
new TextView(
this);
438 title.setText(
"License warning");
439 title.setGravity(Gravity.CENTER);
440 title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
442 TextView msg =
new TextView(
this);
443 msg.setText(message);
444 msg.setGravity(Gravity.CENTER);
445 msg.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
447 new AlertDialog.Builder(
this)
448 .setCustomTitle(title)
450 .setPositiveButton(android.R.string.yes, null)
451 .setIcon(android.R.drawable.ic_dialog_alert)
460 public native
void TrackerInit(String pathToAssets, String configFilename);
462 public native
void LivenessInit();
464 public native
void PerformLiveness();
466 public native
void RestartLiveness();
484 public static native
float GetFps();
509 public static native
void setParameters(
int width,
int height,
510 int orientation,
int flip);
515 public native
void PauseTracker();
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);
523 System.loadLibrary(
"VisageVision");
524 System.loadLibrary(
"VisageWrapper");
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.
Derives from TrackerView.
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...
Derives from TrackerView.
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.