zxing integration into monodroid app

时光怂恿深爱的人放手 提交于 2019-12-03 03:34:27

The first question is, do you actually need to port those files? :-)

You can include Java source code into a Mono for Android project; just set the Build action to AndroidJavaSource and the source will be compiled into the resulting .apk. This can also be done with .jar files.

Then comes the question of invoking the Java code from C#.

In the case of IntentIntegration.java and IntentResult.java, that may be enough, as those types don't support inheritance (they're final). Granted, using JNIEnv to invoke methods on them would be a PITA, but it can be done:

// Untested code, provided for demo purposes:

// Handle of the Java class we're invoking
IntPtr IntentResult = 
        JNIEnv.FindClass("com/google/zxing/integration/android/IntentIntegrator");
// Handle of the method to invoke
IntPtr IntentResult_initiateScan = 
        JNIEnv.GetMethodID(IntentResult, "initiateScan", 
            "(Landroid/app/Activity;)Landroid/app/AlertDialog;");
            // method signature can be obtained from `javap -s`
// Invoke the method; return value is an AlertDialog instance
IntPtr rAlertDialog = JNIEnv.CallStaticObjectMethod (
        IntentResult, IntentResult_initiateScan, new JValue (someActivity));
// ...and construct a nice managed wrapper over the Java instance.
AlertDialog alertDialog = new AlertDialog (rAlertDialog);

Furthermore, the IntentIntegrator docs mention that the Activity provided must override the Activity.OnActivityResult method.

All that said, porting IntentIntegrator.java shouldn't be that difficult, as most of it is a wrapper over Activity.StartActivityForResult with an appropriate intent and construction of an AlertDialog (which you may or may not need).

We now have ZXing ports for MonoTouch and Monodroid. https://github.com/Redth/ZXing.Net.Mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!