adding displaying FPS

This commit is contained in:
Andrey Pavlenko 2015-07-31 01:52:10 +03:00
parent 0185cb27eb
commit 9ab291ea1c
4 changed files with 45 additions and 12 deletions

View File

@ -1,12 +1,16 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent" >
tools:context="${relativePackage}.${activityClass}" >
<org.opencv.samples.tutorial4.MyGLSurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_gl_surface_view" />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/app_name" /> android:id="@+id/fps_text_view"
android:text="FPS:" />
</RelativeLayout> </FrameLayout>

View File

@ -6,7 +6,11 @@ import javax.microedition.khronos.opengles.GL10;
import android.graphics.SurfaceTexture; import android.graphics.SurfaceTexture;
import android.opengl.GLES20; import android.opengl.GLES20;
import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.widget.TextView;
public abstract class MyGLRendererBase implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener { public abstract class MyGLRendererBase implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener {
protected final String LOGTAG = "MyGLRendererBase"; protected final String LOGTAG = "MyGLRendererBase";
@ -15,6 +19,7 @@ public abstract class MyGLRendererBase implements GLSurfaceView.Renderer, Surfac
protected SurfaceTexture mSTex; protected SurfaceTexture mSTex;
protected MyGLSurfaceView mView; protected MyGLSurfaceView mView;
protected TextView mFpsText;
protected boolean mGLInit = false; protected boolean mGLInit = false;
protected boolean mTexUpdate = false; protected boolean mTexUpdate = false;
@ -27,6 +32,11 @@ public abstract class MyGLRendererBase implements GLSurfaceView.Renderer, Surfac
protected abstract void closeCamera(); protected abstract void closeCamera();
protected abstract void setCameraPreviewSize(int width, int height); protected abstract void setCameraPreviewSize(int width, int height);
public void setFpsTextView(TextView fpsTV)
{
mFpsText = fpsTV;
}
public void onResume() { public void onResume() {
Log.i(LOGTAG, "onResume"); Log.i(LOGTAG, "onResume");
frameCounter = 0; frameCounter = 0;
@ -70,8 +80,16 @@ public abstract class MyGLRendererBase implements GLSurfaceView.Renderer, Surfac
frameCounter++; frameCounter++;
if(frameCounter >= 10) if(frameCounter >= 10)
{ {
int fps = (int) (frameCounter * 1e9 / (System.nanoTime() - lastNanoTime)); final int fps = (int) (frameCounter * 1e9 / (System.nanoTime() - lastNanoTime));
Log.i(LOGTAG, "drawFrame() FPS: "+fps); Log.i(LOGTAG, "drawFrame() FPS: "+fps);
if(mFpsText != null) {
Runnable fpsUpdater = new Runnable() {
public void run() {
mFpsText.setText("FPS: " + fps);
}
};
new Handler(Looper.getMainLooper()).post(fpsUpdater);
}
frameCounter = 0; frameCounter = 0;
lastNanoTime = System.nanoTime(); lastNanoTime = System.nanoTime();
} }

View File

@ -2,14 +2,16 @@ package org.opencv.samples.tutorial4;
import android.content.Context; import android.content.Context;
import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import android.widget.TextView;
class MyGLSurfaceView extends GLSurfaceView { public class MyGLSurfaceView extends GLSurfaceView {
MyGLRendererBase mRenderer; MyGLRendererBase mRenderer;
MyGLSurfaceView(Context context) { public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context); super(context, attrs);
if(android.os.Build.VERSION.SDK_INT >= 21) if(android.os.Build.VERSION.SDK_INT >= 21)
mRenderer = new Camera2Renderer(this); mRenderer = new Camera2Renderer(this);
@ -21,6 +23,10 @@ class MyGLSurfaceView extends GLSurfaceView {
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
} }
public void setFpsTextView(TextView tv) {
mRenderer.setFpsTextView(tv);
}
@Override @Override
public void surfaceCreated(SurfaceHolder holder) { public void surfaceCreated(SurfaceHolder holder) {
super.surfaceCreated(holder); super.surfaceCreated(holder);

View File

@ -5,6 +5,7 @@ import android.content.pm.ActivityInfo;
import android.os.Bundle; import android.os.Bundle;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.TextView;
public class Tutorial4Activity extends Activity { public class Tutorial4Activity extends Activity {
@ -20,8 +21,12 @@ public class Tutorial4Activity extends Activity {
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mView = new MyGLSurfaceView(this); //mView = new MyGLSurfaceView(this, null);
setContentView(mView); //setContentView(mView);
setContentView(R.layout.activity);
mView = (MyGLSurfaceView) findViewById(R.id.my_gl_surface_view);
TextView tv = (TextView)findViewById(R.id.fps_text_view);
mView.setFpsTextView(tv);
} }
@Override @Override