2

我正在为具有内置热敏打印机的 android 设备构建一个带有 kivy 的应用程序。我已经找到了可以让我访问打印机的 API,并且是用 java 编写的(所以我使用的是pyjnius),但我无法让它工作。我在一个名为“JarExplorer”的程序中看到,我感兴趣的构造函数类需要一个名为“ android.content.context Context ”的参数,但我不知道如何正确传递它。

这是我尝试的python代码:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from jnius import autoclass

# Java Context class
Context = autoclass('android.content.Context')

# Java ThermalPrinter class
Printer = autoclass('com.telpo.tps550.api.printer.UsbThermalPrinter')

class Box(BoxLayout):
    def construct_method1(self):
        self.Printer = Printer()

    def construct_method2(self):
        self.Printer = Printer('android/content/Context')

    def construct_method3(self):
        self.Printer = Printer(Context())

    def print_something(self):
        # Open the printer
        self.Printer.start()
        # Specifying text
        self.Printer.addString(self.ids.input.text)
        # Print text
        self.Printer.printString()
        # Release the printer
        self.Printer.stop()

class MainApp(App):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

MainApp().run()

使用方法 1,logcat 确认我需要传递 android 上下文:

06-02 20:41:29.688 28544 28925 I python  :    File "/home/ivo/Escritorio/Printer/.buildozer/android/app/main.py", line 14, in construct_method1
06-02 20:41:29.689 28544 28925 I python  :    File "jnius/jnius_export_class.pxi", line 256, in jnius.jnius.JavaClass.__init__
06-02 20:41:29.690 28544 28925 I python  :    File "jnius/jnius_export_class.pxi", line 294, in jnius.jnius.JavaClass.call_constructor
06-02 20:41:29.691 28544 28925 I python  :  jnius.jnius.JavaException: Invalid call, number of argument mismatch for constructor, available: ['(Landroid/content/Context;)V']
06-02 20:41:29.691 28544 28925 I python  : Python for android ended.

使用方法 2的 Logcat :

06-02 20:44:33.109 29084 29126 I python  :    File "jnius/jnius_export_class.pxi", line 256, in jnius.jnius.JavaClass.__init__
06-02 20:44:33.110 29084 29126 I python  :    File "jnius/jnius_export_class.pxi", line 326, in jnius.jnius.JavaClass.call_constructor
06-02 20:44:33.111 29084 29126 I python  :    File "jnius/jnius_conversion.pxi", line 109, in jnius.jnius.populate_args
06-02 20:44:33.112 29084 29126 I python  :  jnius.jnius.JavaException: Invalid python object for this argument. Want 'android/content/Context', got 'android/content/Context'
06-02 20:44:33.112 29084 29126 I python  : Python for android ended.

方法3的Logcat :

06-02 20:47:24.241 30838 30876 F rg.test.printe: java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: can't make objects of type android.content.Context: 0x6fdea688

然后应用程序关闭。

我知道 API 应该可以工作,因为它带有一个可以成功打印但完全用 Java 编写的演示应用程序。

你们知道什么是错的还是更深层次的问题?我非常感谢您的帮助和知识:) 谢谢!

4

1 回答 1

0

我对 android CameraManager有类似的问题。它需要应用程序上下文作为参数。适用于此的最小示例如下:

from jnius import autoclass

CameraManager = autoclass('android.hardware.camera2.CameraManager')
Application = autoclass('android.app.Application')


def android_camera2_test():
    print(Application.getProcessName())
    app = Application()
    cm = CameraManager(app)
    print(cm.getCameraIdList())

由于Application类是从Context类派生的,因此可以正常工作。我不太“流利”地以正确的方式在 android 中正确处理 Context/Application 变量,但它适用于初始测试。

因此,对于您的测试,我建议添加/更改以下几行:

...

Application = autoclass('android.app.Application')

...

class Box(BoxLayout):
    app = Application()

    ...

    def construct_method2(self):
        self.Printer = Printer(self.app)

    ...
于 2021-02-05T21:56:53.440 回答