zxing 集成到 monodroid 应用程序中

发布于 2024-10-31 18:00:24 字数 449 浏览 3 评论 0原文

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

另外,如果有人有任何其他可以实施的 3 方条形码扫描仪,请在评论中提出这些建议。

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺孤伤于心 2024-11-07 18:00:24

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

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

接下来就是从C#调用Java代码的问题。

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

// 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(您可能需要也可能不需要)。

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

鸩远一方 2024-11-07 18:00:24

我们现在有用于 MonoTouchMonodroidZXing 端口。
https://github.com/Redth/ZXing.Net.Mobile

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文