我是Javacv和android的新手,我完全不知道如何处理这个错误。我正在尝试加载haarcascade文件但不幸的是应用程序总是崩溃。
这是我的mainactivity.java代码
package com.example.cameraapp;
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private static String logtag = "CAMERA APP";
private Uri imageUri;
private static int TAKE_PICTURE=1;
String s ;
Bitmap bitmap;
ImageView k ;
private static final int MAX_FACES = 5;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button c = (Button)findViewById(R.id.button_camera);
c.setOnClickListener(cameraListener);
}
private OnClickListener cameraListener = new OnClickListener(){
public void onClick(View v){takePhoto(v);}
};
private void takePhoto(View v){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File image = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
imageUri = Uri.fromFile(image);
s = image.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode==Activity.RESULT_OK){
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
try{
bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
}catch(Exception e){Log.e(logtag,e.toString());
}
try{
String uri = "android.resource://" + getPackageName() + "/"+R.raw.haarcascade_frontalface_default;
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(uri+" "+s);
FaceDetector s = new FaceDetector();
s.detect(uri);
}catch(Exception e){Toast.makeText(MainActivity.this, Log.e(logtag,e.toString()), Toast.LENGTH_SHORT).show();}
}
}
}
这是将加载文件的facedetector类
package com.example.cameraapp;
import java.io.*;
import android.content.Context;
import android.graphics.Bitmap;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
public class FaceDetector {
public void detect(String XML_FILE){
CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(XML_FILE));
}
}
以下是我从logcat获得的错误:
09-07 15:00:59.169: D/dalvikvm(1298): GC_FOR_ALLOC freed 2K (69), 2% free 22074K/22404K, paused 15ms, total 15ms
09-07 15:00:59.396: D/skia(1298): jpeg_decoder finish successfully, L:1406, reuse 0!!!
09-07 15:00:59.420: E/dalvikvm(1298): Could not find class 'com.googlecode.javacv.cpp.opencv_objdetect$CvHaarClassifierCascade', referenced from method com.example.cameraapp.FaceDetector.detect
09-07 15:00:59.421: W/dalvikvm(1298): VFY: unable to resolve new-instance 1164 (Lcom/googlecode/javacv/cpp/opencv_objdetect$CvHaarClassifierCascade;) in Lcom/example/cameraapp/FaceDetector;
09-07 15:00:59.421: D/dalvikvm(1298): VFY: replacing opcode 0x22 at 0x0000
09-07 15:00:59.422: D/dalvikvm(1298): DexOpt: unable to opt direct call 0x2462 at 0x06 in Lcom/example/cameraapp/FaceDetector;.detect
09-07 15:00:59.423: D/AndroidRuntime(1298): Shutting down VM
09-07 15:00:59.423: W/dalvikvm(1298): threadid=1: thread exiting with uncaught exception (group=0x40e419a8)
09-07 15:00:59.423: W/dalvikvm(1298): threadid=1: uncaught exception occurred
09-07 15:00:59.423: W/System.err(1298): java.lang.NoClassDefFoundError: com.googlecode.javacv.cpp.opencv_objdetect$CvHaarClassifierCascade
09-07 15:00:59.425: W/System.err(1298): at com.example.cameraapp.FaceDetector.detect(FaceDetector.java:22)
09-07 15:00:59.425: W/System.err(1298): at com.example.cameraapp.MainActivity.onActivityResult(MainActivity.java:86)
09-07 15:00:59.425: W/System.err(1298): at android.app.Activity.dispatchActivityResult(Activity.java:5311)
09-07 15:00:59.426: W/System.err(1298): at android.app.ActivityThread.deliverResults(ActivityThread.java:3529)
09-07 15:00:59.426: W/System.err(1298): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3576)
09-07 15:00:59.426: W/System.err(1298): at android.app.ActivityThread.access$1100(ActivityThread.java:165)
09-07 15:00:59.426: W/System.err(1298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
09-07 15:00:59.426: W/System.err(1298): at android.os.Handler.dispatchMessage(Handler.java:107)
09-07 15:00:59.426: W/System.err(1298): at android.os.Looper.loop(Looper.java:194)
09-07 15:00:59.426: W/System.err(1298): at android.app.ActivityThread.main(ActivityThread.java:5391)
09-07 15:00:59.426: W/System.err(1298): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 15:00:59.426: W/System.err(1298): at java.lang.reflect.Method.invoke(Method.java:525)
09-07 15:00:59.426: W/System.err(1298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
09-07 15:00:59.426: W/System.err(1298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-07 15:00:59.426: W/System.err(1298): at dalvik.system.NativeStart.main(Native Method)
09-07 15:00:59.426: W/dalvikvm(1298): threadid=1: calling UncaughtExceptionHandler
09-07 15:00:59.429: E/AndroidRuntime(1298): FATAL EXCEPTION: main
09-07 15:00:59.429: E/AndroidRuntime(1298): java.lang.NoClassDefFoundError: com.googlecode.javacv.cpp.opencv_objdetect$CvHaarClassifierCascade
09-07 15:00:59.429: E/AndroidRuntime(1298): at com.example.cameraapp.FaceDetector.detect(FaceDetector.java:22)
09-07 15:00:59.429: E/AndroidRuntime(1298): at com.example.cameraapp.MainActivity.onActivityResult(MainActivity.java:86)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.app.Activity.dispatchActivityResult(Activity.java:5311)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.app.ActivityThread.deliverResults(ActivityThread.java:3529)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3576)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.app.ActivityThread.access$1100(ActivityThread.java:165)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.os.Handler.dispatchMessage(Handler.java:107)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.os.Looper.loop(Looper.java:194)
09-07 15:00:59.429: E/AndroidRuntime(1298): at android.app.ActivityThread.main(ActivityThread.java:5391)
09-07 15:00:59.429: E/AndroidRuntime(1298): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 15:00:59.429: E/AndroidRuntime(1298): at java.lang.reflect.Method.invoke(Method.java:525)
09-07 15:00:59.429: E/AndroidRuntime(1298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
09-07 15:00:59.429: E/AndroidRuntime(1298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-07 15:00:59.429: E/AndroidRuntime(1298): at dalvik.system.NativeStart.main(Native Method)