zxing integration into monodroid app

前端 未结 2 600
攒了一身酷
攒了一身酷 2021-02-06 10:16

I\'m trying to integrate ZXing\'s barcode scanner into a MonoDroid application. I see that normal Android (java) apps have IntentIntegration.java and IntentResult.java to includ

相关标签:
2条回答
  • 2021-02-06 10:35

    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).

    0 讨论(0)
  • 2021-02-06 10:43

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

    0 讨论(0)
提交回复
热议问题