OSDN Git Service

ライブビューデータを拾うために検討中。その4。
[gokigen/FujiCam.git] / opencv41 / src / main / java / org / opencv / android / FpsMeter.java
1 package org.opencv.android;
2
3 import java.text.DecimalFormat;
4
5 import org.opencv.core.Core;
6
7 import android.graphics.Canvas;
8 import android.graphics.Color;
9 import android.graphics.Paint;
10 import android.util.Log;
11
12 public class FpsMeter {
13     private static final String TAG               = "FpsMeter";
14     private static final int    STEP              = 20;
15     private static final DecimalFormat FPS_FORMAT = new DecimalFormat("0.00");
16
17     private int                 mFramesCounter;
18     private double              mFrequency;
19     private long                mprevFrameTime;
20     private String              mStrfps;
21     Paint                       mPaint;
22     boolean                     mIsInitialized = false;
23     int                         mWidth = 0;
24     int                         mHeight = 0;
25
26     public void init() {
27         mFramesCounter = 0;
28         mFrequency = Core.getTickFrequency();
29         mprevFrameTime = Core.getTickCount();
30         mStrfps = "";
31
32         mPaint = new Paint();
33         mPaint.setColor(Color.BLUE);
34         mPaint.setTextSize(20);
35     }
36
37     public void measure() {
38         if (!mIsInitialized) {
39             init();
40             mIsInitialized = true;
41         } else {
42             mFramesCounter++;
43             if (mFramesCounter % STEP == 0) {
44                 long time = Core.getTickCount();
45                 double fps = STEP * mFrequency / (time - mprevFrameTime);
46                 mprevFrameTime = time;
47                 if (mWidth != 0 && mHeight != 0)
48                     mStrfps = FPS_FORMAT.format(fps) + " FPS@" + Integer.valueOf(mWidth) + "x" + Integer.valueOf(mHeight);
49                 else
50                     mStrfps = FPS_FORMAT.format(fps) + " FPS";
51                 Log.i(TAG, mStrfps);
52             }
53         }
54     }
55
56     public void setResolution(int width, int height) {
57         mWidth = width;
58         mHeight = height;
59     }
60
61     public void draw(Canvas canvas, float offsetx, float offsety) {
62         Log.d(TAG, mStrfps);
63         canvas.drawText(mStrfps, offsetx, offsety, mPaint);
64     }
65
66 }