Code review comments applied.
authorAlexander Smorkalov <alexander.smorkalov@itseez.com>
Tue, 27 Nov 2012 10:55:49 +0000 (14:55 +0400)
committerAlexander Smorkalov <alexander.smorkalov@itseez.com>
Tue, 27 Nov 2012 10:55:49 +0000 (14:55 +0400)
modules/java/generator/src/java/android+CameraBridgeViewBase.java
modules/java/generator/src/java/android+FpsMeter.java

index c98574b..cd13d1f 100644 (file)
@@ -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);
             }
index d15b87a..88e826c 100644 (file)
@@ -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);
     }
 
 }