Android UnsatisfiedLinkError with OpenCV 2.4.9

asked 2014-04-24 20:43:16 -0600

dPeluChe gravatar image

updated 2014-04-25 00:27:37 -0600

berak gravatar image

Im trying to create a camera app with FaceRecognition from an image. So I implement an native Android Camera, a button and an ClickListener. So when I touch that button I generate an image and after that I need check if has any face. And this what Im doing.

So I follow all the Tutorials from OpenCvAndroid Tuts

If I run without any OpenCV methods or calls my app works fine.

here is my onCreate Method who get a camera instance:

    @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Boolean hasCamera = checkCameraHardware(getApplicationContext());
    if(!hasCamera){
        Toast.makeText(getApplicationContext(), "Camera Not Available", Toast.LENGTH_LONG).show();
    }else{


    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    RelativeLayout preview = (RelativeLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    mSnapButton = (ImageButton) findViewById(R.id.button_capture);
    mSnapButton.bringToFront();

    // capture image button
    mSnapButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // get an image from the camera
            mCamera.takePicture(null, null, mPicture);
        }
    });
    }
}

getCameraInstance()

    private Camera getCameraInstance() {
    Camera c = null;
    try { 
        Log.e(" PELUCHE ", " No de Cameras--> "+ c.getNumberOfCameras());   
        c = Camera.open(c.getNumberOfCameras() - 1 ); // attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

SaveImage on SDCARD and after that check with OpenCV

 private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

        if (pictureFile == null) {
            Log.d(TAG,
                    "Error creating media file, check storage permissions");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
        mCamera.startPreview();
        // call alert dialog to display file saved location
        alertDialog("Imagen Guardada",
                "Image saved in: " + DIRECTORY_SAVE_IMAGES + " SDCARD");

        Log.e("PeluChe", "FileName -> " + pictureFile.getName() );


        /*
        static {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        }
        Mat image = Highgui.imread(FaceDetector.class.getResource(pictureFile.getName()).getPath());

        CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("haarcascade_frontalface_alt.xml").getPath());
        Mat image = Highgui.imread(FaceDetector.class.getResource(pictureFile.getName()).getPath());
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image,faceDetections);

        Log.e("PeluChe", "Detect FACES -> " + faceDetections.toArray().length);
       */
    }
};

So If I uncomment the:

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

I get:

java.lang.UnsatisfiedLinkError: Couldn't load opencv_java249: findLibrary returned null

I follow How setup for opencv java jar library .

THX!

Here my Full Code

edit retag flag offensive close merge delete

Comments

System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // that's for *desktop* java, not for android

you'll need something like this :

OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);

and you can only call opencv code , after the async loading of the so's finished

berak gravatar imageberak ( 2014-04-25 00:17:18 -0600 )edit

I implement:

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){
    @Override
    public void onManagerConnected(int status){
        switch(status){
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i("PeluChe", "OpenCV loaded");
                setContentView(R.layout.activity_main);

            }break;
            default:
            {
                super.onManagerConnected(status);
            }break;
        }   
    }
};

and

OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);

But I have an error this error:

The method initAsync(String, Context, LoaderCallbackInterface) in the type OpenCVLoader is not applicable for the arguments (String, new Camera.PictureCallback(){}, BaseLoaderCallback)

dPeluChe gravatar imagedPeluChe ( 2014-04-25 12:18:29 -0600 )edit

imho, the 'this' ptr you pass into OpenCVLoader.initAsync() has to be the Activity, not the PictureCallback. did you put it into the wrong class here ?

berak gravatar imageberak ( 2014-04-25 13:36:51 -0600 )edit