Added FPS measurement to some Android samples
authorAndrey Kamaev <no@email>
Tue, 19 Jul 2011 11:06:27 +0000 (11:06 +0000)
committerAndrey Kamaev <no@email>
Tue, 19 Jul 2011 11:06:27 +0000 (11:06 +0000)
samples/android/face-detection/src/org/opencv/samples/fd/FpsMeter.java [new file with mode: 0644]
samples/android/face-detection/src/org/opencv/samples/fd/SampleCvViewBase.java
samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/FpsMeter.java [new file with mode: 0644]
samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/SampleCvViewBase.java

diff --git a/samples/android/face-detection/src/org/opencv/samples/fd/FpsMeter.java b/samples/android/face-detection/src/org/opencv/samples/fd/FpsMeter.java
new file mode 100644 (file)
index 0000000..df05744
--- /dev/null
@@ -0,0 +1,50 @@
+package org.opencv.samples.fd;
+
+import java.text.DecimalFormat;
+
+import org.opencv.core;
+
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.util.Log;
+
+public class FpsMeter {
+    private static final String TAG       = "Sample::FpsMeter";
+    int                         step;
+    int                         framesCouner;
+    double                      freq;
+    long                        prevFrameTime;
+    String                      strfps;
+    DecimalFormat               twoPlaces = new DecimalFormat("0.00");
+    Paint                       paint;
+
+    public void init() {
+        step = 20;
+        framesCouner = 0;
+        freq = core.getTickFrequency();
+        prevFrameTime = core.getTickCount();
+        strfps = "";
+
+        paint = new Paint();
+        paint.setColor(Color.BLUE);
+        paint.setTextSize(50);
+    }
+
+    public void measure() {
+        framesCouner++;
+        if (framesCouner % step == 0) {
+            long time = core.getTickCount();
+            double fps = step * freq / (time - prevFrameTime);
+            prevFrameTime = time;
+            DecimalFormat twoPlaces = new DecimalFormat("0.00");
+            strfps = twoPlaces.format(fps) + " FPS";
+            Log.i(TAG, strfps);
+        }
+    }
+
+    public void draw(Canvas canvas, float offsetx, float offsety) {
+        canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint);
+    }
+
+}
index 12b88be..338bba5 100644 (file)
@@ -16,11 +16,13 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
 
     private SurfaceHolder       mHolder;
     private VideoCapture        mCamera;
+    private FpsMeter            mFps;
 
     public SampleCvViewBase(Context context) {
         super(context);
         mHolder = getHolder();
         mHolder.addCallback(this);
+        mFps = new FpsMeter();
         Log.i(TAG, "Instantiated new " + this.getClass());
     }
 
@@ -78,6 +80,8 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
 
     public void run() {
         Log.i(TAG, "Starting processing thread");
+        mFps.init();
+
         while (true) {
             Bitmap bmp = null;
 
@@ -91,12 +95,15 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
                 }
 
                 bmp = processFrame(mCamera);
+
+                mFps.measure();
             }
 
             if (bmp != null) {
                 Canvas canvas = mHolder.lockCanvas();
                 if (canvas != null) {
                     canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
+                    mFps.draw(canvas, (canvas.getWidth() - bmp.getWidth()) / 2, 0);
                     mHolder.unlockCanvasAndPost(canvas);
                 }
                 bmp.recycle();
diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/FpsMeter.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/FpsMeter.java
new file mode 100644 (file)
index 0000000..30dc060
--- /dev/null
@@ -0,0 +1,50 @@
+package org.opencv.samples.imagemanipulations;
+
+import java.text.DecimalFormat;
+
+import org.opencv.core;
+
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.util.Log;
+
+public class FpsMeter {
+    private static final String TAG       = "Sample::FpsMeter";
+    int                         step;
+    int                         framesCouner;
+    double                      freq;
+    long                        prevFrameTime;
+    String                      strfps;
+    DecimalFormat               twoPlaces = new DecimalFormat("0.00");
+    Paint                       paint;
+
+    public void init() {
+        step = 20;
+        framesCouner = 0;
+        freq = core.getTickFrequency();
+        prevFrameTime = core.getTickCount();
+        strfps = "";
+
+        paint = new Paint();
+        paint.setColor(Color.BLUE);
+        paint.setTextSize(50);
+    }
+
+    public void measure() {
+        framesCouner++;
+        if (framesCouner % step == 0) {
+            long time = core.getTickCount();
+            double fps = step * freq / (time - prevFrameTime);
+            prevFrameTime = time;
+            DecimalFormat twoPlaces = new DecimalFormat("0.00");
+            strfps = twoPlaces.format(fps) + " FPS";
+            Log.i(TAG, strfps);
+        }
+    }
+
+    public void draw(Canvas canvas, float offsetx, float offsety) {
+        canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint);
+    }
+
+}
index 76f8f73..56899b2 100644 (file)
@@ -16,11 +16,13 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
 
     private SurfaceHolder       mHolder;
     private VideoCapture        mCamera;
+    private FpsMeter            mFps;
 
     public SampleCvViewBase(Context context) {
         super(context);
         mHolder = getHolder();
         mHolder.addCallback(this);
+        mFps = new FpsMeter();
         Log.i(TAG, "Instantiated new " + this.getClass());
     }
 
@@ -78,6 +80,8 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
 
     public void run() {
         Log.i(TAG, "Starting processing thread");
+        mFps.init();
+
         while (true) {
             Bitmap bmp = null;
 
@@ -91,12 +95,15 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
                 }
 
                 bmp = processFrame(mCamera);
+
+                mFps.measure();
             }
 
             if (bmp != null) {
                 Canvas canvas = mHolder.lockCanvas();
                 if (canvas != null) {
                     canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
+                    mFps.draw(canvas, (canvas.getWidth() - bmp.getWidth()) / 2, 0);
                     mHolder.unlockCanvasAndPost(canvas);
                 }
                 bmp.recycle();