Code review comments applied.

This commit is contained in:
Alexander Smorkalov 2012-11-27 14:55:49 +04:00
parent e95fc27490
commit 2e5a7284d2
2 changed files with 39 additions and 48 deletions

View File

@ -28,35 +28,27 @@ import android.view.SurfaceView;
* The clients shall implement CvCameraViewListener. * The clients shall implement CvCameraViewListener.
*/ */
public abstract class CameraBridgeViewBase extends SurfaceView implements SurfaceHolder.Callback { 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 MAX_UNSPECIFIED = -1;
private static final int STOPPED = 0; private static final int STOPPED = 0;
private static final int STARTED = 1; 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 mFrameWidth;
protected int mFrameHeight; protected int mFrameHeight;
protected int mMaxHeight; protected int mMaxHeight;
protected int mMaxWidth; protected int mMaxWidth;
protected int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA; protected int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA;
protected int mCameraIndex = -1; protected int mCameraIndex = -1;
private boolean mEnabled; protected boolean mEnabled;
private Bitmap mCacheBitmap;
protected FpsMeter mFpsMeter = null; 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) { public CameraBridgeViewBase(Context context, int cameraId) {
super(context); super(context);
mCameraIndex = cameraId; mCameraIndex = cameraId;
@ -68,11 +60,11 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
int count = attrs.getAttributeCount(); int count = attrs.getAttributeCount();
Log.d(TAG, "Attr count: " + Integer.valueOf(count)); Log.d(TAG, "Attr count: " + Integer.valueOf(count));
TypedArray tmp = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase); TypedArray styledAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase);
if (tmp.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false)) if (styledAttrs.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false))
enableFpsMeter(); enableFpsMeter();
mCameraIndex = tmp.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1); mCameraIndex = styledAttrs.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1);
getHolder().addCallback(this); getHolder().addCallback(this);
mMaxWidth = MAX_UNSPECIFIED; 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); canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, null);
if (mFpsMeter != null) { if (mFpsMeter != null) {
mFpsMeter.measure(); mFpsMeter.measure();
mFpsMeter.draw(canvas, 0, 0); mFpsMeter.draw(canvas, 20, 30);
} }
getHolder().unlockCanvasAndPost(canvas); getHolder().unlockCanvasAndPost(canvas);
} }

View File

@ -10,46 +10,45 @@ import android.graphics.Paint;
import android.util.Log; import android.util.Log;
public class FpsMeter { public class FpsMeter {
private static final String TAG = "OCVSample::FpsMeter"; private static final String TAG = "FpsMeter";
int step; private static final int STEP = 20;
int framesCouner; private static final DecimalFormat FPS_FORMAT = new DecimalFormat("0.00");
double freq;
long prevFrameTime; private int mFramesCouner;
String strfps; private double mFrequency;
DecimalFormat twoPlaces = new DecimalFormat("0.00"); private long mprevFrameTime;
Paint paint; private String mStrfps;
boolean isInitialized = false; Paint mPaint;
boolean mIsInitialized = false;
int mWidth = 0; int mWidth = 0;
int mHeight = 0; int mHeight = 0;
public void init() { public void init() {
step = 20; mFramesCouner = 0;
framesCouner = 0; mFrequency = Core.getTickFrequency();
freq = Core.getTickFrequency(); mprevFrameTime = Core.getTickCount();
prevFrameTime = Core.getTickCount(); mStrfps = "";
strfps = "";
paint = new Paint(); mPaint = new Paint();
paint.setColor(Color.BLUE); mPaint.setColor(Color.BLUE);
paint.setTextSize(50); mPaint.setTextSize(20);
} }
public void measure() { public void measure() {
if (!isInitialized) { if (!mIsInitialized) {
init(); init();
isInitialized = true; mIsInitialized = true;
} else { } else {
framesCouner++; mFramesCouner++;
if (framesCouner % step == 0) { if (mFramesCouner % STEP == 0) {
long time = Core.getTickCount(); long time = Core.getTickCount();
double fps = step * freq / (time - prevFrameTime); double fps = STEP * mFrequency / (time - mprevFrameTime);
prevFrameTime = time; mprevFrameTime = time;
DecimalFormat twoPlaces = new DecimalFormat("0.00");
if (mWidth != 0 && mHeight != 0) 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 else
strfps = twoPlaces.format(fps) + " FPS"; mStrfps = FPS_FORMAT.format(fps) + " FPS";
Log.i(TAG, strfps); Log.i(TAG, mStrfps);
} }
} }
} }
@ -60,8 +59,8 @@ public class FpsMeter {
} }
public void draw(Canvas canvas, float offsetx, float offsety) { public void draw(Canvas canvas, float offsetx, float offsety) {
Log.d(TAG, strfps); Log.d(TAG, mStrfps);
canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint); canvas.drawText(mStrfps, offsetx, offsety, mPaint);
} }
} }