visageSDK
ImageDetectorView.java
1 package com.visagetechnologies.facedetectdemo;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 
9 import android.app.Activity;
10 import android.content.Context;
11 import android.graphics.Bitmap;
12 import android.graphics.BitmapFactory;
13 import android.graphics.Canvas;
14 import android.graphics.ImageDecoder;
15 import android.graphics.drawable.BitmapDrawable;
16 import android.graphics.drawable.Drawable;
17 import android.net.Uri;
18 import android.os.Build;
19 import android.util.Log;
20 import android.view.SurfaceHolder;
21 
22 import androidx.core.content.res.ResourcesCompat;
23 
28 public class ImageDetectorView extends DetectorView implements Runnable
29 {
33  Bitmap _bitmap;
34 
38  Uri _uri;
39  Context _context;
40  boolean _isDetected;
41  boolean _detect;
42  String pathToGallery;
43 
49  ImageDetectorView(Context context, Uri imageUri, boolean detect) throws IOException {
50  super(context);
51  _context = context;
52  _uri = imageUri;
53  _detect = detect;
54  _isDetected = false;
55  _bitmap = LoadFromFile((Activity) context, _uri);
56  getHolder().addCallback(this);
57 
58  Drawable logo = ResourcesCompat.getDrawable(this.getResources(), R.drawable.logo, null);
59  Bitmap bitmapLogo = ((BitmapDrawable)logo).getBitmap();
60  WriteLogoImage(ConvertToByte(bitmapLogo, true), bitmapLogo.getWidth(), bitmapLogo.getHeight());
61 
62  pathToGallery = context.getFilesDir().getAbsolutePath() + "/Famous Actors/gallery";
63  String pathToFont = context.getFilesDir().getAbsolutePath() + "/font.png";
64 
65 
66  Bitmap font = LoadFromFile((Activity) context, Uri.fromFile(new File(pathToFont)));
67  WriteFontImage(ConvertToByte(font, true), font.getWidth(), font.getHeight());
68  }
69 
74  public void surfaceCreated(SurfaceHolder holder)
75  {
76  setKeepScreenOn(true);
77  _bitmap = SetOptimalBitmapSize(_bitmap);
78 
79  if (_detect)
80  DetectFeatures(ConvertToByte(_bitmap, false), _bitmap.getWidth(), _bitmap.getHeight());
81  else
82  {
83  try {
84  CreateGallery();
85  } catch (IOException e) {
86  e.printStackTrace();
87  }
88  recognizeFace(ConvertToByte(_bitmap, false), _bitmap.getWidth(), _bitmap.getHeight());
89  }
90  }
91 
95  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
96  {
97  _width = width;
98  _height = height;
99  }
100 
104  public void surfaceDestroyed(SurfaceHolder holder)
105  {
106 
107  }
108 
109 
114  public int getBitmapWidth()
115  {
116  return _bitmap.getWidth();
117  }
118 
123  public int getBitmapHeight()
124  {
125  return _bitmap.getHeight();
126  }
127 
133  public Bitmap SetOptimalBitmapSize(Bitmap bitmap)
134  {
135  int maxW = 1000;
136  int maxH = 1000;
137  int minW = 320;
138  int minH = 320;
139 
140  int w = bitmap.getWidth();
141  int h = bitmap.getHeight();
142 
143 
144  if(bitmap.getWidth() < maxW && bitmap.getHeight() < maxH && bitmap.getWidth() > minW && bitmap.getHeight() > minH && bitmap.getWidth() % 4 == 0 && bitmap.getHeight() % 4 == 0)
145  {
146  return bitmap;
147  }
148  int bitmapW = bitmap.getWidth();
149  int bitmapH = bitmap.getHeight();
150 
151  if(bitmap.getWidth() > bitmap.getHeight())
152  {
153  float ratio = (float)bitmapH / (float)bitmapW;
154  while(true)
155  {
156  if(w <= maxW && w >= minW)
157  {
158  break;
159  }
160  else if (w > maxW)
161  {
162  w = w / 2;
163  }
164  else if (w < minW)
165  {
166  w = w * 2;
167  }
168 
169  }
170  h = Math.round(ratio * w);
171  }
172 
173  if(bitmap.getWidth() < bitmap.getHeight())
174  {
175  float ratio = (float)bitmapW / (float)bitmapH;
176  while(true)
177  {
178  if(h <= maxH && h >= minH)
179  {
180  break;
181  }
182  else if (h > maxH)
183  {
184  h = h / 2;
185  }
186  else if (h < minH)
187  {
188  h = h * 2;
189  }
190 
191  }
192  w = Math.round(ratio * h);
193  }
194  if(bitmap.getWidth() == bitmap.getHeight())
195  {
196  float ratio = (float)bitmapW / (float)bitmapH;
197  while(true)
198  {
199  if(h < maxH && h > minH)
200  {
201  break;
202  }
203  else if (h > maxH)
204  {
205  h = h / 2;
206  }
207  else if (h < minH)
208  {
209  h = h * 2;
210  }
211  }
212  w = Math.round(ratio * h);
213  }
214  if(w % 4 != 0)
215  {
216  while(true)
217  {
218  w++;
219  if(w % 4 == 0)
220  {
221  break;
222  }
223  }
224  }
225 
226  if(h % 4 != 0)
227  {
228  while(true)
229  {
230  h++;
231  if(h % 4 == 0)
232  {
233  break;
234  }
235  }
236  }
237  return Bitmap.createScaledBitmap(bitmap, w, h, true);
238  }
239 
243  public static Bitmap LoadFromFile(Activity activity, Uri uri) throws IOException {
244  if (Build.VERSION.SDK_INT >= 28)
245  {
246  Bitmap tempBitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(activity.getContentResolver(), uri));
247  return tempBitmap.copy(Bitmap.Config.ARGB_8888,true);
248  }
249  else
250  {
251  BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
252  bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
253  InputStream input = activity.getContentResolver().openInputStream(uri);
254  return BitmapFactory.decodeStream(input, null, bitmapOptions);
255  }
256  }
257 
267  public byte[] ConvertToByte(Bitmap bitmap, boolean opacity)
268  {
269  int w = bitmap.getWidth();
270  int h = bitmap.getHeight();
271 
272  int[] rgbStream = new int[ w * h];
273  bitmap.getPixels(rgbStream, 0, w, 0, 0, w, h);
274 
275  byte[] buffer = new byte [w * h * 4];
276  int offset = 0;
277  for(int i = 0; i < w * h; i++)
278  {
279  int pixel = rgbStream[i];
280  int R = (pixel >> 16) & 0xFF;
281  int G = (pixel >> 8) & 0xFF;
282  int B = pixel & 0xFF;
283 
284  buffer[offset + 0] = (byte)R;
285  buffer[offset + 1] = (byte)G;
286  buffer[offset + 2] = (byte)B;
287 
288  if (opacity) {
289  int alpha = (pixel >> 24) & 0xFF;
290  buffer[offset + 3] = (byte) alpha;
291  offset = offset + 4;
292  } else {
293 
294  offset = offset + 3;
295  }
296 
297  }
298 
299  return buffer;
300  }
301 
305  public void CreateGallery() throws IOException {
306  File directory = new File(pathToGallery);
307  File[] files = directory.listFiles();
308  Log.d("Files", "Size: "+ files.length);
309  for (int i = 0; i < files.length; i++)
310  {
311  String galleryPath = pathToGallery + "/" + files[i].getName();
312  Bitmap bitmap = LoadFromFile((Activity) _context, Uri.fromFile(new File(galleryPath)));
313  bitmap = SetOptimalBitmapSize(bitmap);
314  AddToGallery(ConvertToByte(bitmap, false), files[i].getName(), bitmap.getWidth(), bitmap.getHeight());
315  }
316  }
317 
322  public void run()
323  {
324  SurfaceHolder surfaceHolder = getHolder();
325  Canvas c = null;
326  isRunning = true;
327  try
328  {
329  //int st = GetStatus();
330  //int count = GetNotifyCount();
331  c = surfaceHolder.lockCanvas(null);
332  synchronized(this)
333  {
334 
335  _tview.requestRender();
336  }
337  }
338  finally
339  {
340  if(c != null)
341  {
342  surfaceHolder.unlockCanvasAndPost(c);
343  }
344  }
345  return;
346  }
347 
354  public static native void DetectFeatures(byte[] frame, int width, int height);
355  public static native void recognizeFace(byte[] frame, int width, int height);
356  public static native void WriteLogoImage(byte[] logo, int width, int height);
357  public static native void WriteFontImage(byte[] font, int width, int height);
358  public static native void AddToGallery(byte[] frame, String filename, int width, int height);
359 }
void CreateGallery()
Method used to create face recognition gallery from set of images.
Base class used for defining surface for displaying tracking results.
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 Bitmap LoadFromFile(Activity activity, Uri uri)
Utility method called for loading the image from file into a Bitmap object.
Bitmap SetOptimalBitmapSize(Bitmap bitmap)
Utility method used to resize bitmap of image used for tracking.
static native void DetectFeatures(byte[] frame, int width, int height)
Interface to native method used for passing raw pixel data to detector.
void run()
Implementation of run method of Runnable interface.
int getBitmapWidth()
Called to obtain width of bitmap used for tracking.
int getBitmapHeight()
Called to obtain height of bitmap used for tracking.
void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
Implementation of surfaceChanged method provided by SurfaceHolder.Callback class. ...
void surfaceCreated(SurfaceHolder holder)
Implementation of surfaceCreated method provided by SurfaceHolder.Callback class. ...
void surfaceDestroyed(SurfaceHolder holder)
Implementation of surfaceDestroyed method provided by SurfaceHolder.Callback class.