我通过 Android Java Bindings Library 为Xamarin.Android使用原生的android Opencv 3.0.0库。
我做的很像 Tutorial ,问题是我得到了这个异常
找不到原生方法:org.opencv.core.Mat.n_Mat :()J
我该怎么做才能解决这个问题?任何帮助将不胜感激,谢谢!
主要活动的源代码:
public class MainActivity : Activity
{
Mat m;
Mat grayM;
ImageView imageView;
public MainActivity()
{
if (!OpenCVLoader.InitDebug())
{
System.Console.WriteLine("GG");
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.myButton);
ImageButton crop = FindViewById<ImageButton>(Resource.Id.crop);
crop.Visibility = ViewStates.Invisible;
imageView = FindViewById<ImageView>(Resource.Id.IMG);
if (IsThereAnAppToTakePictures ())
{
CreateDirectoryForPictures ();
button.Click += delegate
{
Intent intent = new Intent (MediaStore.ActionImageCapture);
App._file = new File (App._dir, String.Format("myPhoto_{0}.jpg", System.Guid.NewGuid()));
intent.PutExtra (MediaStore.ExtraOutput, Android.Net.Uri.FromFile (App._file));
StartActivityForResult (intent, 0);
crop.Visibility = ViewStates.Visible;
// SetImg();
};
}
crop.Click += (sender, e) =>
{
SetImg();
};
}
void SetImg()
{
using (Bitmap _img = BitmapFactory.DecodeFile(App._file.Path))
{
if (_img != null)
{
m = new Mat();
grayM = new Mat();
Utils.BitmapToMat(_img, m);
Imgproc.CvtColor(m, grayM, Imgproc.ColorBgr2gray);
Imgproc.CvtColor(grayM, m, Imgproc.ColorGray2bgra);
using (Bitmap bit = Bitmap.CreateBitmap(m.Cols(), m.Rows(), Bitmap.Config.Argb8888))
{
Utils.BitmapToMat(bit,m);
imageView.SetImageBitmap(bit);
}
m.Release();
grayM.Release();
}
}
}
protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult (requestCode, resultCode, data);
// Make it available in the gallery
Intent mediaScanIntent = new Intent (Intent.ActionMediaScannerScanFile);
Android.Net.Uri contentUri = Android.Net.Uri.FromFile (App._file);
mediaScanIntent.SetData (contentUri);
SendBroadcast (mediaScanIntent);
imageView.SetImageBitmap(BitmapFactory.DecodeFile(contentUri.Path));
// Dispose of the Java side bitmap.
GC.Collect();
}
private void CreateDirectoryForPictures ()
{
App._dir = new File (
Android.OS.Environment.GetExternalStoragePublicDirectory (
Android.OS.Environment.DirectoryPictures), "CameraAppDemo");
if (!App._dir.Exists ())
{
App._dir.Mkdirs( );
}
}
private bool IsThereAnAppToTakePictures ()
{
Intent intent = new Intent (MediaStore.ActionImageCapture);
IList<ResolveInfo> availableActivities =
PackageManager.QueryIntentActivities (intent, PackageInfoFlags.MatchDefaultOnly);
return availableActivities != null && availableActivities.Count > 0;
}
}
public static class App
{
public static File _file;
public static File _dir;
public static Bitmap bitmap;
}
}
异常的记录:
[MonoDroid] UNHANDLED EXCEPTION:[MonoDroid] Java.Lang.UnsatisfiedLinkError:类型异常 &#39; java.lang.UnsatisfiedLinkError中&#39;被扔了。 [MonoDroid]在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61 [MonoDroid]在Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue *)[0x00084] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029 [MonoDroid]在Android.Runtime.JNIEnv.FinishCreateInstance (intptr,intptr,intptr,Android.Runtime.JValue *)[0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:306 [MonoDroid]在Android.Runtime.JNIEnv.FinishCreateInstance (intptr,intptr,intptr,Android.Runtime.JValue [])[0x0001d] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:312 [MonoDroid]在Org.Opencv.Core.Mat..ctor()[0x000da] in
答案 0 :(得分:0)
我尝试用 ILoaderCallbackInterface (实现)做一些技巧:
public class LoaderCallBackHelper: Java.Lang.Object,ILoaderCallbackInterface
{
public void OnManagerConnected(int p0)
{
switch (p0)
{
case LoaderCallbackInterface.Success:
System.Console.WriteLine("Succes");
break;
default:
this.OnManagerConnected(p0);
break;
}
}
public void OnPackageInstall(int p0, IInstallCallbackInterface p1)
{
p1.Install();
System.Console.WriteLine(p1.PackageName);
}
然后是这样的(在 MainActivity 上):
ILoaderCallbackInterface t = new LoaderCallBackHelper();
public MainActivity()
{
if(OpenCVLoader.InitAsync(OpenCVLoader.OpencvVersion3000,this,t))
{
System.Console.WriteLine("OK");
}
}
没有运气。
然后我尝试做其他技巧:
public class BaseLoaderHelper : BaseLoaderCallback
{
public BaseLoaderHelper(Context context):base(context)
{
}
}
BaseLoaderCallback _t = new BaseLoaderHelper(this);
if(OpenCVLoader.InitAsync(OpenCVLoader.OpencvVersion3000,this,_t))
{
System.Console.WriteLine("OK");
}
}
一切都是一样的,OpenCV再也不会初始化! 所以 我的解决方案 是我下载OpenCV版本2.4.11并重新安装到我的项目中。现在所有的都是罚款:
public MainActivity()
{
if (!OpenCVLoader.InitDebug())
{
System.Console.WriteLine("Failed to INIT \n OpenCV Failure");
}
else
{
System.Console.WriteLine("OpenCV INIT Succes");
}
}