我一直在关注如何在android studio中使用opencv和ndk的一些教程。我已经执行了一些youtube频道教程。很可能,我一直坚持这个问题一段时间了 这是详细的错误:
Process: medianet.com.helloopencv, PID: 17079
java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.core.Mat.n_Mat(int, int, int) (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__III)
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:37)
at medianet.com.helloopencv.MainActivity.onCameraViewStarted(MainActivity.java:95)
这是我的cpp文件,它应该检测人脸。这是一个来自opencv.org的简单代码:
#include "medianet_com_helloopencv_OpencvClass.h"
JNIEXPORT void JNICALL Java_medianet_com_helloopencv_OpencvClass_faceDetection
(JNIEnv *, jclass, jlong addRgba){
Mat& frame= *(Mat*) addRgba;
detect(frame);
}
void detect(Mat& frame){
String face_cascade_name = "/storage/emulated/0/data/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "/storage/emulated/0/data/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return ; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return ; };
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t j = 0; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
}
她是我的java课程。这个类是我的主要活动,它将启动相机并调用本机脚本中实现的方法,该脚本将检测到面部
public class MainActivity extends AppCompatActivity implements
CameraBridgeViewBase.CvCameraViewListener2 {
private static final String TAG = "MainActivity";
JavaCameraView javaCameraView;
Mat mRgba/*, imggray, imgCanny*/;
BaseLoaderCallback mloderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case BaseLoaderCallback.SUCCESS: {
javaCameraView.enableView();
break;
}
default: {
super.onManagerConnected(status);
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
javaCameraView = findViewById(R.id.cameraview);
initOpencv();
/* set for front camera
* if index=0 then it s back camera
* else it s front camera
*/
javaCameraView.setCameraIndex(1);
javaCameraView.setVisibility(SurfaceView.VISIBLE);
javaCameraView.setCvCameraViewListener(this);
}
@Override
protected void onPause() {
super.onPause();
if (javaCameraView != null) {
javaCameraView.disableView();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (javaCameraView != null) {
javaCameraView.disableView();
}
}
@Override
protected void onResume() {
super.onResume();
initOpencv();
}
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
/* imggray=new Mat(height, width, CvType.CV_8UC1);
imgCanny=new Mat(height, width, CvType.CV_8UC1);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);*/
}
@Override
public void onCameraViewStopped() {
mRgba.release();
}
Display display;
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
OpencvClass.faceDetection(mRgba.getNativeObjAddr());
// return dummy value for compiler's sake
return mRgba;
}
// initialisation opencv called in on activity start et on resume
private void initOpencv(){
if (!OpenCVLoader.initDebug()) {
Log.d(TAG, "fail firas");
mloderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
} else {
Log.d(TAG, "sucess firas");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mloderCallback);
}
}
}