From 8266eab8b43f4965829c8c8b4567f4bfc8d188b1 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Thu, 22 Nov 2012 15:56:55 +0400 Subject: [PATCH 1/4] OpenCV app framework improved FPS meter added; Camera switching posibility added; Attributes loading for layout filed implemented. --- modules/java/android_lib/res/values/attrs.xml | 5 ++ .../java/android+CameraBridgeViewBase.java | 55 +++++++++++++----- .../generator/src/java/android+FpsMeter.java | 57 +++++++++++++++++++ .../src/java/android+JavaCameraView.java | 50 +++++++++------- .../src/java/android+NativeCameraView.java | 12 +++- 5 files changed, 144 insertions(+), 35 deletions(-) create mode 100644 modules/java/android_lib/res/values/attrs.xml create mode 100644 modules/java/generator/src/java/android+FpsMeter.java diff --git a/modules/java/android_lib/res/values/attrs.xml b/modules/java/android_lib/res/values/attrs.xml new file mode 100644 index 000000000..0294ad676 --- /dev/null +++ b/modules/java/android_lib/res/values/attrs.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/modules/java/generator/src/java/android+CameraBridgeViewBase.java b/modules/java/generator/src/java/android+CameraBridgeViewBase.java index cbcd78b35..c267df928 100644 --- a/modules/java/generator/src/java/android+CameraBridgeViewBase.java +++ b/modules/java/generator/src/java/android+CameraBridgeViewBase.java @@ -30,6 +30,11 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac private static final int MAX_UNSPECIFIED = -1; + private static final int STOPPED = 0; + private static final int STARTED = 1; + + private static final String TAG = "CameraBridge"; + protected int mFrameWidth; protected int mFrameHeight; @@ -37,11 +42,26 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac protected int mMaxWidth; protected int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA; + protected int mCameraIndex = -1; + private boolean mEnabled; private Bitmap mCacheBitmap; + protected FpsMeter mFpsMeter = null; + + private CvCameraViewListener mListener; + private int mState = STOPPED; + + private boolean mSurfaceExist; + + private Object mSyncObject = new Object(); public CameraBridgeViewBase(Context context, AttributeSet attrs) { super(context, attrs); + if (attrs.getAttributeBooleanValue(null, "show_fps", false)) + enableFpsMeter(); + + mCameraIndex = attrs.getAttributeIntValue(null,"camera_index", -1); + getHolder().addCallback(this); mMaxWidth = MAX_UNSPECIFIED; mMaxHeight = MAX_UNSPECIFIED; @@ -71,19 +91,6 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac } - private static final int STOPPED = 0; - private static final int STARTED = 1; - - private static final String TAG = "CameraBridge"; - - private CvCameraViewListener mListener; - private int mState = STOPPED; - - private boolean mEnabled; - private boolean mSurfaceExist; - - private Object mSyncObject = new Object(); - public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { Log.d(TAG, "call surfaceChanged event"); synchronized(mSyncObject) { @@ -135,6 +142,24 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac } } + /** + * This method enables label with fps value on the screen + */ + public void enableFpsMeter() { + if (mFpsMeter == null) { + mFpsMeter = new FpsMeter(); + } + } + + public void disableFpsMeter() { + mFpsMeter = null; + } + + /** + * + * @param listener + */ + public void setCvCameraViewListener(CvCameraViewListener listener) { mListener = listener; } @@ -272,6 +297,10 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac if (canvas != null) { canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR); canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, null); + if (mFpsMeter != null) { + mFpsMeter.measure(); + mFpsMeter.draw(canvas, 0, 0); + } getHolder().unlockCanvasAndPost(canvas); } } diff --git a/modules/java/generator/src/java/android+FpsMeter.java b/modules/java/generator/src/java/android+FpsMeter.java new file mode 100644 index 000000000..858d95a4a --- /dev/null +++ b/modules/java/generator/src/java/android+FpsMeter.java @@ -0,0 +1,57 @@ +package org.opencv.android; + +import java.text.DecimalFormat; + +import org.opencv.core.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 = "OCVSample::FpsMeter"; + int step; + int framesCouner; + double freq; + long prevFrameTime; + String strfps; + DecimalFormat twoPlaces = new DecimalFormat("0.00"); + Paint paint; + boolean isInitialized = false; + + 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() { + if (!isInitialized) { + init(); + isInitialized = true; + } else { + 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) { + Log.d(TAG, strfps); + canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint); + } + +} diff --git a/modules/java/generator/src/java/android+JavaCameraView.java b/modules/java/generator/src/java/android+JavaCameraView.java index f8b378781..f41cc8ea5 100644 --- a/modules/java/generator/src/java/android+JavaCameraView.java +++ b/modules/java/generator/src/java/android+JavaCameraView.java @@ -40,6 +40,8 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb private Thread mThread; private boolean mStopThread; + protected Camera mCamera; + private SurfaceTexture mSurfaceTexture; public static class JavaCameraSizeAccessor implements ListItemAccessor { @@ -55,8 +57,6 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb } } - private Camera mCamera; - public JavaCameraView(Context context, AttributeSet attrs) { super(context, attrs); Log.d(TAG, "Java camera view ctor"); @@ -69,25 +69,36 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb synchronized (this) { mCamera = null; - Log.d(TAG, "Trying to open camera with old open()"); - try { - mCamera = Camera.open(); - } - catch (Exception e){ - Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage()); - } + if (mCameraIndex == -1) { + Log.d(TAG, "Trying to open camera with old open()"); + try { + mCamera = Camera.open(); + } + catch (Exception e){ + Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage()); + } - if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { - boolean connected = false; - for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) { - Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")"); - try { - mCamera = Camera.open(camIdx); - connected = true; - } catch (RuntimeException e) { - Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage()); + if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + boolean connected = false; + for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) { + Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")"); + try { + mCamera = Camera.open(camIdx); + connected = true; + } catch (RuntimeException e) { + Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage()); + } + if (connected) break; + } + } + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(mCameraIndex) + ")"); + try { + mCamera = Camera.open(mCameraIndex); + } catch (RuntimeException e) { + Log.e(TAG, "Camera #" + mCameraIndex + "failed to open: " + e.getLocalizedMessage()); } - if (connected) break; } } @@ -217,6 +228,7 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb releaseCamera(); } + @TargetApi(Build.VERSION_CODES.FROYO) public void onPreviewFrame(byte[] frame, Camera arg1) { Log.i(TAG, "Preview Frame received. Need to create MAT and deliver it to clients"); Log.i(TAG, "Frame size is " + frame.length); diff --git a/modules/java/generator/src/java/android+NativeCameraView.java b/modules/java/generator/src/java/android+NativeCameraView.java index a62d6bb5c..1a92d00a3 100644 --- a/modules/java/generator/src/java/android+NativeCameraView.java +++ b/modules/java/generator/src/java/android+NativeCameraView.java @@ -19,7 +19,8 @@ public class NativeCameraView extends CameraBridgeViewBase { public static final String TAG = "NativeCameraView"; private boolean mStopThread; private Thread mThread; - private VideoCapture mCamera; + + protected VideoCapture mCamera; public NativeCameraView(Context context, AttributeSet attrs) { super(context, attrs); @@ -77,12 +78,17 @@ public class NativeCameraView extends CameraBridgeViewBase { private boolean initializeCamera(int width, int height) { synchronized (this) { - mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID); + + if (mCameraIndex == -1) + mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID); + else + mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID + mCameraIndex); if (mCamera == null) return false; - //TODO: improve error handling + if (mCamera.isOpened() == false) + return false; java.util.List sizes = mCamera.getSupportedPreviewSizes(); From 0efc32fc21bb53eb787f1e9f3123376361a3ac0b Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Tue, 27 Nov 2012 10:31:19 +0400 Subject: [PATCH 2/4] Attribure loading from layout improved. OpenCV namespace added; Default values for camera_id added; Aditional constructor with cameraId added. Resolution added to FPS message. --- modules/java/android_lib/res/values/attrs.xml | 12 +++++++++--- .../src/java/android+CameraBridgeViewBase.java | 17 +++++++++++++++-- .../generator/src/java/android+FpsMeter.java | 12 +++++++++++- .../src/java/android+JavaCameraView.java | 8 ++++++++ .../src/java/android+NativeCameraView.java | 8 ++++++++ 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/modules/java/android_lib/res/values/attrs.xml b/modules/java/android_lib/res/values/attrs.xml index 0294ad676..0cdf1097a 100644 --- a/modules/java/android_lib/res/values/attrs.xml +++ b/modules/java/android_lib/res/values/attrs.xml @@ -1,5 +1,11 @@ - - - \ No newline at end of file + + + + + + + + + diff --git a/modules/java/generator/src/java/android+CameraBridgeViewBase.java b/modules/java/generator/src/java/android+CameraBridgeViewBase.java index c267df928..c98574b06 100644 --- a/modules/java/generator/src/java/android+CameraBridgeViewBase.java +++ b/modules/java/generator/src/java/android+CameraBridgeViewBase.java @@ -2,6 +2,7 @@ package org.opencv.android; import java.util.List; +import org.opencv.R; import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.core.Size; @@ -11,6 +12,7 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; +import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.util.AttributeSet; @@ -55,12 +57,22 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac private Object mSyncObject = new Object(); + public CameraBridgeViewBase(Context context, int cameraId) { + super(context); + mCameraIndex = cameraId; + } + public CameraBridgeViewBase(Context context, AttributeSet attrs) { super(context, attrs); - if (attrs.getAttributeBooleanValue(null, "show_fps", false)) + + int count = attrs.getAttributeCount(); + Log.d(TAG, "Attr count: " + Integer.valueOf(count)); + + TypedArray tmp = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase); + if (tmp.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false)) enableFpsMeter(); - mCameraIndex = attrs.getAttributeIntValue(null,"camera_index", -1); + mCameraIndex = tmp.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1); getHolder().addCallback(this); mMaxWidth = MAX_UNSPECIFIED; @@ -148,6 +160,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac public void enableFpsMeter() { if (mFpsMeter == null) { mFpsMeter = new FpsMeter(); + mFpsMeter.setResolution(mFrameWidth, mFrameHeight); } } diff --git a/modules/java/generator/src/java/android+FpsMeter.java b/modules/java/generator/src/java/android+FpsMeter.java index 858d95a4a..d15b87a42 100644 --- a/modules/java/generator/src/java/android+FpsMeter.java +++ b/modules/java/generator/src/java/android+FpsMeter.java @@ -19,6 +19,8 @@ public class FpsMeter { DecimalFormat twoPlaces = new DecimalFormat("0.00"); Paint paint; boolean isInitialized = false; + int mWidth = 0; + int mHeight = 0; public void init() { step = 20; @@ -43,12 +45,20 @@ public class FpsMeter { double fps = step * freq / (time - prevFrameTime); prevFrameTime = time; DecimalFormat twoPlaces = new DecimalFormat("0.00"); - strfps = twoPlaces.format(fps) + " FPS"; + if (mWidth != 0 && mHeight != 0) + strfps = twoPlaces.format(fps) + " FPS@" + Integer.valueOf(mWidth) + "x" + Integer.valueOf(mHeight); + else + strfps = twoPlaces.format(fps) + " FPS"; Log.i(TAG, strfps); } } } + public void setResolution(int width, int height) { + mWidth = width; + mHeight = height; + } + public void draw(Canvas canvas, float offsetx, float offsety) { Log.d(TAG, strfps); canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint); diff --git a/modules/java/generator/src/java/android+JavaCameraView.java b/modules/java/generator/src/java/android+JavaCameraView.java index f41cc8ea5..ce50d66a9 100644 --- a/modules/java/generator/src/java/android+JavaCameraView.java +++ b/modules/java/generator/src/java/android+JavaCameraView.java @@ -57,6 +57,10 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb } } + public JavaCameraView(Context context, int cameraId) { + super(context, cameraId); + } + public JavaCameraView(Context context, AttributeSet attrs) { super(context, attrs); Log.d(TAG, "Java camera view ctor"); @@ -131,6 +135,10 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb mFrameWidth = params.getPreviewSize().width; mFrameHeight = params.getPreviewSize().height; + if (mFpsMeter != null) { + mFpsMeter.setResolution(mFrameWidth, mFrameHeight); + } + int size = mFrameWidth * mFrameHeight; size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8; mBuffer = new byte[size]; diff --git a/modules/java/generator/src/java/android+NativeCameraView.java b/modules/java/generator/src/java/android+NativeCameraView.java index 1a92d00a3..dad3cb95f 100644 --- a/modules/java/generator/src/java/android+NativeCameraView.java +++ b/modules/java/generator/src/java/android+NativeCameraView.java @@ -22,6 +22,10 @@ public class NativeCameraView extends CameraBridgeViewBase { protected VideoCapture mCamera; + public NativeCameraView(Context context, int cameraId) { + super(context, cameraId); + } + public NativeCameraView(Context context, AttributeSet attrs) { super(context, attrs); } @@ -98,6 +102,10 @@ public class NativeCameraView extends CameraBridgeViewBase { mFrameWidth = (int)frameSize.width; mFrameHeight = (int)frameSize.height; + if (mFpsMeter != null) { + mFpsMeter.setResolution(mFrameWidth, mFrameHeight); + } + AllocateCache(); mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, frameSize.width); From e95fc27490b9c51d87e208035154668ce7e76787 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Tue, 27 Nov 2012 11:57:01 +0400 Subject: [PATCH 3/4] Samples updated In tutorial-1 fps meter enabled via layout.xml. Camera id is set to "any" via lauout.xml; In tutorial-2 message moved ion the bottom of the screen. --- .../res/layout/tutorial1_surface_view.xml | 9 +++++++-- .../opencv/samples/tutorial2/Sample2NativeCamera.java | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/samples/android/tutorial-1-addopencv/res/layout/tutorial1_surface_view.xml b/samples/android/tutorial-1-addopencv/res/layout/tutorial1_surface_view.xml index 844fcab00..52b9dfaf6 100644 --- a/samples/android/tutorial-1-addopencv/res/layout/tutorial1_surface_view.xml +++ b/samples/android/tutorial-1-addopencv/res/layout/tutorial1_surface_view.xml @@ -1,5 +1,6 @@ @@ -7,12 +8,16 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" - android:id="@+id/tutorial1_activity_java_surface_view" /> + android:id="@+id/tutorial1_activity_java_surface_view" + opencv:show_fps="true" + opencv:camera_id="any" /> + android:id="@+id/tutorial1_activity_native_surface_view" + opencv:show_fps="true" + opencv:camera_id="any" /> diff --git a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java index 009d00a8e..901e8dd1c 100644 --- a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java +++ b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java @@ -110,7 +110,7 @@ public class Sample2NativeCamera extends Activity implements CvCameraViewListene case Sample2NativeCamera.VIEW_MODE_RGBA: { inputFrame.copyTo(mRgba); - Core.putText(mRgba, "OpenCV+Android", new Point(10, 50), 3, 1, new Scalar(255, 0, 0, 255), 2); + Core.putText(mRgba, "OpenCV+Android", new Point(10, inputFrame.rows() - 10), 3, 1, new Scalar(255, 0, 0, 255), 2); } break; case Sample2NativeCamera.VIEW_MODE_CANNY: { From 2e5a7284d24fcc2eaf992f5d2410de32c8d6e3ce Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Tue, 27 Nov 2012 14:55:49 +0400 Subject: [PATCH 4/4] Code review comments applied. --- .../java/android+CameraBridgeViewBase.java | 30 ++++------ .../generator/src/java/android+FpsMeter.java | 57 +++++++++---------- 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/modules/java/generator/src/java/android+CameraBridgeViewBase.java b/modules/java/generator/src/java/android+CameraBridgeViewBase.java index c98574b06..cd13d1f1a 100644 --- a/modules/java/generator/src/java/android+CameraBridgeViewBase.java +++ b/modules/java/generator/src/java/android+CameraBridgeViewBase.java @@ -28,35 +28,27 @@ import android.view.SurfaceView; * The clients shall implement CvCameraViewListener. */ public abstract class CameraBridgeViewBase extends SurfaceView implements SurfaceHolder.Callback { -//TODO: add method to control the format in which the frames will be delivered to CvCameraViewListener + private static final String TAG = "CameraBridge"; private static final int MAX_UNSPECIFIED = -1; - private static final int STOPPED = 0; private static final int STARTED = 1; - private static final String TAG = "CameraBridge"; + private int mState = STOPPED; + private Bitmap mCacheBitmap; + private CvCameraViewListener mListener; + private boolean mSurfaceExist; + private Object mSyncObject = new Object(); protected int mFrameWidth; protected int mFrameHeight; - protected int mMaxHeight; protected int mMaxWidth; - protected int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA; protected int mCameraIndex = -1; - private boolean mEnabled; - - private Bitmap mCacheBitmap; + protected boolean mEnabled; protected FpsMeter mFpsMeter = null; - private CvCameraViewListener mListener; - private int mState = STOPPED; - - private boolean mSurfaceExist; - - private Object mSyncObject = new Object(); - public CameraBridgeViewBase(Context context, int cameraId) { super(context); mCameraIndex = cameraId; @@ -68,11 +60,11 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac int count = attrs.getAttributeCount(); Log.d(TAG, "Attr count: " + Integer.valueOf(count)); - TypedArray tmp = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase); - if (tmp.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false)) + TypedArray styledAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase); + if (styledAttrs.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false)) enableFpsMeter(); - mCameraIndex = tmp.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1); + mCameraIndex = styledAttrs.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1); getHolder().addCallback(this); mMaxWidth = MAX_UNSPECIFIED; @@ -312,7 +304,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, null); if (mFpsMeter != null) { mFpsMeter.measure(); - mFpsMeter.draw(canvas, 0, 0); + mFpsMeter.draw(canvas, 20, 30); } getHolder().unlockCanvasAndPost(canvas); } diff --git a/modules/java/generator/src/java/android+FpsMeter.java b/modules/java/generator/src/java/android+FpsMeter.java index d15b87a42..88e826cf9 100644 --- a/modules/java/generator/src/java/android+FpsMeter.java +++ b/modules/java/generator/src/java/android+FpsMeter.java @@ -10,46 +10,45 @@ import android.graphics.Paint; import android.util.Log; public class FpsMeter { - private static final String TAG = "OCVSample::FpsMeter"; - int step; - int framesCouner; - double freq; - long prevFrameTime; - String strfps; - DecimalFormat twoPlaces = new DecimalFormat("0.00"); - Paint paint; - boolean isInitialized = false; + private static final String TAG = "FpsMeter"; + private static final int STEP = 20; + private static final DecimalFormat FPS_FORMAT = new DecimalFormat("0.00"); + + private int mFramesCouner; + private double mFrequency; + private long mprevFrameTime; + private String mStrfps; + Paint mPaint; + boolean mIsInitialized = false; int mWidth = 0; int mHeight = 0; public void init() { - step = 20; - framesCouner = 0; - freq = Core.getTickFrequency(); - prevFrameTime = Core.getTickCount(); - strfps = ""; + mFramesCouner = 0; + mFrequency = Core.getTickFrequency(); + mprevFrameTime = Core.getTickCount(); + mStrfps = ""; - paint = new Paint(); - paint.setColor(Color.BLUE); - paint.setTextSize(50); + mPaint = new Paint(); + mPaint.setColor(Color.BLUE); + mPaint.setTextSize(20); } public void measure() { - if (!isInitialized) { + if (!mIsInitialized) { init(); - isInitialized = true; + mIsInitialized = true; } else { - framesCouner++; - if (framesCouner % step == 0) { + mFramesCouner++; + if (mFramesCouner % STEP == 0) { long time = Core.getTickCount(); - double fps = step * freq / (time - prevFrameTime); - prevFrameTime = time; - DecimalFormat twoPlaces = new DecimalFormat("0.00"); + double fps = STEP * mFrequency / (time - mprevFrameTime); + mprevFrameTime = time; if (mWidth != 0 && mHeight != 0) - strfps = twoPlaces.format(fps) + " FPS@" + Integer.valueOf(mWidth) + "x" + Integer.valueOf(mHeight); + mStrfps = FPS_FORMAT.format(fps) + " FPS@" + Integer.valueOf(mWidth) + "x" + Integer.valueOf(mHeight); else - strfps = twoPlaces.format(fps) + " FPS"; - Log.i(TAG, strfps); + mStrfps = FPS_FORMAT.format(fps) + " FPS"; + Log.i(TAG, mStrfps); } } } @@ -60,8 +59,8 @@ public class FpsMeter { } public void draw(Canvas canvas, float offsetx, float offsety) { - Log.d(TAG, strfps); - canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint); + Log.d(TAG, mStrfps); + canvas.drawText(mStrfps, offsetx, offsety, mPaint); } }