gpt4 book ai didi

android - opencv在变量中获取fps

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:07 25 4
gpt4 key购买 nike

我是 Android 开发新手。我想用 OpenCV 制作一个应用程序。对于我的应用程序,我希望将预览的 FPS 存储在一个变量中。我在 OpenCVLibrary 文件夹中找到了一个名为 FpsMeter 的类,但是当我调用 fpsmeter.init() 时,我的应用程序崩溃了。我尝试了不同的东西,比如设置

cameraBridgeViewBase.enableFpsMeter();

cameraBridgeViewBase.disableFpsMeter();

fpsmeter.setResolution(320, 240);

但每次都崩溃。

有人可以帮帮我吗?提前致谢。

package be.anton.OpenCV_test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.Toast;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.FpsMeter;
import org.opencv.android.JavaCameraView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {

CameraBridgeViewBase cameraBridgeViewBase;
BaseLoaderCallback baseLoaderCallback;
FpsMeter fpsMeter;
Mat mat1;
private static final String TAG = "MyActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraBridgeViewBase = (JavaCameraView) findViewById(R.id.cam);
cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
cameraBridgeViewBase.enableFpsMeter();
cameraBridgeViewBase.setCvCameraViewListener(this);
baseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
// super.onManagerConnected(status);
switch (status) {
case BaseLoaderCallback.SUCCESS: {
cameraBridgeViewBase.enableView();
break;
}
default: {
super.onManagerConnected(status);
break;
}
}
}
};
fpsMeter.init(); // when I add this line my app crashes

}

@Override
protected void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
Toast.makeText(getApplicationContext(), "OpenCV could not be loaded.", Toast.LENGTH_SHORT).show();
} else {
baseLoaderCallback.onManagerConnected(BaseLoaderCallback.SUCCESS);
}
}

@Override
protected void onPause() {
super.onPause();
if (cameraBridgeViewBase != null) {
cameraBridgeViewBase.disableView();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (cameraBridgeViewBase != null) {
cameraBridgeViewBase.disableView();
}
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mat1 = inputFrame.rgba();
return mat1;
}

@Override
public void onCameraViewStopped() {
mat1.release();
}

@Override
public void onCameraViewStarted(int width, int height) {
mat1 = new Mat(width, height, CvType.CV_8UC4);
}

}

最佳答案

解决方案:我创建了自己的 FPS 仪表。这是代码:

我使用的变量:

Textview txt1;
int mFPS;
long startTime = 0;
long currentTime = 1000;

在 onCreate() 中设置:

runOnUiThread(new Runnable() {
@Override
public void run() {
txt1 = (TextView) findViewById(R.id.txt1);
}
});

这是我的 onCameraFrame():

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mat1 = inputFrame.rgba();
//Log.i(TAG, mat1.get(128, 128).toString());


runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentTime - startTime >= 1000) {
txt1.setText("FPS: " + String.valueOf(mFPS));
mFPS = 0;
startTime = System.currentTimeMillis();
}
currentTime = System.currentTimeMillis();
mFPS += 1;

}
});
return mat1;
}

关于android - opencv在变量中获取fps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51534190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com