zxing 集成到 monodroid 应用程序中 [英] zxing integration into monodroid app

查看:30
本文介绍了zxing 集成到 monodroid 应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ZXing 的条码扫描器集成到 MonoDroid 应用程序中.我看到普通的 Android (java) 应用程序有 IntentIntegration.java 和 IntentResult.java 以包含到他们的项目中以提供帮助.我想知道是否有人将它们移植到 .NET(我没有看到它们被移植到 csharp 项目中.)?我还想知道是否有人以另一种方式实现了 ZXing 来使用他们的应用程序?如果有人与 MonoDroid 集成,需要做什么才能在按钮点击处理程序中启动扫描?

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 include into their project to help. I was wondering if anyone has ported those to .NET (I didn't see them ported in the csharp project.)? I'm also wondering if anyone has implemented ZXing in another way to get to work with their app? If anyone has integrated with MonoDroid, what needs to be done to initiate a scan in a button click handler?

此外,如果有人可以替代实施任何其他 3 方条码扫描器,请将这些建议放在评论中.

Also, if anyone has any other 3 party barcode scanner that could be implemented instead, put those suggestions in the comments.

推荐答案

第一个问题是,你真的需要移植那些文件吗?:-)

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

您可以将 Java 源代码包含到 Mono for Android 项目中;只需将构建操作设置为 AndroidJavaSource,源代码就会被编译到生成的 .apk 中.这也可以通过 .jar 文件来完成.

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.

然后是从C#调用Java代码的问题.

对于 IntentIntegration.javaIntentResult.java可能就足够了,因为这些类型不支持继承(它们是 final).当然,使用 JNIEnv 来调用它们的方法将是 PITA,但可以做到:

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

此外,IntentIntegrator 文档提到提供的 Activity 必须覆盖 Activity.OnActivityResult 方法.

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

综上所述,移植 IntentIntegrator.java 不应该困难,因为它大部分是 Activity.StartActivityForResultAlertDialog 的意图和构造(您可能需要也可能不需要).

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

这篇关于zxing 集成到 monodroid 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