我正在使用带有预构建的OpenCV库的JavaCV库。我想我已经以正确的方式设置了Eclipse,因为我已经将jar包括javacv.jar和javacpp.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崩溃了,我强行关闭应用程序。我是否正确包含了罐子和所需的库?有什么我应该知道的吗?任何帮助将不胜感激。
以下是喜欢Cats的人的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文件夹的文件夹结构
答案 0 :(得分:5)
jars需要位于 project-root / libs 文件夹中,或者将它们标记为在项目的构建路径中导出...
现在它应该有用......