NDK或OpenCV相机崩溃这个应用程序?为什么?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NDK或OpenCV相机崩溃这个应用程序?为什么?相关的知识,希望对你有一定的参考价值。
我正在尝试使用本机代码来处理一些使用JavaCameraView类的图像。一切似乎都很好但是当我运行应用程序时,它会崩溃并显示以下消息:“您的设备似乎不支持相机(或其锁定)。应用程序将关闭。”
- 我已经将构建系统更改为所有设备但仍然得到相同的错误。
- 尝试使用CameraBridgeViewBase,但错误消息不会更改。
- 清单中的权限都已设置
什么可能导致这次崩溃?
这是布局:
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sample.myapplicationocv.MainActivity">
<org.opencv.android.JavaCameraView
android:id="@+id/java_camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这是活动:
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{
JavaCameraView javaCameraView;
Mat matIn, mOut;
// CameraBridgeViewBase mCameraView;
static {
System.loadLibrary("MyLibs");
}
BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
javaCameraView.enableView();
// mCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
javaCameraView = (JavaCameraView) findViewById(R.id.java_camera_view);
javaCameraView.setVisibility(View.VISIBLE);
javaCameraView.setCvCameraViewListener(this);
// mCameraView = (CameraBridgeViewBase) findViewById(R.id.hello);
// mCameraView.setVisibility(SurfaceView.VISIBLE);
// mCameraView.setCvCameraViewListener(this);
}
@Override
public void onCameraViewStarted(int width, int height) {
matIn = new Mat(height, width, CvType.CV_8UC4);
mOut = new Mat(height, width, CvType.CV_8UC1);
}
@Override
public void onCameraViewStopped() {
matIn.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
matIn = inputFrame.rgba();
mOut = new Mat();
matIn = inputFrame.rgba();
NativeMethods.binnarize(matIn.getNativeObjAddr(),mOut.getNativeObjAddr());
return mOut;
}
@Override
protected void onPause() {
super.onPause();
if(javaCameraView!=null) {
javaCameraView.disableView();
}
// if (mCameraView!=null)
// mCameraView.disableView();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(javaCameraView!=null) {
javaCameraView.disableView();
}
// if (mCameraView!=null)
// mCameraView.disableView();
}
@Override
protected void onResume() {
super.onResume();
if (OpenCVLoader.initDebug()) {
Log.d("MAINAC", "onResume: OpenCV loaded successfully");
baseLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
} else {
Log.i("MAINAC", "onResume: OpenCV not Loaded succesfully");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, baseLoaderCallback);
}
}
}
这是logcat输出:
10-19 10:16:42.473 2501-2501/? I/art: Late-enabling -Xcheck:jni
10-19 10:16:42.636 2501-2501/? D/CameraBridge: Attr count: 3
10-19 10:16:42.646 2501-2501/? D/OpenCV/StaticHelper: Trying to get library list
10-19 10:16:42.648 2501-2501/? E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
10-19 10:16:42.648 2501-2501/? D/OpenCV/StaticHelper: Library list: ""
10-19 10:16:42.648 2501-2501/? D/OpenCV/StaticHelper: First attempt to load libs
10-19 10:16:42.648 2501-2501/? D/OpenCV/StaticHelper: Trying to init OpenCV libs
10-19 10:16:42.648 2501-2501/? D/OpenCV/StaticHelper: Trying to load library opencv_java3
10-19 10:16:42.649 2501-2501/? D/OpenCV/StaticHelper: Library opencv_java3 loaded
10-19 10:16:42.649 2501-2501/? D/OpenCV/StaticHelper: First attempt to load libs is OK
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: General configuration for OpenCV 3.1.0 =====================================
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Version control: 3.1.0
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Platform:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Host: Darwin 15.0.0 x86_64
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Target: Android 1 i686
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: CMake: 3.3.2
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: CMake generator: Ninja
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: CMake build tool: /usr/local/bin/ninja
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Configuration: Release
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C/C++:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Built as dynamic libs?: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C++ Compiler: /usr/local/bin/ccache /opt/android/android-ndk-r10e/toolchains/x86-4.8/prebuilt/darwin-x86_64/bin/i686-linux-android-g++ (ver 4.8)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C++ flags (Release): -fexceptions -frtti -fpic --sysroot=/opt/android/android-ndk-r10e/platforms/android-9/arch-x86 -funwind-tables -funswitch-loops -finline-limit=300 -fsigned-char -no-canonical-prefixes -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -march=i686 -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -mfpmath=sse -fvisibility=hidden -fvisibility-inlines-hidden -fomit-frame-pointer -fstrict-aliasing -O2 -DNDEBUG -DNDEBUG
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C++ flags (Debug): -fexceptions -frtti -fpic --sysroot=/opt/android/android-ndk-r10e/platforms/android-9/arch-x86 -funwind-tables -funswitch-loops -finline-limit=300 -fsigned-char -no-canonical-prefixes -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -march=i686 -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -mfpmath=sse -fvisibility=hidden -fvisibility-inlines-hidden -fno-omit-frame-pointer -fno-strict-aliasing -O0 -g -DDEBUG -D_DEBUG -O0 -DDEBUG -D_DEBUG
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C Compiler: /usr/local/bin/ccache /opt/android/android-ndk-r10e/toolchains/x86-4.8/prebuilt/darwin-x86_64/bin/i686-linux-android-gcc
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C flags (Release): -fexceptions -fpic --sysroot=/opt/android/android-ndk-r10e/platforms/android-9/arch-x86 -funwind-tables -funswitch-loops -finline-limit=300 -fsigned-char -no-canonical-prefixes -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -march=i686 -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -mfpmath=sse -fvisibility=hidden -fomit-frame-pointer -fstrict-aliasing -O2 -DNDEBUG -DNDEBUG
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C flags (Debug): -fexceptions -fpic --sysroot=/opt/android/android-ndk-r10e/platforms/android-9/arch-x86 -funwind-tables -funswitch-loops -finline-limit=300 -fsigned-char -no-canonical-prefixes -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -march=i686 -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -mfpmath=sse -fvisibility=hidden -fno-omit-frame-pointer -fno-strict-aliasing -O0 -g -DDEBUG -D_DEBUG -O0 -DDEBUG -D_DEBUG
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Linker flags (Release): -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Linker flags (Debug): -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Precompiled headers: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Extra dependencies: z dl m log
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: 3rdparty dependencies: libjpeg libwebp libpng libtiff libjasper IlmImf tbb
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: OpenCV modules:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: To be built: core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres features2d calib3d java stitching videostab
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Disabled: world
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Disabled by dependency: -
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev python2 ts viz
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Android:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Android ABI: x86
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: STL type: gnustl_static
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Native API level: android-9
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: SDK target: android-14
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Android NDK: /opt/android/android-ndk-r10e (toolchain: x86-4.8)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: android tool: /opt/android/android-sdk-macosx/tools/android (Android SDK Tools, revision 24.1.2.)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Google Play manager: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Android examples: YES
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: GUI:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: GTK+: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: GThread : NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: GtkGlExt: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: OpenGL support: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: VTK support: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Media I/O:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: ZLib: z (ver 1.2.3)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: JPEG: build (ver 90)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: WEBP: build (ver 0.3.1)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: PNG: build (ver 1.6.19)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: TIFF: build (ver 42 - 4.0.2)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: JPEG 2000: build (ver 1.900.1)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: OpenEXR: build (ver 1.7.1)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: GDAL: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Video I/O:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Parallel framework: TBB (ver 4.3 interface 8002)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Other third-party libraries:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Use IPP: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Use IPP Async: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Use Eigen: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Use Cuda: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Use OpenCL: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Use custom HAL: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Python 2:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Interpreter: /opt/pythonenv/build/bin/python2.7 (ver 2.7.10)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Python 3:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Interpreter: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Python (for build): /opt/pythonenv/build/bin/python2.7
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Java:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: ant: /usr/local/bin/ant (ver 1.9.4)
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Java wrappers: YES
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Java tests: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Matlab: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Tests and samples:
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Tests: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Performance tests: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: C/C++ Examples: NO
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: Install path: /Volumes/Linux/builds/master_pack-android/build/o4a/install
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: cvconfig.h is in: /Volumes/Linux/builds/master_pack-android/build/o4a
10-19 10:16:42.653 2501-2501/? I/OpenCV/StaticHelper: -----------------------------------------------------------------
10-19 10:16:42.653 2501-2501/? D/MAINAC: onResume: OpenCV loaded successfully
10-19 10:16:42.653 2501-2501/? D/CameraBridge: call checkCurrentState
10-19 10:16:42.668 2501-2514/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-19 10:16:42.810 2501-2514/? D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
10-19 10:16:42.811 2501-2514/? D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
10-19 10:16:42.818 2501-2514/? D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
[ 10-19 10:16:42.824 2501: 2514 D/ ]
HostConnection::get() New Host Connection established 0xeeb12730, tid 2514
10-19 10:16:42.850 2501-2514/? I/OpenGLRenderer: Initialized EGL, version 1.4
10-19 10:16:42.962 2501-2514/? W/EGL_emulation: eglSurfaceAttrib not implemented
10-19 10:16:42.962 2501-2514/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf2cfd3c0, error=EGL_SUCCESS
10-19 10:16:43.259 2501-2501/? D/CameraBridge: call surfaceChanged event
10-19 10:16:43.259 2501-2501/? D/CameraBridge: call checkCurrentState
10-19 10:16:43.259 2501-2501/? D/CameraBridge: call processExitState: 0
10-19 10:16:43.260 2501-2501/? D/CameraBridge: call processEnterState: 1
10-19 10:16:43.260 2501-2501/? D/CameraBridge: call onEnterStartedState
10-19 10:16:43.260 2501-2501/? D/JavaCameraView: Connecting to camera
10-19 10:16:43.260 2501-2501/? D/JavaCameraView: Initialize java camera
10-19 10:16:43.260 2501-2501/? D/JavaCameraView: Trying to open camera with old open()
10-19 10:16:43.265 2501-2501/? W/CameraBase: An error occurred while connecting to camera: 0
10-19 10:16:43.265 2501-2501/? E/JavaCameraView: Camera is not available (in use or does not exist): Fail to connect to camera service
10-19 10:16:43.266 2501-2501/? D/JavaCameraView: Trying to open camera with new open(0)
10-19 10:16:43.268 2501-2501/? W/CameraBase: An error occurred while connecting to camera: 0
10-19 10:16:43.268 2501-2501/? E/JavaCameraView: Camera #0failed to open: Fail to connect to camera service
10-19 10:16:43.268 2501-2501/? D/JavaCameraView: Trying to open camera with new open(1)
10-19 10:16:43.269 2501-2501/? W/CameraBase: An error occurred while connecting to camera: 1
10-19 10:16:43.269 2501-2501/? E/JavaCameraView: Camera #1failed to open: Fail to connect to camera service
10-19 10:16:43.494 2501-2501/com.sample.myapplicationocv I/Choreographer: Skipped 45 frames! The application may be doing too much work on its main thread.
10-19 10:16:43.961 2501-2514/com.sample.myapplicationocv W/EGL_emulation: eglSurfaceAttrib not implemented
10-19 10:16:43.961 2501-2514/com.sample.myapplicationocv W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeeb13e40, error=EGL_SUCCESS
10-19 11:47:11.404 2501-2506/com.sample.myapplicationocv W/art: Suspending all threads took: 7.478ms
答案
从Android 6.0(API级别23)开始,您必须先获得用户的许可,然后才能访问手机中的某些功能,包括使用传感器和读/写外部存储。所以要修复你的问题,你需要在android清单中请求权限,如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app.myapp"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
...
如果您仍然遇到问题,则可以在运行时请求权限。这是你如何做到的:
String s = "CAMERA";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!hasPermissionInManifest(getBaseContext(), s)) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
}
}
}
有关请求用户权限的更多详细信息,请查看Android Developer网站。
编辑:修复代码中的拼写错误
以上是关于NDK或OpenCV相机崩溃这个应用程序?为什么?的主要内容,如果未能解决你的问题,请参考以下文章