1 package com.visagetechnologies.facedetectdemo;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
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;
22 import androidx.core.content.res.ResourcesCompat;
49 ImageDetectorView(Context context, Uri imageUri,
boolean detect)
throws IOException {
56 getHolder().addCallback(
this);
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());
62 pathToGallery = context.getFilesDir().getAbsolutePath() +
"/Famous Actors/gallery";
63 String pathToFont = context.getFilesDir().getAbsolutePath() +
"/font.png";
66 Bitmap font =
LoadFromFile((Activity) context, Uri.fromFile(
new File(pathToFont)));
67 WriteFontImage(
ConvertToByte(font,
true), font.getWidth(), font.getHeight());
76 setKeepScreenOn(
true);
85 }
catch (IOException e) {
88 recognizeFace(
ConvertToByte(_bitmap,
false), _bitmap.getWidth(), _bitmap.getHeight());
95 public void surfaceChanged(SurfaceHolder holder,
int format,
int width,
int height)
116 return _bitmap.getWidth();
125 return _bitmap.getHeight();
140 int w = bitmap.getWidth();
141 int h = bitmap.getHeight();
144 if(bitmap.getWidth() < maxW && bitmap.getHeight() < maxH && bitmap.getWidth() > minW && bitmap.getHeight() > minH && bitmap.getWidth() % 4 == 0 && bitmap.getHeight() % 4 == 0)
148 int bitmapW = bitmap.getWidth();
149 int bitmapH = bitmap.getHeight();
151 if(bitmap.getWidth() > bitmap.getHeight())
153 float ratio = (float)bitmapH / (
float)bitmapW;
156 if(w <= maxW && w >= minW)
170 h = Math.round(ratio * w);
173 if(bitmap.getWidth() < bitmap.getHeight())
175 float ratio = (float)bitmapW / (
float)bitmapH;
178 if(h <= maxH && h >= minH)
192 w = Math.round(ratio * h);
194 if(bitmap.getWidth() == bitmap.getHeight())
196 float ratio = (float)bitmapW / (
float)bitmapH;
199 if(h < maxH && h > minH)
212 w = Math.round(ratio * h);
237 return Bitmap.createScaledBitmap(bitmap, w, h,
true);
243 public static Bitmap
LoadFromFile(Activity activity, Uri uri)
throws IOException {
244 if (Build.VERSION.SDK_INT >= 28)
246 Bitmap tempBitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(activity.getContentResolver(), uri));
247 return tempBitmap.copy(Bitmap.Config.ARGB_8888,
true);
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);
269 int w = bitmap.getWidth();
270 int h = bitmap.getHeight();
272 int[] rgbStream =
new int[ w * h];
273 bitmap.getPixels(rgbStream, 0, w, 0, 0, w, h);
275 byte[] buffer =
new byte [w * h * 4];
277 for(
int i = 0; i < w * h; i++)
279 int pixel = rgbStream[i];
280 int R = (pixel >> 16) & 0xFF;
281 int G = (pixel >> 8) & 0xFF;
282 int B = pixel & 0xFF;
284 buffer[offset + 0] = (byte)R;
285 buffer[offset + 1] = (byte)G;
286 buffer[offset + 2] = (byte)B;
289 int alpha = (pixel >> 24) & 0xFF;
290 buffer[offset + 3] = (byte) alpha;
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++)
311 String galleryPath = pathToGallery +
"/" + files[i].getName();
312 Bitmap bitmap =
LoadFromFile((Activity) _context, Uri.fromFile(
new File(galleryPath)));
314 AddToGallery(
ConvertToByte(bitmap,
false), files[i].getName(), bitmap.getWidth(), bitmap.getHeight());
324 SurfaceHolder surfaceHolder = getHolder();
331 c = surfaceHolder.lockCanvas(null);
335 _tview.requestRender();
342 surfaceHolder.unlockCanvasAndPost(c);
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);
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. ...
Derives from TrackerView.
void surfaceDestroyed(SurfaceHolder holder)
Implementation of surfaceDestroyed method provided by SurfaceHolder.Callback class.