作为一名首次接触Android开发的开发者,我在过去几个月中一直在努力解决OpenCV的JavaCameraView问题,该问题始终显示空白/黑屏。我已经遵循了许多在线教程,但是即使完全按照这些教程操作也没有取得任何结果。我知道我的代码正确加载了OpenCV,并且Logcat描述了代码尝试启动相机视图,但是仍然无法解决问题。我需要帮助。代码中存在问题的部分如下:
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
//Activate Camera
private BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback (this) {
@Override
public void onManagerConnected(int status) {
if (status == BaseLoaderCallback.SUCCESS) {
OpenCVCamView.enableView(); //Enable Cam View
Log.d(TAG, "Tried enabling Camera View!");
} else { super.onManagerConnected(status); }
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "on Create");
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main); //UI Layout loaded
OpenCVCamView = findViewById(R.id.cam_view);
textView = findViewById(R.id.arduino_log);
textView.setVisibility(SurfaceView.INVISIBLE);
OpenCVCamView.setVisibility(SurfaceView.VISIBLE);
OpenCVCamView.setCvCameraViewListener(this);
@Override
public void onCameraViewStarted(int width, int height) {
Log.d(TAG, "on Camera view Started");
rgba = new Mat (height, width, CvType.CV_8UC4);
rgbaF = new Mat (height, width, CvType.CV_8UC4);
rgbaT = new Mat (height, width, CvType.CV_8UC4);
intMat = new Mat(height, width, CvType.CV_8UC4);
gray = new Mat (height, width, CvType.CV_8UC1);
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
rgba = inputFrame.rgba();
Log.d(TAG, "on Camera Frame");
Core.transpose(rgba,rgbaT);
Imgproc.resize(rgbaT,rgbaF,rgbaF.size(),0,0,0);
Core.flip(rgbaF,rgba,1);
return rgba;
}
}
我的activity_main XML文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<org.opencv.android.JavaCameraView
android:id="@+id/cam_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
opencv:camera_id="any"/>
<TextView
android:id="@+id/arduino_log"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dp"
android:visibility="invisible" />
</RelativeLayout>
我正在使用适用于Android的OpenCV 4.3版本。