5

我正在使用 JavaCV 库和为 Android 预构建的 OpenCV 库。我想我已经以正确的方式设置了 Eclipse,因为我已经包含了 javacv.jar 和 javacpp.jar 的 jar。另外,java-cv-android-arm.jar,在我的项目中。一切都编译得很好,没有错误,警告,任何应该怀疑在运行时会出错的东西。但是我得到了下面这个方法体中抛出的 NoClassDefFOundError 异常:

@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);
    }

当然,在抛出异常之后,我的 Android 崩溃了,我强制关闭了应用程序。我是否正确包含了罐子和所需的库?有什么我应该注意的吗?任何帮助将不胜感激。

这是为喜欢猫的人准备的 LogCat(在此处插入表情符号):

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)

'libs 文件夹的文件夹结构

在此处输入图像描述

4

1 回答 1

5

jars 需要位于project-root/libs文件夹中,或者在项目的构建路径中将它们标记为导出...

现在它应该工作......

于 2012-05-03T20:20:26.540 回答