我试着用OpenCV访问Android相机。我成功地创建了一个项目,它运行起来没有任何问题。我尝试使用互联网上的代码,并在我的项目中检查它。但是,应用程序停止了。这似乎是一个RuntimeException问题,但我不知道如何解决它。这是我的代码,我从http://people.oregonstate.edu/~robinsti/CS_496/Tutorial/:网站上复制的
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>
日志:
07-29 15:54:44.066 31188-31188/com.tech.te.app W/art: Failed to find OatDexFile for DexFile /data/data/com.tech.te.app/files/instant-run/dex/slice-slice_2-classes.dex ( canonical path /data/data/com.tech.te.app/files/instant-run/dex/slice-slice_2-classes.dex) with checksum 0x7f90e189 in OatFile /data/data/com.tech.te.app/cache/slice-slice_2-classes.dex 07-29 15:54:45.836 31188-31195/com.tech.te.app W/art: Suspending all threads took: 19.602ms 07-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: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 07-29 15:54:46.596 31188-31188/com.tech.te.app E/VdcInflateDelegate: Exception while inflating java.lang.RuntimeException: Failed to resolve attribute at index 1 at android.content.res.TypedArray.twGetColorStateList(TypedArray.java:437) at android.content.res.TypedArray.getColorStateList(TypedArray.java:419) at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:518) at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472) at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436) at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:708) at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:348) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181) at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186) at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77) at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:83) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511) at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71) at android.app.Activity.performCreate(Activity.java:6112) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) at android.app.ActivityThread.access$900(ActivityThread.java:168) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5753) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 07-29 15:54:46.596 31188-31202/com.tech.te.app I/art: Background sticky concurrent mark sweep GC freed 6652(1091KB) AllocSpace objects, 1(20KB) LOS objects, 14% free, 5MB/6MB, paused 36.125ms total 277.107ms 07-29 15:54:46.616 31188-31202/com.tech.te.app I/art: Background sticky concurrent mark sweep GC freed 274(43KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 5MB/6MB, paused 7.366ms total 16.707ms 07-29 15:54:46.706 31188-31188/com.tech.te.app D/AndroidRuntime: Shutting down VM 07-29 15:54:46.716 31188-31188/com.tech.te.app E/AndroidRuntime: FATAL EXCEPTION: main Process: com.tech.te.app, PID: 31188 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tech.te.app/com.tech.te.app.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) at android.app.ActivityThread.access$900(ActivityThread.java:168) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5753) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343) at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312) at android.support.v7.app.AppCompatDelegateImplV7.onPostCreate(AppCompatDelegateImplV7.java:167) at android.support.v7.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:98) at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1198) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2638) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) at android.app.ActivityThread.access$900(ActivityThread.java:168) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5753) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 07-29 15:54:50.366 31188-31188/com.tech.te.app I/Process: Sending signal. 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;
}
我的应用程序在之前的Android版本上运行完美,在实际设备上从4.0测试到6.0.1。工作室仿真牛轧糖7.0和7.1没问题。然而,当我让我的朋友在他运行7.0的设备上测试APK时,这在几秒钟内就发生了: 崩溃通知 我没有访问该设备的权限(他住得很远),并且我无法在网上找到该特定错误的任何相关信息。原因可能是什么?
我已经厌倦了导入cv和numpy并得到错误。我开始导入cv,但出现以下错误: 恐怖:努比。果心multiarray无法导入 回溯(上次调用): 文件“”,第1行,在 所以我在Ubuntu上安装了numpy,使用: 所以当我导入Numpy我得到: Traceback(最近一次调用最后一次): File",第1行,在 我真的需要帮助。我在Ubuntu上使用Python 2.7.3。
问题内容: 我正在使用其他库在使用OpenCV(2.3.1)的python中进行项目开发。到目前为止,我只是apt- get安装了所有内容,但是现在我想与可能尚未安装所有内容的人共享我的代码。因此,virtualenv似乎是完美的解决方案,但我明白了。 我如何设置virtualenv时是否有问题,还是必须执行其他步骤才能看到我的opencv python绑定? 问题答案: Virtualenv创建
我正在从eclipseADT迁移到android Studio,我可以从Eclipse使用相同的android SDK吗?
我正在AndroidStudio中使用java开发一个android应用程序。我有一张图像视图图片,我想把它的图像设置为。保存在可绘制文件夹中的png。我可以试试 但是,需要Drawable,而返回一个整数(图像的ID)。 如何解决此问题?
问题内容: 我正在创建一个iPhone Web应用程序,从iOS5开始,您现在可以使用position:fixed; 用于标题等 尽管可以使用,但是如果您向上滚动页面顶部,它会显示一段时间的常规灰色区域,然后再无法滚动 有没有办法停止这种滚动?我已经尝试过像溢出这样的事情:隐藏;隐藏。但我似乎找不到任何东西。 PS:我只希望一件事停止滚动,我有一个名为#container的div,但我仍然希望它具