visageSDK
ImageTrackerView.java
1 package com.visagetechnologies.visagetrackerdemo;
2 
3 
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.lang.ref.WeakReference;
10 
11 import android.app.Activity;
12 import android.content.ContentResolver;
13 import android.content.Context;
14 import android.graphics.Bitmap;
15 import android.graphics.BitmapFactory;
16 import android.graphics.Canvas;
17 import android.graphics.ImageDecoder;
18 import android.net.Uri;
19 import android.os.Build;
20 import android.view.SurfaceHolder;
21 
26 public class ImageTrackerView extends TrackerView implements Runnable
27 {
31  Bitmap _bitmap;
32 
36  String _path;
37  Context _context;
38 
44  ImageTrackerView(Context context, Uri imageUri, TrackerActivity activity) throws IOException {
45  super(context);
46  _context = context;
47  _activity = activity;
48  _bitmap = LoadFromFile(_activity, imageUri);
49  getHolder().addCallback(this);
50  }
51 
56  public void surfaceCreated(SurfaceHolder holder)
57  {
58  isRunning = true;
59  setKeepScreenOn(true);
60  WriteFrameImage(ConvertToByte(_bitmap, false), _bitmap.getWidth(), _bitmap.getHeight());
61  }
62 
66  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
67  {
68  _width = width;
69  _height = height;
70  }
71 
76  public void surfaceDestroyed(SurfaceHolder holder)
77  {
78  isRunning = false;
79  _bitmap.recycle();
80  }
81 
86  public int getBitmapWidth()
87  {
88  return _bitmap.getWidth();
89  }
90 
95  public int getBitmapHeight()
96  {
97  return _bitmap.getHeight();
98  }
99 
105  public void SetOptimalBitmapSize()
106  {
107  int maxW = _context.getResources().getDisplayMetrics().widthPixels;
108  int maxH = _context.getResources().getDisplayMetrics().heightPixels;
109 
110  int w = maxW;
111  int h = maxH;
112 
113  if(_bitmap.getWidth() < maxW && _bitmap.getHeight() < maxH && _bitmap.getWidth() % 4 == 0 && _bitmap.getHeight() % 4 == 0)
114  {
115  return;
116  }
117 
118  int bitmapW = _bitmap.getWidth();
119  int bitmapH = _bitmap.getHeight();
120 
121  w = (bitmapW / 4) * 4;
122  h = (bitmapH / 4) * 4;
123 
124  Bitmap mBitmap = Bitmap.createScaledBitmap(_bitmap, w, h, true);
125  WeakReference<Bitmap> mBitmapReference = new WeakReference<Bitmap>(mBitmap);
126  _bitmap = mBitmapReference.get();
127  }
128 
133  public static Bitmap LoadFromFile(Activity activity, Uri uri) throws IOException {
134  if (Build.VERSION.SDK_INT >= 28)
135  {
136  Bitmap tempBitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(activity.getContentResolver(), uri));
137  return tempBitmap.copy(Bitmap.Config.ARGB_8888,true);
138  }
139  else
140  {
141  BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
142  bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
143  InputStream input = activity.getContentResolver().openInputStream(uri);
144  return BitmapFactory.decodeStream(input, null, bitmapOptions);
145  }
146  }
147 
157  public static byte[] ConvertToByte(Bitmap bitmap_, boolean opacity)
158  {
159  int w = bitmap_.getWidth();
160  int h = bitmap_.getHeight();
161 
162  int[] rgbStream = new int[ w * h];
163  bitmap_.getPixels(rgbStream, 0, w, 0, 0, w, h);
164 
165  byte[] buffer = new byte [w * h * 4];
166  int offset = 0;
167  for(int i = 0; i < w * h; i++)
168  {
169  int pixel = rgbStream[i];
170  //int A = (pixel >> 24) & 0xFF;
171  int R = (pixel >> 16) & 0xFF;
172  int G = (pixel >> 8) & 0xFF;
173  int B = pixel & 0xFF;
174 
175  buffer[offset + 0] = (byte)R;
176  buffer[offset + 1] = (byte)G;
177  buffer[offset + 2] = (byte)B;
178 
179  if (opacity) {
180  int A = (pixel >> 24) & 0xFF;
181  buffer[offset + 3] = (byte) A;
182  offset = offset + 4;
183  } else {
184 
185  offset = offset + 3;
186  }
187  }
188 
189  return buffer;
190  }
191 
196  public void run()
197  {
198  SurfaceHolder surfaceHolder = getHolder();
199  Canvas c = null;
200  boolean isAutoStopped = false;
201  while(isRunning)
202  {
203  try
204  {
205  //int st = GetStatus();
206  //int count = GetNotifyCount();
207  c = surfaceHolder.lockCanvas(null);
208  synchronized(this)
209  {
210  _tview.requestRender();
211  }
212  }
213  finally
214  {
215  if(c != null)
216  {
217  surfaceHolder.unlockCanvasAndPost(c);
218  }
219  }
220 
221  isAutoStopped = IsAutoStopped();
222 
223  if(isAutoStopped)
224  {
225  isRunning = false;
226  }
227  }
228 
229  if(isAutoStopped)
230  {
231  _activity.ShowDialog();
232  }
233  }
234 
241  public static native void WriteFrameImage(byte[] frame, int width, int height);
242 }
void surfaceDestroyed(SurfaceHolder holder)
Implementation of surfaceDestroyed method provided by SurfaceHolder.Callback class.
void surfaceCreated(SurfaceHolder holder)
Implementation of surfaceCreated method provided by SurfaceHolder.Callback class. ...
static native void WriteFrameImage(byte[] frame, int width, int height)
Interface to native method used for passing raw pixel data to tracker.
Activity called to initiate and control tracking for either tracking from camera or image...
void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
Implementation of surfaceChanged method provided by SurfaceHolder.Callback class. ...
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.
void run()
Implementation of run method of Runnable interface.
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...
int getBitmapWidth()
Called to obtain width of bitmap used for tracking.
int getBitmapHeight()
Called to obtain height of bitmap used for tracking.