我正在尝试进入适用于 android 的 openCV。所以我开始看教程,最后让 openCV 在我的 android 应用程序上工作。
所以相机预览中的问题看起来像这样:
我在 logcat 中发现的唯一内容是:
2019-09-29 00:56:36.458 28721-28721/de.corestudio.opencvtest E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
2019-09-29 00:56:41.244 28721-28742/de.corestudio.opencvtest E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-09-29 00:56:36.546 28721-28721/de.corestudio.opencvtest E/libc: Access denied finding property "persist.vendor.camera.privapp.list"
所以我试图修复它们,但我发现没有修复它们。
我的 main_activity.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<org.opencv.android.JavaCamera2View
android:id="@+id/myCameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:show_fps="true"/>
</LinearLayout>
还有我的 MainActivity.java
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
CameraBridgeViewBase cameraBridgeViewBase;
Mat mat1, mat2, mat3;
BaseLoaderCallback baseLoaderCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(permission()){
cameraBridgeViewBase = findViewById(R.id.myCameraView);
cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
cameraBridgeViewBase.setCvCameraViewListener(this);
baseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status){
case BaseLoaderCallback.SUCCESS:
cameraBridgeViewBase.enableView();
Toast.makeText(mAppContext, "Success",
Toast.LENGTH_SHORT).show();
break;
default:
super.onManagerConnected(status);
Toast.makeText(mAppContext, "Failure",
Toast.LENGTH_SHORT).show();
break;
}
}
};
}
}
@Override
public void onCameraViewStarted(int width, int height) {
mat1 = new Mat(width,height, CvType.CV_8UC4);
mat2 = new Mat(width,height,CvType.CV_8UC4);
mat3 = new Mat(width,height,CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped() {
mat1.release();
mat2.release();
mat3.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
mat1 = inputFrame.rgba();
return mat1;
}
@Override
protected void onPause() {
super.onPause();
if(cameraBridgeViewBase!= null){
cameraBridgeViewBase.disableView();
}
}
@Override
protected void onResume() {
super.onResume();
super.onPause();
if(!OpenCVLoader.initDebug()){
Toast.makeText(this, "There are problems due OpenCV",
Toast.LENGTH_SHORT).show();
}else{
baseLoaderCallback.onManagerConnected(BaseLoaderCallback.SUCCESS);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(cameraBridgeViewBase!= null){
cameraBridgeViewBase.disableView();
}
}
private boolean permission(){
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CAMERA},
50);
}else{
return true;
}
return false;
}