OpenCV 和 Android Studio:"unfortunately app has stopped"



我试图使用OpenCV访问android相机。我成功地创建了一个项目,它运行起来没有任何问题。我试着使用一个来自互联网的代码,并在我的项目中检查它。但是,应用程序停止了。这似乎是一个RuntimeException问题,但我不知道如何解决它。这是我从这个网站复制的代码http://people.oregonstate.edu/~robinsti/CS_496/教程/:

mainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, CameraBridgeViewBase.CvCameraViewListener2{
private CameraBridgeViewBase mOpenCvCameraView;
private Mat mRgba;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.Surface);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
//mOpenCvCameraView.setMaxFrameSize(176, 152);
mOpenCvCameraView.setCvCameraViewListener(this);
}
private BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
super.onManagerConnected(status);
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
mOpenCvCameraView.enableView();
mOpenCvCameraView.setOnTouchListener(MainActivity.this);
} break;
default: {
super.onManagerConnected(status);
} break;
}
}
};
@Override
public void onPause() {
super.onPause();
if(mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
}
@Override
public void onResume() {
super.onResume();
if(mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
}

@Override
public void onDestroy() {
super.onDestroy();
if(!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0,this, mLoaderCallBack);
}
else {
mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
return mRgba;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}

}

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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
tools:context="com.tech.te.smartrotate.MainActivity">
<org.opencv.android.JavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Surface" />
</RelativeLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="                 ">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-feature android:name="android.hardware.camera.front"/>
<uses-feature android:name="android.hardware.camera.front.autofocus"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">        <!--android:theme="@style/AppTheme">-->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Logcat:

07-29 15:54:44.066 31188-31188/com.tech.te.app W/art:在OatFile/data/data/com.tech.te/app/cache/slice-slice_2-classes.dex中找不到用于DexFile/data/doata/com.techn.te.app/files/instant-run/dex/slice-sice_2-cclasses.dex的OatDexFile(规范路径/data/date/com.tech.app/files/instant-run/dox/slice-Sice_2-cClasses.dex),校验和为0x7f90e18907-29 15:54:45.836 31188-31195/com.tech.te.app W/art:挂起所有线程耗时:19.602ms07-29 15:54:45.946 31188-31188/com.tech.te.app D/DisplayManager:DisplayManager()07-29 15:54:46.376 31188-31188/com.tech.te.app W/art:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter android.support.graphics.crawable.VectorDrawableCompat.updateTintFilter07-29 15:54:46.596 31188-31188/com.tech.te.app E/Vdc充气电报:充气时出现异常java.lang.RuntimeException:未能解析索引1处的属性位于android.content.res.TypedArray.twGetColorStateList(TypedArray.java:437)位于android.content.res.TypedArray.getColorStateList(TypedArray.java:419)在android.support.graphics.drawable.VvectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:518)在android.support.graphics.drawable.VvectorDrawableCompat.inflate(VectorDrawableCompat.java:472)在android.support.graphics.drawable.VvectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)位于android.support.v7.widget.AppCompatDrawableManager$VdcInfrateDelegate.createFromXmlInner(AppCompatDrawtableManager.java:708)位于android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawable Manager.java:348)位于android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawable Manager.java:188)位于android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawable Manager.java:181)位于android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawable Manager.java:689)位于android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawable Manager.java:186)在android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)位于android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDeleateImplBase.java:83)在android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDeleateImplV7.java:146)在android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDeleateImplV11.java:28)在android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDeleateImplV14.java:41)在android.support.v7.app.AppCompatDelegate.create上(AppCompatDelegate.java:193)位于android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)位于android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)位于android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)在android.app.Activity.performCreate(Activity.java:612)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2609)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2721)在android.app.ActivityThread.access上$900(ActivityThreads.java:168)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1393)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThreads.java:5753)位于java.lang.reflect.Method.ioke(本机方法)位于java.lang.reflect.Method.ioke(Method.java:372)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:1200)07-29 15:54:46.596 31188-31202/com.tech.te.app I/art:后台粘性并发标记扫描GC释放了6652(1091KB)AllocSpace对象,1(20KB)LOS对象,14%空闲,5MB/6MB,暂停36.125ms,总计277.107ms07-29 15:54:46.616 31188-31202/com.tech.te.app I/art:后台粘性并发标记扫描GC释放274(43KB)AllocSpace对象,0(0B)LOS对象,14%空闲,5MB/6MB,暂停7.366ms总计16.707ms07-29 15:54:46.706 31188-31188/com.tech.te.app D/AndroidRuntime:关闭VM07-29 15:54:46.716 31188-31188/com.tech.te.app E/AndroidRuntime:致命异常:main流程:com.tech.te.app,PID:31188java.lang.RuntimeException:无法启动活动ComponentInfo{com.tech.te.app/com.tech.te.app.MainActivity}:java.lang.IllegalStateException:您需要对此活动使用Theme.AppCompat主题(或子代)。在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2656)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2721)在android.app.ActivityThread.access上$900(ActivityThreads.java:168)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1393)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThreads.java:5753)位于java.lang.reflect.Method.ioke(本机方法)位于java.lang.reflect.Method.ioke(Method.java:372)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:1200)由:java.lang.IollegalStateException引起:您需要在此活动中使用Theme.AppCompat主题(或子代)。在android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDeleateImplV7.java:343)在android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor上(AppCompatDeleateImplV7.java:312)在android.support.v7.app.AppCompatDelegateImplV7.onPostCreate上(AppCompatDeleateImplV7.java:167)位于android.support.v7.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:98)在android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1198)在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2638)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2721)在android.app.ActivityThread.access上$900(ActivityThreads.java:168)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1393)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThreads.java:5753)位于java.lang.reflect.Method.ioke(本机方法)位于java.lang.reflect.Method.ioke(Method.java:372)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:1200)07-29 15:54:50366 31188-31188/com.tech.te.app I/过程:发送信号。PID:31188 SIG:9

