Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JavaCV dilemma, NoClassDefFoundError thrown inside of method 'draw' when IplImage is created

I am using the JavaCV library with pre-built OpenCV libraries for Android. I think I have setup Eclipse the right way, because I have included the jars both javacv.jar and javacpp.jar. In addition, the java-cv-android-arm.jar, in my project. Everything compiles fine, no errors, warning, anything that should be suspicious of something that will go wrong at runtime. But I get NoClassDefFOundError exception that is thrown in this method body below:

@Override
    public void draw(Canvas canvas)
    {
    try
    {
        canvas.drawColor(Color.BLUE);
        if (current != null)
        {
        int width = current.getWidth();
        int height = current.getHeight();
        IplImage i = IplImage.create(width, height, IPL_DEPTH_8U, 1); // I assume here is where the exception gets thrown
        ByteBuffer buffer = i.getByteBuffer();
        current.copyPixelsToBuffer(buffer);
        // We need a grayscale image in order to do the recognition, so
        // we
        // create a new image of the same size as the original one.
        IplImage grayImage = IplImage.create(i.width(), i.height(),
            IPL_DEPTH_8U, 1);
        // We convert the original image to grayscale.
        cvCvtColor(i, grayImage, CV_BGR2GRAY);
        CvMemStorage storage = CvMemStorage.create();
        // We instantiate a classifier cascade to be used for detection,
        // using the cascade definition.
        CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
            cvLoad("haarcascade_frontalface_alt.xml"));
        // We detect the faces.
        CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage,
            1.1, 1, 0);
        // We iterate over the discovered faces and draw yellow
        // rectangles around them.
        for (int index = 0; index < faces.total(); index++)
        {
            CvRect r = new CvRect(cvGetSeqElem(faces, index));
            cvRectangle(i, cvPoint(r.x(), r.y()),
                cvPoint(r.x() + r.width(), r.y() + r.height()),
                opencv_core.CvScalar.YELLOW, 1, CV_AA, 0);
        }
        Bitmap b = BitmapFactory.decodeByteArray(i.getByteBuffer()
            .array(), 0, i.getByteBuffer().array().length);
        canvas.drawBitmap(b, x, y, paint);
        canvas.drawText(new Date().toLocaleString(), canvas.getWidth() - 100,
            canvas.getHeight() - 50, paint);
        paint.setColor(Color.GREEN);
        }
    } catch (Exception e)
    {
        canvas.drawColor(Color.RED);

        canvas.drawText(
            "Handled exception occurred in panel:\n" + e.getMessage(),
            250, 250, paint);
        paint.setColor(Color.GREEN);
    }
    super.draw(canvas);
    }

And of course, right after the exception is thrown, my Android crashes, and I force close the application. Did I include the jars, and required libraries correctly? Is there anything that I should be aware of? Any help would be greatly appreciated.

Here is the LogCat for those who love Cats (insert emoticon here):

05-03 19:07:53.217: E/AndroidRuntime(741): FATAL EXCEPTION: main
05-03 19:07:53.217: E/AndroidRuntime(741): java.lang.NoClassDefFoundError: com.googlecode.javacv.cpp.opencv_core$IplImage
05-03 19:07:53.217: E/AndroidRuntime(741):  at home.security.DrawingPanel.draw(DrawingPanel.java:81)
05-03 19:07:53.217: E/AndroidRuntime(741):  at home.security.Main$2.run(Main.java:105)
05-03 19:07:53.217: E/AndroidRuntime(741):  at android.os.Handler.handleCallback(Handler.java:587)
05-03 19:07:53.217: E/AndroidRuntime(741):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 19:07:53.217: E/AndroidRuntime(741):  at android.os.Looper.loop(Looper.java:123)
05-03 19:07:53.217: E/AndroidRuntime(741):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-03 19:07:53.217: E/AndroidRuntime(741):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 19:07:53.217: E/AndroidRuntime(741):  at java.lang.reflect.Method.invoke(Method.java:507)
05-03 19:07:53.217: E/AndroidRuntime(741):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-03 19:07:53.217: E/AndroidRuntime(741):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-03 19:07:53.217: E/AndroidRuntime(741):  at dalvik.system.NativeStart.main(Native Method)

Folder Structure Of 'libs Folder

enter image description here

like image 570
Daniel Lopez Avatar asked May 03 '12 19:05

Daniel Lopez


1 Answers

The jars need to be in the project-root/libs folder or to mark them as exported in the build path of the project...

now it should work...

like image 161
TacB0sS Avatar answered Nov 02 '22 00:11

TacB0sS