我正在android中使用Opencv,我想将图像从图库加载到Mat,请检查我的代码并帮助我做错了什么?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult( requestCode, resultCode, data );
if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
String selectedImagePath = getPath(imageUri);
Mat imgRgba = Imgcodecs.imread( selectedImagePath );
Mat img = new Mat();
imgRgba.copyTo(img);
}
private String getPath(Uri imageUri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query( imageUri, projection, null, null, null );
if(cursor!=null){
int column_index = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.DATA );
cursor.moveToFirst();
return cursor.getString( column_index );
}
return imageUri.getPath();
}
当我运行它时,它崩溃:
java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.imgcodecs.Imgcodecs.imread_1(java.lang.String) (tried Java_org_opencv_imgcodecs_Imgcodecs_imread_11 and Java_org_opencv_imgcodecs_Imgcodecs_imread_11__Ljava_lang_String_2) at org.opencv.imgcodecs.Imgcodecs.imread_1(Native Method) at org.opencv.imgcodecs.Imgcodecs.imread(Imgcodecs.java:118)
最佳答案
您必须先初始化OpenCV库,然后才能在Android上使用。
在使用OpenCV之前先调用以下代码
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11, this, baseLoaderCallback);
然后,下面的方法将是回调。
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};