OpenCV 4.5.0 空 JavaCameraView 和 JavaCamera2View
OpenCV 4.5.0 empty JavaCameraView and JavaCamera2View
我正在使用目前最新的 OpenCV 4.5.0 版来获取相机帧并对其进行处理。但问题是在完成所有操作后,我的 JavaCameraView 是空的,里面什么也没有。与 JavaCamera2View 相同。我以前使用过 openCV 3.4.5 相同的代码工作正常。也直到 4.1.0,从 4.1.1 到现在,问题是一样的,尝试了很多东西,但没有运气。我也尝试过 解决方案,但没有成功使用 4.5.0。我的应用程序至少需要 4.4.0,因为我将使用 YOLO 4 和 CV。我只是使用基本代码来查看框架。我的应用程序中没有收到任何类型的错误消息。
这是代码。
MainActivity.java
package com.learn.ocv450;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.widget.Toast;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCamera2View;
import org.opencv.android.JavaCameraView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
CameraBridgeViewBase cameraBridgeViewBase;
BaseLoaderCallback baseLoaderCallback;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraBridgeViewBase = (JavaCamera2View) findViewById(R.id.CameraView);
cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
cameraBridgeViewBase.setCvCameraViewListener(this);
baseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
super.onManagerConnected(status);
if (status == BaseLoaderCallback.SUCCESS) {
//OpenCVLoader.initDebug();
//System.loadLibrary("opencv_java4");
cameraBridgeViewBase.enableView();
} else {
super.onManagerConnected(status);
}
}
};
}
@Override
protected void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
Toast.makeText(this, "There is problem", 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 void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat frame = inputFrame.rgba();
if (counter % 2 == 0){
Core.flip(frame, frame, 1);
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2GRAY);
}
counter++;
Toast.makeText(this, "Getting Camera Frame", Toast.LENGTH_SHORT).show();
return frame;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<org.opencv.android.JavaCamera2View
android:id="@+id/CameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="visible">
</org.opencv.android.JavaCamera2View>
</RelativeLayout>
清单权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
顺便说一句,我已经按照基本练习正确地导入了模块并将其添加为依赖项。此外,我在调用 OpenCVLoader.initDebug();
时收到一条成功消息,因此 openCV 已正确安装。
在 OpenCV 4.1.1 及更高版本中,您需要明确告诉 OpenCV 该应用程序具有相机权限。
尝试插入
“cameraBridgeViewBase.setCameraPermissionGranted();”
前
“cameraBridgeViewBase.enableView();”
,假设该应用程序已经拥有相机权限?
我正在使用目前最新的 OpenCV 4.5.0 版来获取相机帧并对其进行处理。但问题是在完成所有操作后,我的 JavaCameraView 是空的,里面什么也没有。与 JavaCamera2View 相同。我以前使用过 openCV 3.4.5 相同的代码工作正常。也直到 4.1.0,从 4.1.1 到现在,问题是一样的,尝试了很多东西,但没有运气。我也尝试过
MainActivity.java
package com.learn.ocv450;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.widget.Toast;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCamera2View;
import org.opencv.android.JavaCameraView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
CameraBridgeViewBase cameraBridgeViewBase;
BaseLoaderCallback baseLoaderCallback;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraBridgeViewBase = (JavaCamera2View) findViewById(R.id.CameraView);
cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
cameraBridgeViewBase.setCvCameraViewListener(this);
baseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
super.onManagerConnected(status);
if (status == BaseLoaderCallback.SUCCESS) {
//OpenCVLoader.initDebug();
//System.loadLibrary("opencv_java4");
cameraBridgeViewBase.enableView();
} else {
super.onManagerConnected(status);
}
}
};
}
@Override
protected void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
Toast.makeText(this, "There is problem", 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 void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat frame = inputFrame.rgba();
if (counter % 2 == 0){
Core.flip(frame, frame, 1);
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2GRAY);
}
counter++;
Toast.makeText(this, "Getting Camera Frame", Toast.LENGTH_SHORT).show();
return frame;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<org.opencv.android.JavaCamera2View
android:id="@+id/CameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="visible">
</org.opencv.android.JavaCamera2View>
</RelativeLayout>
清单权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
顺便说一句,我已经按照基本练习正确地导入了模块并将其添加为依赖项。此外,我在调用 OpenCVLoader.initDebug();
时收到一条成功消息,因此 openCV 已正确安装。
在 OpenCV 4.1.1 及更高版本中,您需要明确告诉 OpenCV 该应用程序具有相机权限。 尝试插入 “cameraBridgeViewBase.setCameraPermissionGranted();” 前 “cameraBridgeViewBase.enableView();” ,假设该应用程序已经拥有相机权限?