【问题标题】:How to import python package using starflut in flutter?如何在flutter中使用starflut导入python包?
【发布时间】:2021-04-19 15:10:42
【问题描述】:

我正在尝试将 python 脚本集成到我的 Flutter 应用程序中,我需要在 python 代码中导入 cv2 但它没有找到包 cv2 ,

有没有办法在flutter中使用starflut导入python包?

这是我用来将 Python 代码链接到我的颤振应用程序的代码:

dynamic python = null;
String _outputString = "python 3.6";
StarSrvGroupClass SrvGroup;
String docPath;
String resPath;
StarServiceClass Service;

Future<void> testCallPython() async {
  //Initialize starecore
  StarCoreFactory starcore = await Starflut.getFactory();
  Service = await starcore.initSimple("test", "123", 0, 0, []);

  //Register callback function
  await starcore.regMsgCallBackP(
      (int serviceGroupID, int uMsg, Object wParam, Object lParam) async {
    print("FIRST PRINT $serviceGroupID  $uMsg   $wParam   $lParam");
    return null;
  });
  SrvGroup = await Service["_ServiceGroup"];

  /*---script python--*/
  //Initialize python
  // for android, before run python, share libraries must be copied to app’s local folder.
  bool isAndroid = await Starflut.isAndroid();
  if (isAndroid == true) {
    await Starflut.copyFileFromAssets("Main_darts.py",
        "flutter_assets/starfiles", "flutter_assets/starfiles");
    await Starflut.copyFileFromAssets("python3.6.zip",
        "flutter_assets/starfiles", null); //desRelatePath must be null
    await Starflut.copyFileFromAssets("zlib.cpython-36m.so", null, null);
    await Starflut.copyFileFromAssets("unicodedata.cpython-36m.so", null, null);
    await Starflut.loadLibrary("libpython3.6m.so");
  }

  docPath = await Starflut.getDocumentPath();
  print("docPath = $docPath");

  resPath = await Starflut.getResourcePath();
  print("resPath = $resPath");

// Initialize python
  dynamic rr1 = await SrvGroup.initRaw("python36", Service);

  print("initRaw = $rr1");

// PYTHON SCRIPT
  var Result1 = await SrvGroup.loadRawModule("python", "",
      resPath + "/flutter_assets/starfiles/" + "Main_darts.py", false);
  print("loadRawModule = $Result1");

  python = await Service.importRawContext("python", "", false, "");
  print("python = " + await python.getString());
}

提前致谢。

【问题讨论】:

    标签: python opencv flutter python-import


    【解决方案1】:

    如果你想在你的应用程序中集成 python 包,我强烈建议使用 chaquopy 插件来实现颤振,因为它易于使用并且还支持 python 包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-01
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      相关资源
      最近更新 更多