非常感谢。

您正在OnDestroy上加载OpenCV,不应该这样做。

@Override
public void onDestroy() {
super.onDestroy();
if(!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0,this, mLoaderCallBack);
}
else {
mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}

此外,您不应该同时使用initDebug和initAsync。

initDebug用于静态初始化,您需要在apk中拥有库。

initAsync将与安装的OpenCV管理器一起使用。

对于静态初始化,这里有一个很好的答案

onResume、onPause和onDestroy应该是这样的:

@Override
protected void onResume()
{
super.onResume();
_javaCameraView.enableView();
}
@Override
protected void onPause() {
if (_javaCameraView != null) {
_javaCameraView.disableView();
}
super.onPause();
}
@Override
public void onDestroy() {
if (_javaCameraView != null) {
_javaCameraView.disableView();
}
super.onDestroy();
}

关于静态初始化的一些信息:

public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2 {
private static boolean openCVStarted = false;
JavaCameraView _javaCameraView;
static{
System.loadLibrary("opencv_java3");

if(!OpenCVLoader.initDebug()) {
Log.d("ERROR", "Unable to load OpenCV");
openCVStarted = false;
} else {
Log.d("SUCCESS", "OpenCV loaded");
openCVStarted = true;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_javaCameraView = (JavaCameraView)findViewById(R.id.cameraView);
//_javaCameraView.setMaxFrameSize(size.width, size.height);
_javaCameraView.setVisibility(SurfaceView.VISIBLE);
_javaCameraView.setCvCameraViewListener(this);
_javaCameraView.enableView();
_javaCameraView.enableFpsMeter();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
_imgBgrTemp = inputFrame.rgba();
// Work with the frame here
return _imgBgrTemp;
}

相关内容

  • 没有找到相关文章

最新更新