#android #opencv #android-ndk #android-camera
#Android #opencv #android-ndk #android-камера
Вопрос:
Я пытаюсь использовать машинный код для обработки некоторых изображений с использованием класса 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
Комментарии:
1. У вас есть разрешения на камеру в вашем
AndroidManifest.xml
?2. Да, все разрешения были установлены.
3. Возможно, ваше устройство не совместимо с OpenCV. Попробуйте запустить их пример приложения play.google.com/store/apps /… .
Ответ №1:
Начиная с 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.
Редактировать: исправить опечатку в коде