如何在flutter中使用starflut导入python包?

Mar*_*bil 5 python opencv python-import flutter

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

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

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

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());
}
Run Code Online (Sandbox Code Playgroud)

提前致谢 。

Jay*_*gar 2

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