Zxing integration in monodroid app

I am trying to integrate a ZXing barcode scanner into a MonoDroid application. I see that normal Android (java) applications have IntentIntegration.java and IntentResult.java to include them in their project to help. I was wondering if anyone put them in .NET (I have not seen them ported to a csharp project)? I am also wondering if someone implemented ZXing differently to work with their application? If someone integrated MonoDroid, what needs to be done to start scanning in the button handler?

Also, if someone has another 3-way barcode scanner that can be implemented instead, add these suggestions in the comments.

+6
source share
3 answers

First question: do you really need to transfer these files? :-)

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

Then the question arises of calling Java code from C # .

In the case of IntentIntegration.java and IntentResult.java this may be enough, because these types do not support inheritance (they are final ). Of course, using JNIEnv to call methods on them will be PITA, but this 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); 

In addition, the IntentIntegrator mention that the activity provided should override the Activity.OnActivityResult method.

All that said, porting IntentIntegrator.java should not be so difficult, since most of them are a wrapper over Activity.StartActivityForResult with the appropriate intent and construction of an AlertDialog (which you may or may not need).

+4
source

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

0
source

Source: https://habr.com/ru/post/885623/


All Articles