Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

NativeCameraView in OpenCV4Android not working

Good morning to all. I have tried to create a simple application for Android that uses opencv4android. This app simply should access through opencv to camera, and captures frames. I have followed this guide: http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-async-initialization If I use org.opencv.android.JavaCameraView it works correctly, but if I want to use org.opencv.NativeCameraView, when I try to run the app, in my logcat I see:

 05-18 13:00:16.979: E/OpenCV::camera(29894): ||libnative_camera_r2.3.3.so
 05-18 13:00:16.979: E/OpenCV::camera(29894): ||libnative_camera_r4.0.3.so
 05-18 13:00:16.979: E/OpenCV::camera(29894): ||libnative_camera_r4.2.0.so
 05-18 13:00:16.979: E/OpenCV::camera(29894): ||libnative_camera_r4.1.1.so
 05-18 13:00:16.979: E/OpenCV::camera(29894): ||libnative_camera_r4.0.0.so
 05-18 13:00:16.979: E/OpenCV::camera(29894): ||libnative_camera_r2.2.0.so
 05-18 13:00:16.979: E/OpenCV::camera(29894): ||libnative_camera_r3.0.1.so
 05-18 13:00:17.239: A/libc(29894): @@@ ABORTING: LIBC: HEAP MEMORY CORRUPTION IN dlmalloc
 05-18 13:00:17.249: A/libc(29894): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 29894 (.videocamopencv)

Here my code:

---MAIN ACTIVITY:

package com.example.videocamopencv;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.NativeCameraView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.SurfaceView;
import android.view.WindowManager;

public class MainActivity extends Activity implements CvCameraViewListener2{
private NativeCameraView mOpenCvCameraView;
private static final String TAG ="VideoCamera App :: ";

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

public MainActivity(){
    Log.i(TAG, "Instantiated new " + this.getClass());
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
     super.onCreate(savedInstanceState);
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     setContentView(R.layout.activity_main);
     mOpenCvCameraView = (NativeCameraView) findViewById(R.id.HelloOpenCvView);
     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
     mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
 public void onPause()
 {
     super.onPause();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onDestroy() {
     super.onDestroy();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onCameraViewStarted(int width, int height) {
 }

 public void onCameraViewStopped() {
 }

 @Override
 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
     return inputFrame.rgba();
 }

}

----MANIFEST.XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videocamopencv"
android:versionCode="21"
android:versionName="2.1" >


<!-- android:icon="@drawable/ic_launcher" -->
<!-- android:label="@string/app_name" -->
<!-- android:theme="@style/AppTheme" -->
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    <activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:configChanges="keyboardHidden|orientation" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>
  <uses-sdk android:minSdkVersion="8" />

  <supports-screens android:resizeable="true"
                  android:smallScreens="true"
                  android:normalScreens="true"
                  android:largeScreens="true"
                  android:anyDensity="true" />

  <uses-permission android:name="android.permission.CAMERA"/>

<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>

</manifest>

----ACTIVITY_MAIN.XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<org.opencv.android.NativeCameraView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/HelloOpenCvView"
    opencv:show_fps="true"
    opencv:camera_id="any" />

</LinearLayout>

Someone can help me?