visageSDK
StartupActivity.java
1 package com.visagetechnologies.visagetrackerdemo;
2 
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 
8 import android.Manifest;
9 import android.app.Activity;
10 import android.content.Intent;
11 import android.content.pm.PackageManager;
12 import android.content.res.AssetManager;
13 import android.os.Bundle;
14 import androidx.core.app.ActivityCompat;
15 import androidx.core.content.ContextCompat;
16 import android.util.Log;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.view.WindowManager;
20 import android.widget.Button;
21 
24 public class StartupActivity extends Activity
25 {
26  public final String TAG = "StartupActivity";
27 
28  final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
29  final int MY_PERMISSIONS_REQUEST_STORAGE = 2;
30 
31  boolean camera = false;
32  boolean image = false;
33  boolean liveness = false;
34 
38  public void onCreate(Bundle savedInstanceState) {
39  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
40 
41  super.onCreate(savedInstanceState);
42  copyAssets(getFilesDir().getAbsolutePath());
43 
44  setContentView(R.layout.main);
45  Button buttonImageClick = (Button)findViewById(R.id.btnStartImage);
46  buttonImageClick.setOnClickListener(new OnClickListener() {
47  public void onClick(View v) {
48  image = true; camera = false; liveness = false;
50  }
51  });
52 
53  Button buttonCamClick = (Button)findViewById(R.id.btnStartCam);
54  buttonCamClick.setOnClickListener(new OnClickListener() {
55  public void onClick(View v) {
56  image = false; camera = true; liveness = false;
58  }
59  });
60 
61  Button buttonLivenessClick = (Button)findViewById(R.id.btnStartLiveness);
62  buttonLivenessClick.setOnClickListener(new OnClickListener() {
63  public void onClick(View v) {
64  image = false; camera = false; liveness = true;
66  }
67  });
68  }
69 
72  public void StartImages() {
73  Intent intent = new Intent(this,ImagesActivity.class);
74  startActivity(intent);
75  }
76 
79  public void StartCam(int type) {
80  Intent intent = new Intent(this, TrackerActivity.class);
81  Bundle bundle = new Bundle();
82  bundle.putInt("type", type);
83  intent.putExtras(bundle);
84  startActivity(intent);
85  }
86 
87  public void StartLiveness(int type) {
88  Intent intent = new Intent(this, TrackerActivity.class);
89  Bundle bundle = new Bundle();
90  bundle.putInt("type", type);
91  intent.putExtras(bundle);
92  startActivity(intent);
93  }
94 
97  public void handlePermissions() {
98  if (camera || liveness)
99  {
100  if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
101  ActivityCompat.requestPermissions(this,
102  new String[]{
103  Manifest.permission.CAMERA
104  },
105  MY_PERMISSIONS_REQUEST_CAMERA);
106 
107  return;
108  }
109 
110  if (camera)
111  StartCam(0);
112  else
113  StartLiveness(2);
114 
115  return;
116  }
117 
118  if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
119  ActivityCompat.requestPermissions(this,
120  new String[]{
121  Manifest.permission.READ_EXTERNAL_STORAGE,
122  Manifest.permission.READ_MEDIA_IMAGES,
123  Manifest.permission.READ_MEDIA_VIDEO,
124  },
125  MY_PERMISSIONS_REQUEST_STORAGE);
126 
127  return;
128  }
129 
130  StartImages();
131  }
132 
135  @Override
136  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
137  assert(requestCode == MY_PERMISSIONS_REQUEST_CAMERA || requestCode == MY_PERMISSIONS_REQUEST_STORAGE);
138 
139  // If request is cancelled, the result arrays are empty.
140  if (grantResults.length == 0) {
141  Log.e(TAG, "Failed to get permission: Cancelled request");
142  return;
143  }
144 
145  switch (requestCode) {
146  case MY_PERMISSIONS_REQUEST_CAMERA: {
147  if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
148  Log.e(TAG, "Failed to get permission: " + permissions[0]);
149  return;
150  }
151 
152  if (camera)
153  StartCam(0);
154  else
155  StartLiveness(2);
156 
157  return;
158  }
159  case MY_PERMISSIONS_REQUEST_STORAGE: {
160  // If request is cancelled, the result arrays are empty.
161  if (grantResults[0] == PackageManager.PERMISSION_DENIED) { // READ_EXTERNAL_STORAGE API < 33
162  for (int i = 1; i < grantResults.length; ++i) { // API > 33
163  if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
164  Log.e(TAG, "Failed to get permission: " + permissions[i]);
165  return;
166  }
167  }
168  }
169 
170  StartImages();
171  return;
172  }
173  }
174  }
175 
181  public void copyFile(String rootDir, String filename) {
182  AssetManager assetManager = this.getAssets();
183 
184  InputStream in = null;
185  OutputStream out = null;
186  try {
187  String newFileName = rootDir + File.separator + filename;
188  File file = new File(newFileName);
189 
190  if(!file.exists()) {
191  in = assetManager.open("trackerdata/" + filename);
192  out = new FileOutputStream(newFileName);
193 
194  byte[] buffer = new byte[4*1024];
195  int read;
196  while ((read = in.read(buffer)) != -1) {
197  out.write(buffer, 0, read);
198  }
199  out.flush();
200  out.close();
201  out = null;
202 
203  in.close();
204  in = null;
205  }
206  } catch (Exception e) {
207  Log.e("VisageTrackerDemo", e.getMessage());
208  }
209  }
210 
215  public void copyAssets(String rootDir) {
216  AssetManager assetManager = this.getAssets();
217 
218  String assets[] = null;
219  try {
220  assets = assetManager.list("trackerdata");
221 
222  for (String asset : assets) {
223  Log.i("VisageTrackerDemo", rootDir + File.separator + asset);
224  try {
225  if (!asset.contains("vfa") && !asset.contains("vfr")
226  && !asset.contains("vft")) copyFile(rootDir, asset);
227  } catch (Exception e) {
228  Log.e("VisageTrackerDemo", e.getMessage());
229  }
230  }
231  } catch (Exception e) {
232  Log.e("VisageTrackerDemo", e.getMessage());
233  }
234 
235  // create dirs
236  final String[] dirs = {
237  "vft",
238  "vft/er",
239  "vft/fa",
240  "vft/ff",
241  "vft/fm",
242  "vft/pr"
243  };
244 
245  for (String dirname : dirs) {
246  try {
247  File dir = new File(rootDir + File.separator + dirname);
248  if (!dir.exists()) dir.mkdir();
249  } catch (Exception e) {
250  Log.e("VisageTrackerDemo", e.getMessage());
251  }
252  }
253 
254  // copy files
255  final String[] files = {
256  "vft/er/efa.lbf",
257  "vft/er/efc.lbf",
258  "vft/fa/aux_file.bin",
259  "vft/fa/d1qy.tflite",
260  "vft/fa/d2.tflite",
261  "vft/ff/ff.tflite",
262  "vft/fm/candide3.fdp",
263  "vft/fm/candide3.wfm",
264  "vft/fm/jk_300.fdp",
265  "vft/fm/jk_300.wfm",
266  "vft/fm/jk_300.cfg",
267  "vft/fm/jk_300_wEars.fdp",
268  "vft/fm/jk_300_wEars.wfm",
269  "vft/fm/jk_300_wEars.cfg",
270  "vft/pr/pr.tflite"
271  };
272 
273  for (String filename : files) {
274  try {
275  Log.i("VisageTrackerDemo", rootDir + File.separator + filename);
276  copyFile(rootDir, filename);
277  } catch(Exception e) {
278  Log.e("VisageTrackerDemo", e.getMessage());
279  }
280  }
281  }
282 
283  static {
284  System.loadLibrary("c++_shared");
285  }
286 }
void copyFile(String rootDir, String filename)
Utility method called to copy required file to trackerdata folder.
Activity called to initiate and control tracking for either tracking from camera or image...
void StartImages()
Method called when user interacts with Track from image button.
void handlePermissions()
Method that handles permissions.
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
Method invoked when the user responds to permission request.
Activity started when application is ran.
void StartCam(int type)
Method called when user interacts with Track from cam button.
void onCreate(Bundle savedInstanceState)
Implementation of onCreate method provided by Activity interface.
void copyAssets(String rootDir)
Utility method called to create required directories and initiate copy of all assets required for tra...