Zxing集成到monodroid应用程序
本文关键字:应用程序 monodroid 集成 Zxing | 更新日期: 2023-09-27 17:49:33
我正在尝试将ZXing的条形码扫描器集成到MonoDroid应用程序中。我看到一般的Android (java)应用都有IntentIntegration.java和IntentResult.java来帮助他们的项目。我想知道是否有人将这些移植到。net(我没有看到它们在csharp项目中移植)?我也想知道是否有人以另一种方式实现ZXing来使用他们的应用程序?如果有人集成了MonoDroid,需要做什么来启动一个按钮点击处理程序扫描?
另外,如果有人有任何其他三方条码扫描器可以实现,请在评论中提出这些建议。
第一个问题是,您真的需要移植这些文件吗?: -)
你可以将Java源代码包含到Mono for Android项目中;只需将Build操作设置为AndroidJavaSource
,源代码将被编译成结果。apk。这也可以用.jar
文件完成。
接下来的问题是如何从c#中调用Java代码。
在IntentIntegration.java
和IntentResult.java
的情况下,可能就足够了,因为这些类型不支持继承(它们是final
)。当然,使用JNIEnv来调用它们的方法将是一个难题,但它可以这样做:
// 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);
此外,IntentIntegrator
文档提到所提供的Activity必须覆盖Activity。OnActivityResult方法。
IntentIntegrator.java
应该没有那么困难,因为它的大部分是对Activity的包装。StartActivityForResult带有适当的意图和AlertDialog
的构造(您可能需要也可能不需要)。
我们现在有MonoTouch
和Monodroid
的ZXing
端口。https://github.com/Redth/ZXing.Net.Mobile